Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,8 @@
|
|
1 |
import gradio as gr
|
2 |
import requests
|
3 |
import time
|
|
|
|
|
4 |
|
5 |
# 设置 FastGPT API 的基本信息
|
6 |
#API_URL = "https://api.fastgpt.in/api/v1/chat/completions" # 请根据您的部署地址修改
|
@@ -16,27 +18,96 @@ import time
|
|
16 |
|
17 |
API_KEY = "fastgpt-aleL83PzPiul9lZDJQFD4NHYfVIxP7mDIWFCyin2FuMhYgwlRC3NMkV2K5lxc8gF" # 请替换为您的实际 API 密钥
|
18 |
|
19 |
-
API_URL = "https://api.fastgpt.com/v1/chat"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
headers = {
|
23 |
"Authorization": f"Bearer {API_KEY}",
|
24 |
-
"Content-Type": "application/json"
|
25 |
}
|
26 |
-
|
27 |
-
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
}
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import requests
|
3 |
import time
|
4 |
+
import json
|
5 |
+
|
6 |
|
7 |
# 设置 FastGPT API 的基本信息
|
8 |
#API_URL = "https://api.fastgpt.in/api/v1/chat/completions" # 请根据您的部署地址修改
|
|
|
18 |
|
19 |
API_KEY = "fastgpt-aleL83PzPiul9lZDJQFD4NHYfVIxP7mDIWFCyin2FuMhYgwlRC3NMkV2K5lxc8gF" # 请替换为您的实际 API 密钥
|
20 |
|
21 |
+
#API_URL = "https://api.fastgpt.com/v1/chat"
|
22 |
+
|
23 |
+
|
24 |
+
# 替换成你的 FastGPT 的 BaseURL 和 应用特定 API Key
|
25 |
+
BASE_URL = "https://api.fastgpt.in/api/v1"
|
26 |
+
API_KEY = "fastgpt-aleL83PzPiul9lZDJQFD4NHYfVIxP7mDIWFCyin2FuMhYgwlRC3NMkV2K5lxc8gF" # 应用特定 key
|
27 |
+
|
28 |
+
def chat_with_fastgpt(message, history):
|
29 |
+
"""
|
30 |
+
与 FastGPT 进行对话的函数
|
31 |
|
32 |
+
Args:
|
33 |
+
message: 用户输入的消息
|
34 |
+
history: 聊天历史记录
|
35 |
+
|
36 |
+
Returns:
|
37 |
+
生成器的响应内容
|
38 |
+
"""
|
39 |
headers = {
|
40 |
"Authorization": f"Bearer {API_KEY}",
|
41 |
+
"Content-Type": "application/json",
|
42 |
}
|
43 |
+
|
44 |
+
# 构建消息历史
|
45 |
+
messages = []
|
46 |
+
for human, assistant in history:
|
47 |
+
messages.append({"role": "user", "content": human})
|
48 |
+
messages.append({"role": "assistant", "content": assistant})
|
49 |
+
messages.append({"role": "user", "content": message})
|
50 |
+
|
51 |
+
data = {
|
52 |
+
"chatId": "your_chat_id", # 如果需要,替换成你的 chatId
|
53 |
+
"stream": True, # 使用流式输出
|
54 |
+
"detail": False,
|
55 |
+
"messages": messages,
|
56 |
}
|
57 |
+
|
58 |
+
try:
|
59 |
+
response = requests.post(
|
60 |
+
f"{BASE_URL}/chat/completions", headers=headers, json=data, stream=True
|
61 |
+
)
|
62 |
+
response.raise_for_status() # 检查请求是否成功
|
63 |
+
|
64 |
+
# 处理流式输出
|
65 |
+
for chunk in response.iter_content(chunk_size=8192): # 根据实际情况调整chunk_size
|
66 |
+
if chunk:
|
67 |
+
decoded_chunk = chunk.decode('utf-8').replace("data: ", "")
|
68 |
+
|
69 |
+
# 移除末尾的\n
|
70 |
+
if decoded_chunk.endswith('\n'):
|
71 |
+
decoded_chunk = decoded_chunk[:-1]
|
72 |
+
|
73 |
+
# 如果解码后的内容为空,跳过
|
74 |
+
if not decoded_chunk:
|
75 |
+
continue
|
76 |
+
|
77 |
+
# 处理JSON字符串
|
78 |
+
try:
|
79 |
+
# 有时候一个chunk可能包含多个JSON对象,所以需要循环处理
|
80 |
+
json_start_index = 0
|
81 |
+
while json_start_index < len(decoded_chunk):
|
82 |
+
try:
|
83 |
+
json_obj, pos = json.JSONDecoder().raw_decode(decoded_chunk[json_start_index:])
|
84 |
+
json_start_index += pos
|
85 |
+
if json_obj.get("choices"):
|
86 |
+
delta = json_obj["choices"][0].get("delta")
|
87 |
+
if delta and delta.get("content"):
|
88 |
+
yield delta["content"]
|
89 |
+
except json.JSONDecodeError as e:
|
90 |
+
# 找到解码错误的位置,更新起始索引,跳过当前无效的JSON片段
|
91 |
+
json_start_index += e.pos
|
92 |
+
print(f"Skipped invalid JSON part: {e.msg} at position {e.pos}")
|
93 |
+
# 或者如果希望直接跳到下一个字符,可以简单地 json_start_index += 1
|
94 |
+
#json_start_index += 1
|
95 |
+
|
96 |
+
except Exception as e:
|
97 |
+
print(f"Error processing chunk: {e}")
|
98 |
+
|
99 |
+
except requests.exceptions.RequestException as e:
|
100 |
+
print(f"请求错误: {e}")
|
101 |
+
yield "请求 FastGPT API 出错,请检查网络或 API Key。"
|
102 |
+
except Exception as e:
|
103 |
+
print(f"未知错误: {e}")
|
104 |
+
yield "发生未知错误。"
|
105 |
+
|
106 |
+
# Gradio 界面
|
107 |
+
iface = gr.ChatInterface(
|
108 |
+
fn=chat_with_fastgpt,
|
109 |
+
title="FastGPT Chatbot",
|
110 |
+
description="与 FastGPT 支持的聊天机器人对话",
|
111 |
+
)
|
112 |
+
|
113 |
+
iface.launch()
|