forked from zilliztech/GPTCache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.py
46 lines (38 loc) · 1.52 KB
/
map.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
import os
from gptcache.cache.factory import get_data_manager
from gptcache.adapter import openai
from gptcache.core import cache, Cache
def run():
dir_name, _ = os.path.split(os.path.abspath(__file__))
bak_cache = Cache()
bak_data_file = dir_name + "/data_map_bak.txt"
bak_cache.init(data_manager=get_data_manager("map",
data_path=bak_data_file,
max_size=10))
data_file = dir_name + "/data_map.txt"
cache.init(data_manager=get_data_manager("map",
data_path=data_file,
max_size=10),
next_cache=bak_cache)
cache.set_openai_key()
mock_messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "foo15"}
]
if not os.path.isfile(bak_data_file):
for i in range(10):
question = f"foo{i}"
answer = f"receiver the foo {i}"
cache.data_manager.save(question, answer, cache.embedding_func(question))
if not os.path.isfile(data_file):
for i in range(10, 20):
question = f"foo{i}"
answer = f"receiver the foo {i}"
bak_cache.data_manager.save(question, answer, bak_cache.embedding_func(question))
answer = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=mock_messages,
)
print(answer)
if __name__ == '__main__':
run()