File size: 3,857 Bytes
d2d3136
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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()