forked from zilliztech/GPTCache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqlite_faiss_towhee.py
60 lines (47 loc) · 1.87 KB
/
sqlite_faiss_towhee.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import os
import time
from gptcache.adapter import openai
from gptcache.core import cache, Config
from gptcache.cache.factory import get_ss_data_manager
from gptcache.ranker.simple import SearchDistanceEvaluation
from gptcache.encoder import Towhee
def run():
towhee = Towhee()
# chinese model
# towhee = Towhee(model="uer/albert-base-chinese-cluecorpussmall")
sqlite_file = "sqlite.db"
faiss_file = "faiss.index"
has_data = os.path.isfile(sqlite_file) and os.path.isfile(faiss_file)
data_manager = get_ss_data_manager("sqlite", "faiss",
dimension=towhee.dimension(), max_size=2000)
def log_time_func(func_name, delta_time):
print("func `{}` consume time: {:.2f}s".format(func_name, delta_time))
cache.init(embedding_func=towhee.to_embeddings,
data_manager=data_manager,
similarity_evaluation=SearchDistanceEvaluation(),
config=Config(
log_time_func=log_time_func,
),
)
if not has_data:
question = "what do you think about chatgpt"
answer = "chatgpt is a good application"
cache.data_manager.save(question, answer, cache.embedding_func(question))
mock_messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "what do you feel like chatgpt"}
]
# mock_messages = [
# {"role": "system", "content": "You are a helpful assistant."},
# {"role": "user", "content": "what do you think chatgpt"}
# ]
start_time = time.time()
answer = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=mock_messages,
)
end_time = time.time()
print("cache hint time consuming: {:.2f}s".format(end_time - start_time))
print(answer)
if __name__ == '__main__':
run()