代码优化
This commit is contained in:
@@ -13,10 +13,9 @@ from typing import AsyncGenerator, Optional
|
||||
from zai import ZhipuAiClient
|
||||
|
||||
# API 配置
|
||||
API_KEY = os.getenv(
|
||||
"GLM_API_KEY",
|
||||
"5915240ea48d4e93b454bc2412d1cc54.e054ej4pPqi9G6rc"
|
||||
)
|
||||
API_KEY = os.getenv("GLM_API_KEY")
|
||||
if not API_KEY:
|
||||
raise RuntimeError("未设置 GLM_API_KEY 环境变量,请在 .env 中配置")
|
||||
MODEL = "glm-4.6v-flash" # 升级到 glm-4.6v-flash (支持视觉)
|
||||
|
||||
# 星期映射
|
||||
@@ -178,14 +177,35 @@ async def chat_stream(user_message: str, image_base64: Optional[str] = None) ->
|
||||
try:
|
||||
# 流式调用
|
||||
# Day 22: 升级到 glm-4.6v-flash
|
||||
# 【修正】根据官方文档,thinking 参数也是必须的
|
||||
response = await asyncio.to_thread(
|
||||
client.chat.completions.create,
|
||||
model=MODEL,
|
||||
messages=messages,
|
||||
thinking={"type": "disabled"},
|
||||
stream=True,
|
||||
)
|
||||
max_retries = 3
|
||||
retry_delay = 1
|
||||
|
||||
response = None
|
||||
for attempt in range(max_retries):
|
||||
try:
|
||||
# 【修正】根据官方文档,thinking 参数也是必须的
|
||||
response = await asyncio.to_thread(
|
||||
client.chat.completions.create,
|
||||
model=MODEL,
|
||||
messages=messages,
|
||||
thinking={"type": "disabled"},
|
||||
stream=True,
|
||||
)
|
||||
break # 成功则跳出循环
|
||||
except Exception as e:
|
||||
error_str = str(e)
|
||||
if attempt < max_retries - 1:
|
||||
if "429" in error_str or "1305" in error_str or "请求过多" in error_str:
|
||||
print(f"[GLM] (流式) 速率限制,{retry_delay}秒后重试... ({attempt + 1}/{max_retries})")
|
||||
await asyncio.sleep(retry_delay)
|
||||
retry_delay *= 2
|
||||
continue
|
||||
# 其他网络错误也可以重试
|
||||
print(f"[GLM] (流式) 连接错误: {e},重试... ({attempt + 1}/{max_retries})")
|
||||
await asyncio.sleep(retry_delay)
|
||||
continue
|
||||
else:
|
||||
raise e # 最后一次尝试失败,抛出异常
|
||||
|
||||
for chunk in response:
|
||||
if chunk.choices[0].delta.content:
|
||||
|
||||
Reference in New Issue
Block a user