Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ share/python-wheels/
.installed.cfg
*.egg
MANIFEST
langcache-venv/*
langcache/.DS_Store
test/.DS_Store
.DS_Store
.vscode/*

# PyInstaller
# Usually these files are written by a python script from a template
Expand All @@ -50,6 +55,7 @@ coverage.xml
.hypothesis/
.pytest_cache/
cover/
test/evadb_data/*

# Translations
*.mo
Expand Down
51 changes: 50 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,50 @@
# langcache
# langcache
LangCache is a semantic caching library developed for Large Language Model (LLM) queries. Its primary purpose is to address the cost concerns associated with LLM API calls and to improve the speed of LLM applications.

### Installation of langcache for developer setup
To install langcache, we recommend using the pip package manager.

1. Create a new virtual environment called langcache-venv.
```python -m venv langcache-venv```

2. Now, activate the virtual environment:
```source langcache-venv/bin/activate```

3. Install the dependecies
```pip install .```

### OpenAI ChatGPT 3.5 ChatCompletion API usage with langcache enabled

Before running the example, make sure the `OPENAI_API_KEY` environment variable is set by executing `echo $OPENAI_API_KEY`.
If it is not already set, it can be set by using `export OPENAI_API_KEY=YOUR_API_KEY` on Unix/Linux/MacOS systems or `set OPENAI_API_KEY=YOUR_API_KEY` on Windows systems.

#### OpenAI API original usage
```
import openai

question = "What is ChatGPT?"
completion = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content":"You are an helpful assistant to answer all my questions within 15 words limit"},
{"role": "user", "content": question}
]
)
```
#### OpenAI API + LangCache, similar search cache
```
from langcache.adapter.openai import OpenAI
from langcache.core import Cache

cache = Cache(tune_frequency=5, tune_policy="recall")
client = OpenAI(cache)

question = "What is ChatGPT?"
completion = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content":"You are an helpful assistant to answer all my questions within 15 words limit"},
{"role": "user", "content": question}
]
)
```
Empty file added langcache/__init__.py
Empty file.
24 changes: 24 additions & 0 deletions langcache/adapter/openai.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import openai
from ..core import Cache

class LangCacheCompletions(openai.resources.chat.completions.Completions):
# TODO: Add remaining args
def create(self, messages, model):
# Get the last message with role "user"
last_user_message = [message for message in messages if message["role"] == "user"][-1]["content"]
cached_value = self._client.cache.get(last_user_message)

if cached_value is None:
llm_output = openai.resources.chat.completions.Completions.create(self, messages=messages, model=model).choices[0].message.content
self._client.cache.put(last_user_message, llm_output)
return llm_output
else:
return cached_value


class OpenAI(openai.OpenAI):
# TODO: Add remaining args
def __init__(self, cache):
super(OpenAI, self).__init__()
self.cache = cache
self.chat.completions = LangCacheCompletions(self)
7 changes: 4 additions & 3 deletions langcache/core.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os
import evadb
import openai
from openai import OpenAI
import string
import random
import multiprocessing as mp
Expand All @@ -17,6 +17,7 @@
class Cache:
def __init__(self, name=None, tune_frequency=5, tune_policy="precision"):
self.cursor = evadb.connect().cursor()
self.client = OpenAI()

# Setup needed functions.
self.cursor.query(
Expand Down Expand Up @@ -72,7 +73,7 @@ def _evaluate_and_tune(self, key: str, ret_key: str, distance: float, **kwargs):
# LLM tuning.
self.llm_msg[1]["content"] = f""" "{ret_key}" , "{key}" """
response = (
openai.ChatCompletion.create(
self.client.chat.completions.create(
model="gpt-3.5-turbo",
messages=self.llm_msg,
)
Expand Down Expand Up @@ -165,4 +166,4 @@ def put(self, key: str, value: str):
f"""
INSERT INTO {self.cache_name} (key, value) VALUES ("{key}", "{value}")
"""
).df()
).df()
Binary file added langcache/evadb_data/evadb.db
Binary file not shown.
Empty file.
131 changes: 131 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
aenum==3.1.15
aiohttp==3.9.0
aiosignal==1.3.1
anyio==3.7.1
appnope==0.1.3
asttokens==2.4.1
attrs==23.1.0
backcall==0.2.0
beautifulsoup4==4.12.2
bs4==0.0.1
certifi==2023.11.17
cffi==1.16.0
charset-normalizer==3.3.2
click==8.1.7
comm==0.2.0
contourpy==1.2.0
cryptography==41.0.5
cycler==0.12.1
dataclasses-json==0.6.2
debugpy==1.8.0
decorator==5.1.1
diskcache==5.6.3
distro==1.8.0
evadb==0.3.9
executing==2.0.1
faiss-cpu==1.7.4
fastjsonschema==2.19.0
filelock==3.13.1
fonttools==4.45.1
frozenlist==1.4.0
fsspec==2023.10.0
gpt4all==2.0.2
h11==0.14.0
httpcore==1.0.2
httpx==0.25.1
huggingface-hub==0.19.4
idna==3.4
iniconfig==2.0.0
ipykernel==6.26.0
ipython==8.12.3
ipywidgets==8.1.1
jedi==0.19.1
Jinja2==3.1.2
joblib==1.3.2
jsonpatch==1.33
jsonpointer==2.4
jsonschema==4.20.0
jsonschema-specifications==2023.11.1
jupyter_client==8.6.0
jupyter_core==5.5.0
jupyterlab-widgets==3.0.9
kiwisolver==1.4.5
langcache==0.0.1
langchain==0.0.340
langsmith==0.0.66
lark==1.1.8
MarkupSafe==2.1.3
marshmallow==3.20.1
matplotlib==3.8.2
matplotlib-inline==0.1.6
mpmath==1.3.0
multidict==6.0.4
mypy-extensions==1.0.0
nbclient==0.6.8
nbformat==5.9.2
nbmake==1.4.6
nest-asyncio==1.5.8
networkx==3.2.1
nltk==3.8.1
numpy==1.26.2
openai==1.3.5
packaging==23.2
pandas==2.1.3
parso==0.8.3
pdfminer.six==20221105
pexpect==4.9.0
pickleshare==0.7.5
Pillow==10.1.0
platformdirs==4.0.0
pluggy==1.3.0
prompt-toolkit==3.0.41
protobuf==4.25.1
psutil==5.9.6
ptyprocess==0.7.0
pure-eval==0.2.2
py==1.11.0
pycparser==2.21
pydantic==1.10.13
Pygments==2.17.2
PyMuPDF==1.22.5
pyparsing==3.1.1
pytest==7.4.3
python-dateutil==2.8.2
pytz==2023.3.post1
PyYAML==6.0.1
pyzmq==25.1.1
rapidfuzz==3.5.2
referencing==0.31.0
regex==2023.10.3
requests==2.31.0
retry==0.9.2
rpds-py==0.13.1
safetensors==0.4.0
scikit-learn==1.3.2
scipy==1.11.4
sentence-transformers==2.2.2
sentencepiece==0.1.99
six==1.16.0
sniffio==1.3.0
soupsieve==2.5
SQLAlchemy==2.0.23
SQLAlchemy-Utils==0.41.1
stack-data==0.6.3
sympy==1.12
tenacity==8.2.3
thefuzz==0.20.0
threadpoolctl==3.2.0
tokenizers==0.15.0
torch==2.1.1
torchvision==0.16.1
tornado==6.3.3
tqdm==4.66.1
traitlets==5.13.0
transformers==4.35.2
typing-inspect==0.9.0
typing_extensions==4.8.0
tzdata==2023.3
urllib3==2.1.0
wcwidth==0.2.12
widgetsnbextension==4.0.9
yarl==1.9.3
27 changes: 27 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import setuptools

with open("README.md", "r", encoding="utf-8") as fh:
long_description = fh.read()

# Function to read the list of dependencies from requirements.txt
def load_requirements(filename='requirements.txt'):
with open(filename, 'r') as file:
return file.read().splitlines()

setuptools.setup(
name="langcache",
version="0.0.1",
author="Georgia Tech Database Group",
author_email="jiashenc@gatech.edu",
description="A small example package",
long_description=long_description,
long_description_content_type="text/markdown",
url="https://github.com/jiashenC/langcache",
packages=setuptools.find_packages(),
classifiers=[
"Programming Language :: Python :: 3",
"Development Status :: 1 - Planning"
],
python_requires='>=3.6',
install_requires=load_requirements(),
)
Loading