leonsimon23 commited on
Commit
d2d3136
·
verified ·
1 Parent(s): b586950

Create app_bak2.py

Browse files
Files changed (1) hide show
  1. app_bak2.py +110 -0
app_bak2.py ADDED
@@ -0,0 +1,110 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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" # 请根据您的部署地址修改
9
+ #API_KEY = "fastgpt-aleL83PzPiul9lZDJQFD4NHYfVIxP7mDIWFCyin2FuMhYgwlRC3NMkV2K5lxc8gF" # 请替换为您的实际 API 密钥
10
+
11
+
12
+ # 设置 FastGPT API 的基本信息
13
+ #API_URL = "https://api.fastgpt.com/generate" # 请根据您的实际部署地址修改
14
+ #API_KEY = "fastgpt-aleL83PzPiul9lZDJQFD4NHYfVIxP7mDIWFCyin2FuMhYgwlRC3NMkV2K5lxc8gF" # 请替换为您的实际 API 密钥
15
+
16
+ #import gradio as gr
17
+ #import requests
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
+ # 替换成你的 FastGPT 的 BaseURL 和 应用特定 API Key
29
+ BASE_URL = "https://api.fastgpt.in/api/v1"
30
+ API_KEY = "fastgpt-aleL83PzPiul9lZDJQFD4NHYfVIxP7mDIWFCyin2FuMhYgwlRC3NMkV2K5lxc8gF" # 应用特定 key
31
+
32
+
33
+ import ijson # Install with: pip install ijson
34
+
35
+
36
+ def chat_with_fastgpt(message, history):
37
+ """
38
+ 与 FastGPT 进行对话的函数
39
+ Args:
40
+ message: 用户输入的消息
41
+ history: 聊天历史记录
42
+ Returns:
43
+ 生成器的响应内容
44
+ """
45
+ headers = {
46
+ "Authorization": f"Bearer {API_KEY}",
47
+ "Content-Type": "application/json",
48
+ }
49
+
50
+ # 构建消息历史
51
+ messages = []
52
+ for human, assistant in history:
53
+ messages.append({"role": "user", "content": human})
54
+ messages.append({"role": "assistant", "content": assistant})
55
+ messages.append({"role": "user", "content": message})
56
+
57
+ data = {
58
+ "chatId": "your_chat_id", # 如果需要,替换成你的 chatId
59
+ "stream": True, # 仍然使用流式请求
60
+ "detail": False,
61
+ "messages": messages,
62
+ }
63
+
64
+ try:
65
+ response = requests.post(
66
+ f"{BASE_URL}/chat/completions", headers=headers, json=data, stream=True
67
+ )
68
+ response.raise_for_status()
69
+
70
+ buffer = ""
71
+ for chunk in response.iter_content(chunk_size=8192):
72
+ if chunk:
73
+ buffer += chunk.decode('utf-8')
74
+
75
+ # 尝试解析 JSON 对象
76
+ while True: # Continue processing the buffer until no more complete objects are found
77
+ try:
78
+ first_brace_index = buffer.find('{')
79
+ if first_brace_index == -1:
80
+ break
81
+
82
+ obj, end_index = json.JSONDecoder().raw_decode(buffer[first_brace_index:])
83
+
84
+ # 解析成功,处理 JSON 对象
85
+ if obj.get("choices"):
86
+ delta = obj["choices"][0].get("delta")
87
+ if delta and delta.get("content"):
88
+ yield delta["content"]
89
+
90
+ buffer = buffer[first_brace_index + end_index:]
91
+
92
+ except json.JSONDecodeError as e:
93
+ break
94
+
95
+ except requests.exceptions.RequestException as e:
96
+ print(f"请求错误: {e}")
97
+ yield "请求 FastGPT API 出错,请检查网络或 API Key。"
98
+ except Exception as e:
99
+ print(f"未知错误: {e}")
100
+ print(f"错误信息: {e}")
101
+ yield "发生未知错误。"
102
+
103
+ # Gradio 界面
104
+ iface = gr.ChatInterface(
105
+ fn=chat_with_fastgpt,
106
+ title="FastGPT Chatbot",
107
+ description="与 FastGPT 支持的聊天机器人对话",
108
+ )
109
+
110
+ iface.launch()