-
Notifications
You must be signed in to change notification settings - Fork 79
gemini api不支持流式输出 #21
Copy link
Copy link
Open
Description
我成功部署了一个worker,在使用非流式输出(也就是您提供的脚本)时,能够接收到gemini返回的补全token。
当我在脚本里增加了流式输出的功能的时候,却不能接收到gemini返回的补全token,预期应该是能够接收到gemini返回的补全token。
代码如下:
from openai import OpenAI
import sys
def validate_api_key(api_key):
if not api_key or len(api_key) < 10:
print("错误:API密钥无效")
return False
return True
def validate_base_url(base_url):
if not base_url.startswith(("http://", "https://")):
print("错误:基础URL格式无效")
return False
return True
def main():
api_key = "省略"
model = "gemini-1.5-flash"
BASE_URL = "https://域名/v2/gemini"
# 验证API密钥和基础URL
if not validate_api_key(api_key) or not validate_base_url(BASE_URL):
sys.exit(1)
try:
client = OpenAI(base_url=BASE_URL, api_key=api_key)
# 使用流式输出
stream = client.chat.completions.create(
model=model,
messages=[
{"role": "system", "content": "You are a helpful assistant。"},
{"role": "user", "content": "介绍一下你自己"}
],
temperature=0.7,
top_p=1,
max_tokens=1024,
top_logprobs=32,
stream=True # 启用流式输出
)
# 处理流式响应
for chunk in stream:
if chunk.choices[0].delta.content is not None:
print(chunk.choices[0].delta.content, end="", flush=True)
print() # 最后打印换行
except Exception as e:
print(f"发生错误:{str(e)}")
sys.exit(1)
if name == "main":
main()
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels