Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -27,7 +27,8 @@ class FastGPTChat:
|
|
| 27 |
data = {
|
| 28 |
"chatId": chat_id,
|
| 29 |
"stream": False,
|
| 30 |
-
"detail": True,
|
|
|
|
| 31 |
"messages": [
|
| 32 |
{
|
| 33 |
"role": "user",
|
|
@@ -47,7 +48,7 @@ class FastGPTChat:
|
|
| 47 |
f"{self.base_url}/v1/chat/completions",
|
| 48 |
headers=self.headers,
|
| 49 |
json=data,
|
| 50 |
-
timeout=30
|
| 51 |
)
|
| 52 |
|
| 53 |
# 打印响应信息
|
|
@@ -67,8 +68,30 @@ class FastGPTChat:
|
|
| 67 |
response_data = response.json()
|
| 68 |
logger.debug(f"Parsed Response: {json.dumps(response_data, indent=2)}")
|
| 69 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
if "choices" in response_data and len(response_data["choices"]) > 0:
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
else:
|
| 73 |
ai_message = "No response generated from the API"
|
| 74 |
logger.warning("No choices in response data")
|
|
@@ -94,7 +117,7 @@ def create_chat_interface(api_key):
|
|
| 94 |
|
| 95 |
# 创建Gradio界面
|
| 96 |
with gr.Blocks(title="FastGPT Chat") as interface:
|
| 97 |
-
gr.Markdown("# FastGPT Chat Interface")
|
| 98 |
|
| 99 |
with gr.Row():
|
| 100 |
api_key_input = gr.Textbox(
|
|
@@ -144,4 +167,4 @@ def create_chat_interface(api_key):
|
|
| 144 |
if __name__ == "__main__":
|
| 145 |
API_KEY = "fastgpt-aleL83PzPiul9lZDJQFD4NHYfVIxP7mDIWFCyin2FuMhYgwlRC3NMkV2K5lxc8gF" # 替换为你的API密钥
|
| 146 |
demo = create_chat_interface(API_KEY)
|
| 147 |
-
demo.launch(debug=True)
|
|
|
|
| 27 |
data = {
|
| 28 |
"chatId": chat_id,
|
| 29 |
"stream": False,
|
| 30 |
+
"detail": True,
|
| 31 |
+
"model": "gpt-4", # 指定使用 gpt-4 模型
|
| 32 |
"messages": [
|
| 33 |
{
|
| 34 |
"role": "user",
|
|
|
|
| 48 |
f"{self.base_url}/v1/chat/completions",
|
| 49 |
headers=self.headers,
|
| 50 |
json=data,
|
| 51 |
+
timeout=30
|
| 52 |
)
|
| 53 |
|
| 54 |
# 打印响应信息
|
|
|
|
| 68 |
response_data = response.json()
|
| 69 |
logger.debug(f"Parsed Response: {json.dumps(response_data, indent=2)}")
|
| 70 |
|
| 71 |
+
# 检查是否有错误信息
|
| 72 |
+
if "responseData" in response_data:
|
| 73 |
+
for item in response_data["responseData"]:
|
| 74 |
+
if "error" in item:
|
| 75 |
+
error_msg = item["error"].get("message", "Unknown error")
|
| 76 |
+
logger.error(f"API Error: {error_msg}")
|
| 77 |
+
chat_history.append((message, f"Error: {error_msg}"))
|
| 78 |
+
return "", chat_history
|
| 79 |
+
|
| 80 |
if "choices" in response_data and len(response_data["choices"]) > 0:
|
| 81 |
+
content = response_data["choices"][0]["message"]["content"]
|
| 82 |
+
# 如果content是列表,提取文本内容
|
| 83 |
+
if isinstance(content, list):
|
| 84 |
+
text_contents = []
|
| 85 |
+
for item in content:
|
| 86 |
+
if isinstance(item, dict) and "text" in item:
|
| 87 |
+
text_contents.append(item["text"].get("content", ""))
|
| 88 |
+
ai_message = " ".join(text_contents)
|
| 89 |
+
else:
|
| 90 |
+
ai_message = content
|
| 91 |
+
|
| 92 |
+
if not ai_message:
|
| 93 |
+
ai_message = "No response generated from the API"
|
| 94 |
+
logger.warning("Empty response content")
|
| 95 |
else:
|
| 96 |
ai_message = "No response generated from the API"
|
| 97 |
logger.warning("No choices in response data")
|
|
|
|
| 117 |
|
| 118 |
# 创建Gradio界面
|
| 119 |
with gr.Blocks(title="FastGPT Chat") as interface:
|
| 120 |
+
gr.Markdown("# FastGPT Chat Interface (Using GPT-4)")
|
| 121 |
|
| 122 |
with gr.Row():
|
| 123 |
api_key_input = gr.Textbox(
|
|
|
|
| 167 |
if __name__ == "__main__":
|
| 168 |
API_KEY = "fastgpt-aleL83PzPiul9lZDJQFD4NHYfVIxP7mDIWFCyin2FuMhYgwlRC3NMkV2K5lxc8gF" # 替换为你的API密钥
|
| 169 |
demo = create_chat_interface(API_KEY)
|
| 170 |
+
demo.launch(debug=True)
|