Spaces:
Sleeping
Sleeping
Commit
·
99bd06e
1
Parent(s):
2a13fc7
Update app.py
Browse files
app.py
CHANGED
@@ -1,82 +1,65 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
|
17 |
-
|
18 |
-
|
19 |
-
user_messages.append(message["content"])
|
20 |
-
elif message["role"] == "assistant":
|
21 |
-
assistant_messages.append(message["content"])
|
22 |
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
)
|
27 |
-
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
|
|
|
|
|
|
35 |
|
36 |
-
return formatted_history
|
37 |
|
38 |
-
|
39 |
-
|
40 |
-
)
|
41 |
-
history_messages = state
|
42 |
-
if history_messages == None:
|
43 |
-
history_messages = []
|
44 |
-
history_messages.append({"role": "system", "content": "A helpful assistant."})
|
45 |
-
|
46 |
-
history_messages.append({"role": "user", "content": message})
|
47 |
-
# We have no content for the assistant's response yet but we will update this:
|
48 |
-
history_messages.append({"role": "assistant", "content": ""})
|
49 |
-
|
50 |
-
response_message = ""
|
51 |
-
|
52 |
-
chat_generator = client.create(
|
53 |
-
messages=history_messages, stream=True, model=model_name
|
54 |
-
)
|
55 |
-
|
56 |
-
for chunk in chat_generator:
|
57 |
-
if "choices" in chunk:
|
58 |
-
for choice in chunk["choices"]:
|
59 |
-
if "delta" in choice and "content" in choice["delta"]:
|
60 |
-
new_token = choice["delta"]["content"]
|
61 |
-
# Add the latest token:
|
62 |
-
response_message += new_token
|
63 |
-
# Update the assistant's response in our model:
|
64 |
-
history_messages[-1]["content"] = response_message
|
65 |
-
|
66 |
-
if "finish_reason" in choice and choice["finish_reason"] == "stop":
|
67 |
-
break
|
68 |
-
formatted_history = create_formatted_history(history_messages)
|
69 |
-
yield formatted_history, history_messages
|
70 |
-
|
71 |
-
chatbot = gr.Chatbot(label="Chat").style(color_map=("yellow", "purple"))
|
72 |
-
iface = gr.Interface(
|
73 |
-
fn=chat,
|
74 |
-
inputs=[
|
75 |
-
gr.Textbox(placeholder="Hello! How are you? etc.", label="Message"),
|
76 |
-
"state",
|
77 |
-
],
|
78 |
-
outputs=[chatbot, "state"],
|
79 |
-
allow_flagging="never",
|
80 |
-
)
|
81 |
-
|
82 |
-
iface.queue().launch()
|
|
|
1 |
+
import requests
|
2 |
+
import pickle
|
3 |
+
# 用于存储API返回的上下文
|
4 |
+
class gpt:
|
5 |
+
def send_request(self, messages):
|
6 |
+
# 设置代理服务器的地址和端口
|
7 |
+
proxies = {
|
8 |
+
"http": "http://127.0.0.1:7890",
|
9 |
+
"https": "http://127.0.0.1:7890"
|
10 |
+
}
|
11 |
+
# ChatGPT API的URL
|
12 |
+
url = "https://api.openai.com/v1/chat/completions"
|
13 |
+
# ChatGPT API的访问密钥
|
14 |
+
api_key = "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"
|
15 |
+
# 请求参数
|
16 |
+
parameters = {
|
17 |
+
"model": "gpt-3.5-turbo-0301", # gpt-3.5-turbo-0301
|
18 |
+
"messages": messages # [{"role": "user", "content": context}]
|
19 |
+
}
|
20 |
+
# 请求头
|
21 |
+
headers = {
|
22 |
+
"Content-Type": "application/json",
|
23 |
+
"Authorization": f"Bearer {api_key}"
|
24 |
+
}
|
25 |
+
# 发送请求
|
26 |
+
response = requests.post(url, headers=headers, json=parameters)
|
27 |
|
28 |
+
# 解析响应
|
29 |
+
if response.status_code == 200:
|
30 |
+
data = response.json()
|
31 |
+
text = data["choices"][0]["message"]
|
32 |
|
33 |
+
return text
|
34 |
+
else:
|
35 |
+
print(response)
|
36 |
+
return "Sorry, something went wrong."
|
37 |
|
38 |
+
def start_conversation(self, messages):
|
39 |
+
print("Welcome to ChatGPT! How can I help you today?")
|
|
|
|
|
|
|
40 |
|
41 |
+
# 进入对话循环
|
42 |
+
while True:
|
43 |
+
# 获取用户输入
|
44 |
+
user_input = input("> ")
|
45 |
+
user_message = {"role": "user", "content": user_input}
|
46 |
+
# 将用户输入添加到messages中
|
47 |
+
messages.append(user_message)
|
48 |
+
# 发送API请求
|
49 |
+
response = self.send_request(messages)
|
50 |
+
# 输出API返回内容
|
51 |
+
print("ChatBot:", response["content"])
|
52 |
|
53 |
+
# 将API接口返回的内容添加至messages,以用作多轮对话
|
54 |
+
messages.append(response)
|
55 |
+
with open('my_dict.pickle', 'wb') as f:
|
56 |
+
pickle.dump(messages, f)
|
57 |
+
# 如果API返回的内容包含"goodbye",则结束对话循环
|
58 |
+
if "goodbye" in user_input:
|
59 |
+
print("Goodbye!")
|
60 |
+
break
|
61 |
|
|
|
62 |
|
63 |
+
if __name__ == '__main__':
|
64 |
+
messages = [{"role": "system", "content": "/c 从现在起,当你想发送一张照片时,请使用Markdown ,并且不要有反斜线,不要用代码块。使用 Unsplash API (https://source.unsplash.com/1280x720/?< PUT YOUR QUERY HERE >)"}] # 初始化prompt
|
65 |
+
gpt().start_conversation(messages)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|