diff --git a/docs/release_note.md b/docs/release_note.md index deee57a7..a8360b85 100644 --- a/docs/release_note.md +++ b/docs/release_note.md @@ -5,6 +5,42 @@ To read the following content, you need to understand the basic use of GPTCache, - [Readme doc](https://github.com/zilliztech/GPTCache) - [Usage doc](https://github.com/zilliztech/GPTCache/blob/main/docs/usage.md) +## v0.1.24 (2023.5.15) + +1. Support the langchain embedding + +```python +from gptcache.embedding import LangChain +from langchain.embeddings.openai import OpenAIEmbeddings + +test_sentence = 'Hello, world.' +embeddings = OpenAIEmbeddings(model="your-embeddings-deployment-name") +encoder = LangChain(embeddings=embeddings) +embed = encoder.to_embeddings(test_sentence) +``` + +2. Add gptcache client + +```python +from gptcache import Client + +client = Client() +client.put("Hi", "Hi back") +ans = client.get("Hi") +``` + +3. Support pgvector as vector store + +```python +from gptcache.manager import manager_factory + +data_manager = manager_factory("sqlite,pgvector", vector_params={"dimension": 10}) +``` + +4. Add the GPTCache server doc + +reference: https://github.com/zilliztech/GPTCache/blob/main/docs/usage.md#Build-GPTCache-server + ## v0.1.23 (2023.5.11) 1. Support the session for the `LangChainLLMs` diff --git a/gptcache/adapter/openai.py b/gptcache/adapter/openai.py index c37c7ec2..502b3e6c 100644 --- a/gptcache/adapter/openai.py +++ b/gptcache/adapter/openai.py @@ -1,16 +1,13 @@ -import time -from typing import Iterator, Any - -import os -import openai - import base64 +import os +import time from io import BytesIO +from typing import Iterator, Any -from gptcache.utils import import_pillow -from gptcache.utils.error import CacheError from gptcache.adapter.adapter import adapt from gptcache.manager.scalar_data.base import Answer, DataType +from gptcache.utils import import_openai, import_pillow +from gptcache.utils.error import CacheError from gptcache.utils.response import ( get_stream_message_from_openai_answer, get_message_from_openai_answer, @@ -20,6 +17,10 @@ get_audio_text_from_openai_answer, ) +import_openai() + +import openai # pylint: disable=C0413 + class ChatCompletion(openai.ChatCompletion): """Openai ChatCompletion Wrapper diff --git a/gptcache/core.py b/gptcache/core.py index 7483d54d..2fdfd54f 100644 --- a/gptcache/core.py +++ b/gptcache/core.py @@ -2,8 +2,6 @@ import os from typing import Optional, List, Any -import openai - from gptcache.config import Config from gptcache.embedding.string import to_embeddings as string_embedding from gptcache.manager import get_data_manager @@ -13,6 +11,7 @@ from gptcache.report import Report from gptcache.similarity_evaluation import ExactMatchEvaluation from gptcache.similarity_evaluation import SimilarityEvaluation +from gptcache.utils import import_openai from gptcache.utils.cache_func import cache_all from gptcache.utils.log import gptcache_log @@ -109,6 +108,9 @@ def flush(self): @staticmethod def set_openai_key(): + import_openai() + import openai # pylint: disable=C0415 + openai.api_key = os.getenv("OPENAI_API_KEY") diff --git a/gptcache/embedding/__init__.py b/gptcache/embedding/__init__.py index 2c34f410..79301459 100644 --- a/gptcache/embedding/__init__.py +++ b/gptcache/embedding/__init__.py @@ -1,4 +1,4 @@ -__all__ = ["OpenAI", "Huggingface", "SBERT", "Cohere", "Onnx", "FastText", "Data2VecAudio", "Timm", "ViT"] +__all__ = ["OpenAI", "Huggingface", "SBERT", "Cohere", "Onnx", "FastText", "Data2VecAudio", "Timm", "ViT", "LangChain"] from gptcache.utils.lazy_import import LazyImport diff --git a/gptcache/embedding/openai.py b/gptcache/embedding/openai.py index 5ac5cc0a..ac00333a 100644 --- a/gptcache/embedding/openai.py +++ b/gptcache/embedding/openai.py @@ -1,9 +1,13 @@ -import numpy as np -import openai import os +import numpy as np + from gptcache.embedding.base import BaseEmbedding +from gptcache.utils import import_openai + +import_openai() +import openai # pylint: disable=C0413 class OpenAI(BaseEmbedding): """Generate text embedding for given text using OpenAI. diff --git a/gptcache/manager/vector_data/manager.py b/gptcache/manager/vector_data/manager.py index 9a13bb41..9568e6a0 100644 --- a/gptcache/manager/vector_data/manager.py +++ b/gptcache/manager/vector_data/manager.py @@ -115,11 +115,11 @@ def get(name, **kwargs): collection_name = kwargs.get("collection_name", COLLECTION_NAME) index_params = kwargs.get("index_params", PGVECTOR_INDEX_PARAMS) vector_base = PGVector( - dimension = dimension, - top_k = top_k, - url = url, - collection_name = collection_name, - index_params = index_params + dimension=dimension, + top_k=top_k, + url=url, + collection_name=collection_name, + index_params=index_params ) else: raise NotFoundError("vector store", name) diff --git a/gptcache/utils/__init__.py b/gptcache/utils/__init__.py index 5134f564..518b5e1b 100644 --- a/gptcache/utils/__init__.py +++ b/gptcache/utils/__init__.py @@ -28,6 +28,7 @@ "import_ruamel", "import_selective_context", "import_httpx", + "import_openai", ] import importlib.util @@ -194,5 +195,10 @@ def import_ruamel(): def import_selective_context(): _check_library("selective_context") + def import_httpx(): _check_library("httpx") + + +def import_openai(): + _check_library("openai") diff --git a/requirements.txt b/requirements.txt index fced22a9..f16e4145 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,3 @@ -openai numpy cachetools requests \ No newline at end of file diff --git a/setup.py b/setup.py index 4fb13cfd..e8d98128 100644 --- a/setup.py +++ b/setup.py @@ -18,7 +18,7 @@ def parse_requirements(file_name: str) -> List[str]: setuptools.setup( name="gptcache", packages=find_packages(), - version="0.1.23", + version="0.1.24", author="SimFG", author_email="bang.fu@zilliz.com", description="GPTCache, a powerful caching library that can be used to speed up and lower the cost of chat "