Spaces:
Sleeping
Sleeping
import gradio as gr | |
import requests | |
import time | |
import json | |
# 设置 FastGPT API 的基本信息 | |
#API_URL = "https://api.fastgpt.in/api/v1/chat/completions" # 请根据您的部署地址修改 | |
#API_KEY = "fastgpt-aleL83PzPiul9lZDJQFD4NHYfVIxP7mDIWFCyin2FuMhYgwlRC3NMkV2K5lxc8gF" # 请替换为您的实际 API 密钥 | |
# 设置 FastGPT API 的基本信息 | |
#API_URL = "https://api.fastgpt.com/generate" # 请根据您的实际部署地址修改 | |
#API_KEY = "fastgpt-aleL83PzPiul9lZDJQFD4NHYfVIxP7mDIWFCyin2FuMhYgwlRC3NMkV2K5lxc8gF" # 请替换为您的实际 API 密钥 | |
#import gradio as gr | |
#import requests | |
#API_KEY = "fastgpt-aleL83PzPiul9lZDJQFD4NHYfVIxP7mDIWFCyin2FuMhYgwlRC3NMkV2K5lxc8gF" # 请替换为您的实际 API 密钥 | |
#API_URL = "https://api.fastgpt.com/v1/chat" | |
# 替换成你的 FastGPT 的 BaseURL 和 应用特定 API Key | |
#BASE_URL = "https://api.fastgpt.in/api/v1" | |
#API_KEY = "fastgpt-aleL83PzPiul9lZDJQFD4NHYfVIxP7mDIWFCyin2FuMhYgwlRC3NMkV2K5lxc8gF" # 应用特定 key | |
# 替换成你的 FastGPT 的 BaseURL 和 应用特定 API Key | |
BASE_URL = "https://api.fastgpt.in/api/v1" | |
API_KEY = "fastgpt-aleL83PzPiul9lZDJQFD4NHYfVIxP7mDIWFCyin2FuMhYgwlRC3NMkV2K5lxc8gF" # 应用特定 key | |
import ijson # Install with: pip install ijson | |
def chat_with_fastgpt(message, history): | |
""" | |
与 FastGPT 进行对话的函数 | |
Args: | |
message: 用户输入的消息 | |
history: 聊天历史记录 | |
Returns: | |
生成器的响应内容 | |
""" | |
headers = { | |
"Authorization": f"Bearer {API_KEY}", | |
"Content-Type": "application/json", | |
} | |
# 构建消息历史 | |
messages = [] | |
for human, assistant in history: | |
messages.append({"role": "user", "content": human}) | |
messages.append({"role": "assistant", "content": assistant}) | |
messages.append({"role": "user", "content": message}) | |
data = { | |
"chatId": "your_chat_id", # 如果需要,替换成你的 chatId | |
"stream": True, # 仍然使用流式请求 | |
"detail": False, | |
"messages": messages, | |
} | |
try: | |
response = requests.post( | |
f"{BASE_URL}/chat/completions", headers=headers, json=data, stream=True | |
) | |
response.raise_for_status() | |
buffer = "" | |
for chunk in response.iter_content(chunk_size=8192): | |
if chunk: | |
buffer += chunk.decode('utf-8') | |
# 尝试解析 JSON 对象 | |
while True: # Continue processing the buffer until no more complete objects are found | |
try: | |
first_brace_index = buffer.find('{') | |
if first_brace_index == -1: | |
break | |
obj, end_index = json.JSONDecoder().raw_decode(buffer[first_brace_index:]) | |
# 解析成功,处理 JSON 对象 | |
if obj.get("choices"): | |
delta = obj["choices"][0].get("delta") | |
if delta and delta.get("content"): | |
yield delta["content"] | |
buffer = buffer[first_brace_index + end_index:] | |
except json.JSONDecodeError as e: | |
break | |
except requests.exceptions.RequestException as e: | |
print(f"请求错误: {e}") | |
yield "请求 FastGPT API 出错,请检查网络或 API Key。" | |
except Exception as e: | |
print(f"未知错误: {e}") | |
print(f"错误信息: {e}") | |
yield "发生未知错误。" | |
# Gradio 界面 | |
iface = gr.ChatInterface( | |
fn=chat_with_fastgpt, | |
title="FastGPT Chatbot", | |
description="与 FastGPT 支持的聊天机器人对话", | |
) | |
iface.launch() |