fix: Coze v3 API异步轮询 - GET方法+query参数,等待answer消息
CI/CD Pipeline / Check if frontend-only change (pull_request) Successful in 10s
CI/CD Pipeline / Frontend Lint (pull_request) Successful in 47s
CI/CD Pipeline / Validate Code Quality And Tests (pull_request) Failing after 57s
CI/CD Pipeline / Integration Tests (pull_request) Successful in 1m30s
CI/CD Pipeline / Unit Tests (pull_request) Successful in 2m35s
AI Code Review / AI Code Review (pull_request) Successful in 3m55s
CI Build & Deploy Pipeline / Production Browser E2E (pull_request) Failing after 1478h44m44s
CI Build & Deploy Pipeline / Staging E2E Tests (pull_request) Failing after 1478h44m48s
CI Build & Deploy Pipeline / Staging API Integration Tests (pull_request) Failing after 1478h44m49s
CI Build & Deploy Pipeline / Build Production Web Image (pull_request) Failing after 1478h44m50s
CI Build & Deploy Pipeline / Build Production API Image (pull_request) Failing after 1478h44m50s
CI Build & Deploy Pipeline / Deploy Staging (Watchtower auto-deploy) (pull_request) Failing after 1478h44m50s
CI Build & Deploy Pipeline / Build Staging Worker Image (pull_request) Failing after 1478h44m51s
CI Build & Deploy Pipeline / Build Staging Web Image (pull_request) Failing after 1478h44m51s
CI Build & Deploy Pipeline / Build Staging API Image (pull_request) Failing after 1478h44m52s
CI Build & Deploy Pipeline / Deploy Production (pull_request) Failing after 1479h16m41s
CI Build & Deploy Pipeline / Build Production Worker Image (pull_request) Failing after 1479h16m45s
CI/CD Pipeline / Check if frontend-only change (pull_request) Successful in 10s
CI/CD Pipeline / Frontend Lint (pull_request) Successful in 47s
CI/CD Pipeline / Validate Code Quality And Tests (pull_request) Failing after 57s
CI/CD Pipeline / Integration Tests (pull_request) Successful in 1m30s
CI/CD Pipeline / Unit Tests (pull_request) Successful in 2m35s
AI Code Review / AI Code Review (pull_request) Successful in 3m55s
CI Build & Deploy Pipeline / Production Browser E2E (pull_request) Failing after 1478h44m44s
CI Build & Deploy Pipeline / Staging E2E Tests (pull_request) Failing after 1478h44m48s
CI Build & Deploy Pipeline / Staging API Integration Tests (pull_request) Failing after 1478h44m49s
CI Build & Deploy Pipeline / Build Production Web Image (pull_request) Failing after 1478h44m50s
CI Build & Deploy Pipeline / Build Production API Image (pull_request) Failing after 1478h44m50s
CI Build & Deploy Pipeline / Deploy Staging (Watchtower auto-deploy) (pull_request) Failing after 1478h44m50s
CI Build & Deploy Pipeline / Build Staging Worker Image (pull_request) Failing after 1478h44m51s
CI Build & Deploy Pipeline / Build Staging Web Image (pull_request) Failing after 1478h44m51s
CI Build & Deploy Pipeline / Build Staging API Image (pull_request) Failing after 1478h44m52s
CI Build & Deploy Pipeline / Deploy Production (pull_request) Failing after 1479h16m41s
CI Build & Deploy Pipeline / Build Production Worker Image (pull_request) Failing after 1479h16m45s
This commit is contained in:
@@ -300,7 +300,9 @@ def call_llm_coze(
|
||||
llm_model: str,
|
||||
coze_bot_id: str,
|
||||
) -> Optional[str]:
|
||||
"""扣子(Coze)原生 Bot API 调用"""
|
||||
"""扣子(Coze)原生 Bot API 调用(支持异步轮询)"""
|
||||
import time
|
||||
|
||||
base_url = llm_base_url.rstrip("/") + "/"
|
||||
api_url = f"{base_url}v3/chat"
|
||||
|
||||
@@ -342,21 +344,68 @@ def call_llm_coze(
|
||||
continue
|
||||
|
||||
data = resp.json()
|
||||
chat_data = data.get("data", {})
|
||||
chat_id = chat_data.get("id", "")
|
||||
conversation_id = chat_data.get("conversation_id", "")
|
||||
status = chat_data.get("status", "")
|
||||
|
||||
# 解析 Coze 返回格式,兼容多种可能的返回结构
|
||||
# Coze v3 API 异步:先返回 in_progress,需要轮询
|
||||
if status == "in_progress" and conversation_id and chat_id:
|
||||
logger.info(f"Coze 异步处理中,开始轮询... (chat_id={chat_id[:12]}...)")
|
||||
# 轮询 message 列表接口(GET + query参数),最多等 LLM_TIMEOUT 秒
|
||||
poll_url = f"{base_url}v3/chat/message/list"
|
||||
poll_start = time.time()
|
||||
poll_interval = 3 # 每3秒轮询一次
|
||||
|
||||
while time.time() - poll_start < LLM_TIMEOUT:
|
||||
time.sleep(poll_interval)
|
||||
poll_params = {
|
||||
"chat_id": chat_id,
|
||||
"conversation_id": conversation_id,
|
||||
}
|
||||
poll_resp = requests.get(
|
||||
poll_url,
|
||||
headers=headers,
|
||||
params=poll_params,
|
||||
timeout=GITEA_TIMEOUT,
|
||||
)
|
||||
if poll_resp.status_code != 200:
|
||||
logger.debug(f"轮询返回 HTTP {poll_resp.status_code}: {poll_resp.text[:100]}")
|
||||
continue
|
||||
|
||||
poll_data = poll_resp.json()
|
||||
if poll_data.get("code", 0) != 0:
|
||||
logger.debug(f"轮询返回错误: {poll_data.get('msg', '')}")
|
||||
continue
|
||||
|
||||
messages = poll_data.get("data", []) or []
|
||||
|
||||
# 找assistant的answer消息
|
||||
content = None
|
||||
for msg in messages:
|
||||
if msg.get("role") == "assistant" and msg.get("type") == "answer":
|
||||
content = msg.get("content", "")
|
||||
break
|
||||
|
||||
if content and content.strip():
|
||||
logger.info(f"Coze 审查完成,结果长度: {len(content)} 字符")
|
||||
return content
|
||||
|
||||
logger.warning(f"Coze 轮询超时 ({LLM_TIMEOUT}s),未拿到结果")
|
||||
last_error = "poll timeout"
|
||||
continue
|
||||
|
||||
# 同步返回的情况(兼容)
|
||||
content = None
|
||||
# 方式1: data.messages 数组
|
||||
messages = data.get("data", {}).get("messages", []) or data.get("messages", [])
|
||||
messages = chat_data.get("messages", []) or data.get("messages", [])
|
||||
for msg in messages:
|
||||
if msg.get("role") == "assistant" and msg.get("type") == "answer":
|
||||
content = msg.get("content", "")
|
||||
break
|
||||
|
||||
# 方式2: 直接 content 字段
|
||||
if not content:
|
||||
content = data.get("data", {}).get("content") or data.get("content")
|
||||
content = chat_data.get("content") or data.get("content")
|
||||
|
||||
# 方式3: choices 格式(兼容)
|
||||
if not content:
|
||||
choices = data.get("choices", [])
|
||||
if choices:
|
||||
|
||||
Reference in New Issue
Block a user