This commit is contained in:
2025-08-15 16:11:35 +08:00
parent 4a72e41405
commit 04913b776d
3 changed files with 20 additions and 25 deletions

View File

@@ -58,3 +58,6 @@ OBS_PREFIX = "HuangHai"
# 智谱的API KEY【吴缤申请个人版免费】 # 智谱的API KEY【吴缤申请个人版免费】
ZHIPU_API_KEY = "78dc1dfe37e04f29bd4ca9a49858a969.gn7TIZTfzpY35nx9" ZHIPU_API_KEY = "78dc1dfe37e04f29bd4ca9a49858a969.gn7TIZTfzpY35nx9"
# GPTNB的API KEY
GPTNB_API_KEY = "sk-amQHwiEzPIZIB2KuF5A10dC23a0e4b02B48a7a2b6aFa0662"

View File

@@ -1,15 +1,16 @@
import requests
import json import json
import sys
import requests
from Config.Config import GPTNB_API_KEY
# API配置 # API配置
API_URL = "https://goapi.gptnb.ai/v1/chat/completions" API_URL = "https://goapi.gptnb.ai/v1/chat/completions"
API_KEY = "sk-amQHwiEzPIZIB2KuF5A10dC23a0e4b02B48a7a2b6aFa0662"
# 请求头 # 请求头
headers = { headers = {
"Content-Type": "application/json", "Content-Type": "application/json",
"Authorization": f"Bearer {API_KEY}" "Authorization": f"Bearer {GPTNB_API_KEY}"
} }
# 请求体 - 添加stream: true参数启用流式响应 # 请求体 - 添加stream: true参数启用流式响应
@@ -40,7 +41,6 @@ try:
if chunk: if chunk:
# 解码chunk # 解码chunk
chunk_data = chunk.decode('utf-8', errors='replace') chunk_data = chunk.decode('utf-8', errors='replace')
print(f"[调试] 原始chunk: {chunk_data}", file=sys.stderr)
# 处理可能的多部分响应 # 处理可能的多部分响应
for line in chunk_data.splitlines(): for line in chunk_data.splitlines():
@@ -49,15 +49,13 @@ try:
continue continue
# 检查是否结束 # 检查是否结束
if line == '[DONE]': if line == 'data: [DONE]':
print("\n[调试] 接收到结束信号", file=sys.stderr)
break break
# 去除可能的前缀 # 去除可能的前缀
if line.startswith('data: '): if line.startswith('data: '):
line = line[6:] line = line[6:]
try:
# 解析JSON # 解析JSON
data = json.loads(line) data = json.loads(line)
# 提取文本内容 # 提取文本内容
@@ -67,12 +65,6 @@ try:
if content: if content:
# 实时输出内容,不换行 # 实时输出内容,不换行
print(content, end='', flush=True) print(content, end='', flush=True)
except json.JSONDecodeError as e:
print(f"[调试] JSON解析错误: {e}, 内容: {line}", file=sys.stderr)
except Exception as e:
print(f"[调试] 处理错误: {e}", file=sys.stderr)
print("\n\n流式响应结束")
except requests.exceptions.RequestException as e: except requests.exceptions.RequestException as e:
print(f"请求发生错误: {e}") print(f"请求发生错误: {e}")