diff --git a/.gitignore b/.gitignore index 68bc17f..e7449a9 100644 --- a/.gitignore +++ b/.gitignore @@ -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 @@ -50,6 +55,7 @@ coverage.xml .hypothesis/ .pytest_cache/ cover/ +test/evadb_data/* # Translations *.mo diff --git a/README.md b/README.md index cb1ab55..895b477 100644 --- a/README.md +++ b/README.md @@ -1 +1,50 @@ -# langcache \ No newline at end of file +# 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} + ] + ) +``` diff --git a/langcache/__init__.py b/langcache/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/langcache/adapter/openai.py b/langcache/adapter/openai.py new file mode 100644 index 0000000..025792e --- /dev/null +++ b/langcache/adapter/openai.py @@ -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) diff --git a/langcache/core.py b/langcache/core.py index 35b7621..7107ffe 100644 --- a/langcache/core.py +++ b/langcache/core.py @@ -1,6 +1,6 @@ import os import evadb -import openai +from openai import OpenAI import string import random import multiprocessing as mp @@ -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( @@ -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, ) @@ -165,4 +166,4 @@ def put(self, key: str, value: str): f""" INSERT INTO {self.cache_name} (key, value) VALUES ("{key}", "{value}") """ - ).df() + ).df() \ No newline at end of file diff --git a/langcache/evadb_data/evadb.db b/langcache/evadb_data/evadb.db new file mode 100644 index 0000000..a35eb7e Binary files /dev/null and b/langcache/evadb_data/evadb.db differ diff --git a/langcache/statistics/__init__.py b/langcache/statistics/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..7864127 --- /dev/null +++ b/requirements.txt @@ -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 diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..c89f095 --- /dev/null +++ b/setup.py @@ -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(), +) diff --git a/test/open-ai-integration.ipynb b/test/open-ai-integration.ipynb new file mode 100644 index 0000000..ee696db --- /dev/null +++ b/test/open-ai-integration.ipynb @@ -0,0 +1,379 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "# Import dependencies\n", + "import os" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "# get OpenAI key if needed\n", + "\n", + "try:\n", + " api_key = os.environ[\"OPENAI_API_KEY\"]\n", + "except KeyError:\n", + " api_key = str(input(\"🔑 Enter your OpenAI key: \"))\n", + " os.environ[\"OPENAI_API_KEY\"] = api_key" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "/Users/swetavooda/workspace/langCache/langcache/langcache-venv/lib/python3.11/site-packages/sentence_transformers/SentenceTransformer.py:157: FutureWarning: Series.__getitem__ treating keys as positions is deprecated. In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To access a value by position, use `ser.iloc[pos]`\n", + " sentences_sorted = [sentences[idx] for idx in length_sorted_idx]\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "QUESTION: What is GitHub?\n", + "GitHub is a web-based platform for version control and collaboration, focusing on code sharing and projects.\n", + "Time taken: 1.8351471424102783\n", + "\n", + "\n", + "\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "/Users/swetavooda/workspace/langCache/langcache/langcache-venv/lib/python3.11/site-packages/sentence_transformers/SentenceTransformer.py:157: FutureWarning: Series.__getitem__ treating keys as positions is deprecated. In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To access a value by position, use `ser.iloc[pos]`\n", + " sentences_sorted = [sentences[idx] for idx in length_sorted_idx]\n", + "01-25-2024 19:49:43 WARNING[create_index_executor:create_index_executor.py:_create_evadb_index:0119] Index wubhlsyqed already exists. It will be updated on existing table.\n", + "/Users/swetavooda/workspace/langCache/langcache/langcache-venv/lib/python3.11/site-packages/sentence_transformers/SentenceTransformer.py:157: FutureWarning: Series.__getitem__ treating keys as positions is deprecated. In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To access a value by position, use `ser.iloc[pos]`\n", + " sentences_sorted = [sentences[idx] for idx in length_sorted_idx]\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "QUESTION: what are the most important concepts in databases\n", + "Relational model, tables, primary keys, foreign keys, normalization, and query language, such as SQL.\n", + "Time taken: 3.3530569076538086\n", + "\n", + "\n", + "\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "/Users/swetavooda/workspace/langCache/langcache/langcache-venv/lib/python3.11/site-packages/sentence_transformers/SentenceTransformer.py:157: FutureWarning: Series.__getitem__ treating keys as positions is deprecated. In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To access a value by position, use `ser.iloc[pos]`\n", + " sentences_sorted = [sentences[idx] for idx in length_sorted_idx]\n", + "01-25-2024 19:49:45 WARNING[create_index_executor:create_index_executor.py:_create_evadb_index:0119] Index wubhlsyqed already exists. It will be updated on existing table.\n", + "/Users/swetavooda/workspace/langCache/langcache/langcache-venv/lib/python3.11/site-packages/sentence_transformers/SentenceTransformer.py:157: FutureWarning: Series.__getitem__ treating keys as positions is deprecated. In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To access a value by position, use `ser.iloc[pos]`\n", + " sentences_sorted = [sentences[idx] for idx in length_sorted_idx]\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "QUESTION: can you explain what GitHub is\n", + "GitHub is a web-based platform where developers can collaborate on and store their code repositories.\n", + "Time taken: 2.42620587348938\n", + "\n", + "\n", + "\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "/Users/swetavooda/workspace/langCache/langcache/langcache-venv/lib/python3.11/site-packages/sentence_transformers/SentenceTransformer.py:157: FutureWarning: Series.__getitem__ treating keys as positions is deprecated. In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To access a value by position, use `ser.iloc[pos]`\n", + " sentences_sorted = [sentences[idx] for idx in length_sorted_idx]\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "QUESTION: What is GitHub?\n", + "GitHub is a web-based platform for version control and collaboration, focusing on code sharing and projects.\n", + "Time taken: 0.9597771167755127\n", + "\n", + "\n", + "\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "/Users/swetavooda/workspace/langCache/langcache/langcache-venv/lib/python3.11/site-packages/sentence_transformers/SentenceTransformer.py:157: FutureWarning: Series.__getitem__ treating keys as positions is deprecated. In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To access a value by position, use `ser.iloc[pos]`\n", + " sentences_sorted = [sentences[idx] for idx in length_sorted_idx]\n", + "01-25-2024 19:49:49 WARNING[create_index_executor:create_index_executor.py:_create_evadb_index:0119] Index wubhlsyqed already exists. It will be updated on existing table.\n", + "/Users/swetavooda/workspace/langCache/langcache/langcache-venv/lib/python3.11/site-packages/sentence_transformers/SentenceTransformer.py:157: FutureWarning: Series.__getitem__ treating keys as positions is deprecated. In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To access a value by position, use `ser.iloc[pos]`\n", + " sentences_sorted = [sentences[idx] for idx in length_sorted_idx]\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "QUESTION: what is the purpose of GitHub\n", + "The purpose of GitHub is to provide a platform for version control and collaboration on software development projects.\n", + "Time taken: 2.3775222301483154\n", + "\n", + "\n", + "\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "/Users/swetavooda/workspace/langCache/langcache/langcache-venv/lib/python3.11/site-packages/sentence_transformers/SentenceTransformer.py:157: FutureWarning: Series.__getitem__ treating keys as positions is deprecated. In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To access a value by position, use `ser.iloc[pos]`\n", + " sentences_sorted = [sentences[idx] for idx in length_sorted_idx]\n", + "01-25-2024 19:49:51 WARNING[create_index_executor:create_index_executor.py:_create_evadb_index:0119] Index wubhlsyqed already exists. It will be updated on existing table.\n", + "/Users/swetavooda/workspace/langCache/langcache/langcache-venv/lib/python3.11/site-packages/sentence_transformers/SentenceTransformer.py:157: FutureWarning: Series.__getitem__ treating keys as positions is deprecated. In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To access a value by position, use `ser.iloc[pos]`\n", + " sentences_sorted = [sentences[idx] for idx in length_sorted_idx]\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "QUESTION: What's github\n", + "GitHub is a web-based platform for version control and collaborative development of software projects.\n", + "Time taken: 2.712510824203491\n", + "\n", + "\n", + "\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "/Users/swetavooda/workspace/langCache/langcache/langcache-venv/lib/python3.11/site-packages/sentence_transformers/SentenceTransformer.py:157: FutureWarning: Series.__getitem__ treating keys as positions is deprecated. In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To access a value by position, use `ser.iloc[pos]`\n", + " sentences_sorted = [sentences[idx] for idx in length_sorted_idx]\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "QUESTION: What are the important topics to learn in databases?\n", + "Relational model, tables, primary keys, foreign keys, normalization, and query language, such as SQL.\n", + "Time taken: 1.801367998123169\n", + "\n", + "\n", + "\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "/Users/swetavooda/workspace/langCache/langcache/langcache-venv/lib/python3.11/site-packages/sentence_transformers/SentenceTransformer.py:157: FutureWarning: Series.__getitem__ treating keys as positions is deprecated. In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To access a value by position, use `ser.iloc[pos]`\n", + " sentences_sorted = [sentences[idx] for idx in length_sorted_idx]\n", + "01-25-2024 19:49:56 WARNING[create_index_executor:create_index_executor.py:_create_evadb_index:0119] Index wubhlsyqed already exists. It will be updated on existing table.\n", + "/Users/swetavooda/workspace/langCache/langcache/langcache-venv/lib/python3.11/site-packages/sentence_transformers/SentenceTransformer.py:157: FutureWarning: Series.__getitem__ treating keys as positions is deprecated. In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To access a value by position, use `ser.iloc[pos]`\n", + " sentences_sorted = [sentences[idx] for idx in length_sorted_idx]\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "QUESTION: List some topics to learn in databases\n", + "- Data modeling \n", + "- Relational databases \n", + "- SQL \n", + "- Query optimization \n", + "- Indexing \n", + "- Data normalization \n", + "- NoSQL databases \n", + "- Database administration \n", + "- Database security \n", + "- Data warehousing\n", + "Time taken: 3.0051629543304443\n", + "\n", + "\n", + "\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "/Users/swetavooda/workspace/langCache/langcache/langcache-venv/lib/python3.11/site-packages/sentence_transformers/SentenceTransformer.py:157: FutureWarning: Series.__getitem__ treating keys as positions is deprecated. In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To access a value by position, use `ser.iloc[pos]`\n", + " sentences_sorted = [sentences[idx] for idx in length_sorted_idx]\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "QUESTION: Can you tell me what Github is?\n", + "GitHub is a web-based platform where developers can collaborate on and store their code repositories.\n", + "Time taken: 0.9954211711883545\n", + "\n", + "\n", + "\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "/Users/swetavooda/workspace/langCache/langcache/langcache-venv/lib/python3.11/site-packages/sentence_transformers/SentenceTransformer.py:157: FutureWarning: Series.__getitem__ treating keys as positions is deprecated. In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To access a value by position, use `ser.iloc[pos]`\n", + " sentences_sorted = [sentences[idx] for idx in length_sorted_idx]\n", + "01-25-2024 19:50:00 WARNING[create_index_executor:create_index_executor.py:_create_evadb_index:0119] Index wubhlsyqed already exists. It will be updated on existing table.\n", + "/Users/swetavooda/workspace/langCache/langcache/langcache-venv/lib/python3.11/site-packages/sentence_transformers/SentenceTransformer.py:157: FutureWarning: Series.__getitem__ treating keys as positions is deprecated. In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To access a value by position, use `ser.iloc[pos]`\n", + " sentences_sorted = [sentences[idx] for idx in length_sorted_idx]\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "QUESTION: How I can speak English fluently?\n", + "Practice speaking regularly with native English speakers and immerse yourself in the language through reading and listening.\n", + "Time taken: 2.6009669303894043\n", + "\n", + "\n", + "\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "/Users/swetavooda/workspace/langCache/langcache/langcache-venv/lib/python3.11/site-packages/sentence_transformers/SentenceTransformer.py:157: FutureWarning: Series.__getitem__ treating keys as positions is deprecated. In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To access a value by position, use `ser.iloc[pos]`\n", + " sentences_sorted = [sentences[idx] for idx in length_sorted_idx]\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "QUESTION: How can I learn to speak English fluently?\n", + "Practice speaking regularly with native English speakers and immerse yourself in the language through reading and listening.\n", + "Time taken: 0.9783082008361816\n", + "\n", + "\n", + "\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "/Users/swetavooda/workspace/langCache/langcache/langcache-venv/lib/python3.11/site-packages/sentence_transformers/SentenceTransformer.py:157: FutureWarning: Series.__getitem__ treating keys as positions is deprecated. In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To access a value by position, use `ser.iloc[pos]`\n", + " sentences_sorted = [sentences[idx] for idx in length_sorted_idx]\n", + "01-25-2024 19:50:04 WARNING[create_index_executor:create_index_executor.py:_create_evadb_index:0119] Index wubhlsyqed already exists. It will be updated on existing table.\n", + "/Users/swetavooda/workspace/langCache/langcache/langcache-venv/lib/python3.11/site-packages/sentence_transformers/SentenceTransformer.py:157: FutureWarning: Series.__getitem__ treating keys as positions is deprecated. In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To access a value by position, use `ser.iloc[pos]`\n", + " sentences_sorted = [sentences[idx] for idx in length_sorted_idx]\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "QUESTION: How does 3D printing work?\n", + "3D printing works by creating objects layer by layer using melted materials like plastic or metal.\n", + "Time taken: 3.003875732421875\n", + "\n", + "\n", + "\n", + "QUESTION: How do 3D printing work?\n", + "3D printing works by creating objects layer by layer using melted materials like plastic or metal.\n", + "Time taken: 0.9676847457885742\n", + "\n", + "\n", + "\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "/Users/swetavooda/workspace/langCache/langcache/langcache-venv/lib/python3.11/site-packages/sentence_transformers/SentenceTransformer.py:157: FutureWarning: Series.__getitem__ treating keys as positions is deprecated. In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To access a value by position, use `ser.iloc[pos]`\n", + " sentences_sorted = [sentences[idx] for idx in length_sorted_idx]\n" + ] + } + ], + "source": [ + "import time\n", + "from langcache.adapter.openai import OpenAI\n", + "from langcache.core import Cache\n", + "\n", + "cache = Cache(tune_frequency=5, tune_policy=\"recall\")\n", + "client = OpenAI(cache)\n", + "\n", + "results = []\n", + "questions = [\"What is GitHub?\", \n", + " \"what are the most important concepts in databases\", \n", + " \"can you explain what GitHub is\", \"What is GitHub?\", \n", + " \"what is the purpose of GitHub\", \"What's github\", \n", + " \"What are the important topics to learn in databases?\", \n", + " \"List some topics to learn in databases\",\n", + " \"Can you tell me what Github is?\",\n", + " \"How I can speak English fluently?\",\n", + " \"How can I learn to speak English fluently?\",\n", + " \"How does 3D printing work?\",\n", + " \"How do 3D printing work?\"]\n", + "for i in range(0,len(questions)):\n", + " start_time = time.time()\n", + " completion = client.chat.completions.create(\n", + " model=\"gpt-3.5-turbo\",\n", + " messages=[\n", + " {\"role\": \"system\", \"content\":\"You are an helpful assistant to answer all my questions within 15 words limit\"},\n", + " {\"role\": \"user\", \"content\": questions[i]}\n", + " ]\n", + " )\n", + " end_time = time.time()\n", + " print(\"QUESTION: \", questions[i])\n", + " print(completion)\n", + " print(\"Time taken: \", end_time-start_time)\n", + " print(\"\\n\\n\")\n", + " results.append(completion)\n", + " \n" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "langcache-venv", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.11.7" + }, + "orig_nbformat": 4 + }, + "nbformat": 4, + "nbformat_minor": 2 +}