Files
dsProject/dsLightRag/Test/TestGoogleGemini.py

71 lines
2.0 KiB
Python
Raw Normal View History

2025-08-15 15:56:49 +08:00
import json
2025-08-15 16:11:35 +08:00
import requests
from Config.Config import GPTNB_API_KEY
2025-08-15 15:56:49 +08:00
# API配置
API_URL = "https://goapi.gptnb.ai/v1/chat/completions"
# 请求头
headers = {
"Content-Type": "application/json",
2025-08-15 16:11:35 +08:00
"Authorization": f"Bearer {GPTNB_API_KEY}"
2025-08-15 15:56:49 +08:00
}
2025-08-15 16:08:21 +08:00
# 请求体 - 添加stream: true参数启用流式响应
2025-08-15 15:56:49 +08:00
payload = {
"model": "gemini-2.5-pro",
2025-08-15 16:08:21 +08:00
"messages": [{
"role": "user",
"content": "请详细介绍一下你自己,分成几个段落来说明"
}],
"temperature": 0.7,
"stream": True # 启用流式响应
2025-08-15 15:56:49 +08:00
}
try:
2025-08-15 16:08:21 +08:00
# 发送POST请求设置stream=True
response = requests.post(
API_URL,
headers=headers,
data=json.dumps(payload),
stream=True,
timeout=30 # 设置超时时间
)
2025-08-15 15:56:49 +08:00
response.raise_for_status() # 检查请求是否成功
2025-08-15 16:08:21 +08:00
print("流式响应内容:\n")
# 逐块处理响应
for chunk in response.iter_content(chunk_size=None):
if chunk:
# 解码chunk
chunk_data = chunk.decode('utf-8', errors='replace')
# 处理可能的多部分响应
for line in chunk_data.splitlines():
line = line.strip()
if not line:
continue
# 检查是否结束
2025-08-15 16:11:35 +08:00
if line == 'data: [DONE]':
2025-08-15 16:08:21 +08:00
break
# 去除可能的前缀
if line.startswith('data: '):
line = line[6:]
2025-08-15 16:11:35 +08:00
# 解析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)
2025-08-15 15:56:49 +08:00
except requests.exceptions.RequestException as e:
2025-08-15 16:11:35 +08:00
print(f"请求发生错误: {e}")