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

@@ -10,7 +10,7 @@ ALY_OSS_PREFIX = "HuangHai"
LLM_API_KEY = "sk-44ae895eeb614aa1a9c6460579e322f1"
LLM_BASE_URL = "https://api.deepseek.com"
LLM_MODEL_NAME = "deepseek-chat"
#LLM_MODEL_NAME = "deepseek-reasoner"
# LLM_MODEL_NAME = "deepseek-reasoner"
# 嵌入向量模型
EMBED_MODEL_NAME = "BAAI/bge-m3"
@@ -58,3 +58,6 @@ OBS_PREFIX = "HuangHai"
# 智谱的API KEY【吴缤申请个人版免费】
ZHIPU_API_KEY = "78dc1dfe37e04f29bd4ca9a49858a969.gn7TIZTfzpY35nx9"
# GPTNB的API KEY
GPTNB_API_KEY = "sk-amQHwiEzPIZIB2KuF5A10dC23a0e4b02B48a7a2b6aFa0662"

View File

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