-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsole_chat.py
40 lines (34 loc) · 1.28 KB
/
console_chat.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
import os
from openai import OpenAI
client = OpenAI(
api_key="sk-",
base_url="https://api.siliconflow.cn/v1",
)
# Whether the user wants to end the conversation
def isExiting(user_input):
completion = client.chat.completions.create(
model="Qwen/Qwen2.5-7B-Instruct",
messages=[
{"role": "system", "content": "You are an assistant trying to figure out whether a user wants to end a conversation. The user input is surrounded by <UserInput> and </UserInput> tags. If the user wants to end the conversation, say 'Y'. Otherwise, say 'N'."},
{"role": "user", "content": "<UserInput>"+user_input+"</UserInput>"},
],
)
return completion.choices[0].message.content == "Y"
msgs = [
{"role": "system", "content": "You are a helpful assistant."},
]
while True:
user_input = input("\nUser: ")
msgs.append({"role": "user", "content": user_input})
completion = client.chat.completions.create(
model="Qwen/Qwen2.5-7B-Instruct",
messages=msgs,
stream=True,
)
msg = ""
for chunk in completion:
print(chunk.choices[0].delta.content, end="", flush=True)
msg += chunk.choices[0].delta.content
msgs.append({"role": "assistant", "content": msg})
if isExiting(user_input):
break