-
Notifications
You must be signed in to change notification settings - Fork 0
/
ai.py
51 lines (41 loc) · 1.44 KB
/
ai.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
import g4f
from postgresql import (
read_history,
edit_history
)
from config import Config
import json
max_questions = Config.MAX_CONTEXT_QUESTIONS
async def start_conversation(instructions):
messages = [
{"role": "system", "content": instructions},
{"role": "user", "content": "Привет! Кто ты?"}
]
completion = await g4f.ChatCompletion.create_async(
provider=g4f.Provider.Bing,
model=g4f.models.gpt_4_32k,
messages=messages,
stream=False
)
return completion
async def update(instructions, user_id, new_question, photo):
messages = [
{"role": "system", "content": instructions},
]
history = await read_history(user_id)
previous_questions_and_answers = json.loads(history) if history else []
for question, answer in previous_questions_and_answers[-max_questions:]:
messages.append({"role": "user", "content": question})
messages.append({"role": "assistant", "content": answer})
messages.append({"role": "user", "content": new_question})
completion = await g4f.ChatCompletion.create_async(
provider=g4f.Provider.Bing,
model=g4f.models.gpt_4_32k,
messages=messages,
stream=False,
image=photo
)
previous_questions_and_answers.append((new_question, completion))
history = json.dumps(previous_questions_and_answers)
await edit_history(history, user_id)
return completion