-
Notifications
You must be signed in to change notification settings - Fork 518
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use temperature to control possibility of skip_cache (#306)
Signed-off-by: Jael Gu <mengjia.gu@zilliz.com>
- Loading branch information
Showing
5 changed files
with
87 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,43 @@ | ||
import os | ||
import time | ||
|
||
from gptcache import cache, Config | ||
from gptcache.manager import manager_factory | ||
from gptcache.embedding import Onnx | ||
from gptcache.processor.pre import get_prompt | ||
from gptcache.processor.post import temperature_softmax | ||
from gptcache.similarity_evaluation.distance import SearchDistanceEvaluation | ||
from gptcache.adapter.api import put, get | ||
from gptcache.adapter import openai | ||
|
||
cache.set_openai_key() | ||
|
||
# Init cache with vector store | ||
if os.path.exists("faiss.index"): | ||
os.remove("faiss.index") | ||
if os.path.exists("sqlite.db"): | ||
os.remove("sqlite.db") | ||
# if os.path.exists("faiss.index"): | ||
# os.remove("faiss.index") | ||
# if os.path.exists("sqlite.db"): | ||
# os.remove("sqlite.db") | ||
|
||
onnx = Onnx() | ||
data_manager = manager_factory("sqlite,faiss", vector_params={"dimension": onnx.dimension}) | ||
|
||
|
||
cache.init( | ||
pre_embedding_func=get_prompt, | ||
embedding_func=onnx.to_embeddings, | ||
data_manager=data_manager, | ||
similarity_evaluation=SearchDistanceEvaluation(), | ||
post_process_messages_func=temperature_softmax | ||
) | ||
# cache.config = Config(similarity_threshold=0.2) | ||
|
||
# Input some prepared data to mock a cache with data stored | ||
my_data = [ | ||
{"Q": "What is the most popular vector database?", "A": "Milvus!"}, | ||
{"Q": "What are most popular vector databases?", "A": "Milvus, Milvus, still Milvus ..."}, | ||
{"Q": "What is vector database?", "A": "Vector database is xxxx."}, | ||
{"Q": "Is Milvus an open-source vector database?", "A": "Yes, Milvus is open source."}, | ||
{"Q": "What is Zilliz cloud?", "A": "Zilliz cloud provides vector database on cloud."}, | ||
{"Q": "What is Milvus?", "A": "Milvus is an open-source vector database."}, | ||
{"Q": "Can you recommend a vector database?", "A": "Sure, Milvus is a good choice for vector database."}, | ||
{"Q": "Is Zilliz Cloud free?", "A": "No, Zilliz Cloud charges for instance."}, | ||
{"Q": "How many credits can I get for Zilliz Cloud?", "A": "A new user of Zilliz Cloud will get 350 credits."}, | ||
{"Q": "Do you like GPTCache?", "A": "Yea! GPTCache is great!"}, | ||
] | ||
|
||
for qa in my_data: | ||
put(prompt=qa["Q"], data=qa["A"], skip_cache=True) | ||
|
||
|
||
# use cache without temperature (temperature=0.0) | ||
for _ in range(5): | ||
answer = get(prompt="popular vector database") | ||
print(answer) | ||
|
||
# use cache with temperature (eg. temperature=2.0) | ||
for _ in range(5): | ||
answer = get(prompt="popular vector database", temperature=2.0) | ||
print(answer) | ||
question = 'what is github' | ||
|
||
for _ in range(3): | ||
start = time.time() | ||
response = openai.ChatCompletion.create( | ||
model='gpt-3.5-turbo', | ||
temperature = 1.0, # Change temperature here | ||
messages=[{ | ||
'role': 'user', | ||
'content': question | ||
}], | ||
) | ||
print(round(time.time() - start, 3)) | ||
print(response["choices"][0]["message"]["content"]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,3 +16,4 @@ pytest-sugar==0.9.5 | |
pytest-parallel | ||
torch | ||
mock | ||
pexpect |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters