-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat_bot.py
More file actions
73 lines (57 loc) · 2.14 KB
/
chat_bot.py
File metadata and controls
73 lines (57 loc) · 2.14 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
from pathlib import Path
from openai import OpenAI
from style_prompt import style_prompt
import os
file_content=None
client = OpenAI(
api_key="sk-AOgesgyRQevFmXQU0USfwoQr4HcC8nSGYOcleJrwW8hBxCRc",
base_url="https://api.moonshot.cn/v1",
)
messages=[
{
"role": "system",
"content": "你是一个806AI助理,具有强大的能力",
},
]
def change_style(style_choice):
global messages
if "传统型" in style_choice:
messages=style_prompt.messages_traditional
elif "引导型" in style_choice:
messages=style_prompt.messages_guiding
elif "宽松型" in style_choice:
messages=style_prompt.messages_laissez_faire
def ask(question):
# num_of_round = 5
global messages
try:
messages.append({"role": "user", "content": question})
response = client.chat.completions.create(
model="moonshot-v1-8k",
messages=messages,
temperature=0.3,
)
except Exception as e:
print(e)
return e
message_content = response.choices[0].message.content
messages.append({"role": "assistant", "content": message_content})
# if len(messages) > num_of_round * 2 + 1:
# del messages[1:3] # Remove the first round conversation left.
return message_content
def upload_file(input_file):
global file_content
global client
# 将 NamedString 对象转换为字符串,然后编码为字节流
file_object = client.files.create(file=Path(input_file), purpose="file-extract")
# 假设返回的file_object具有id属性
file_content = client.files.content(file_id=file_object.id).text
messages.append({"role": "system", "content": file_content})
def respond(ask_message_content,chat_history=[]):
answer_message_content = ask(ask_message_content)
chat_history.append((ask_message_content,answer_message_content))
#chat_hitstory 是一个元组,每一个都是一个自己来回的对话
return "", chat_history
def sum_file(input_file):
global file_content
return file_content