a2fd666203
model/schema 的 debug 字段已在 4ee6de2 起头,本次补完整条链路:
- repo upsert 存客户端环境/性能字段;端点同机拉 pricebot llm_calls 落库 + 算 llm次数/重试
- schema 加 platform_results 透传(admin「卡在哪一步」从 raw_payload 读)
- admin 比价记录查询接口(按 user_id/phone 列表分页 + 详情含 llm_calls)
- 迁移 comparison_debug_fields: 给 comparison_record 加环境/性能/llm_calls 列(全 nullable)
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
39 lines
1.7 KiB
Python
39 lines
1.7 KiB
Python
"""按 trace_id 同机拉 pricebot 的本次比价 LLM 调用明细(供 admin 比价记录 debug)。
|
|
|
|
pricebot 在 chat() 收口处把每次 LLM input/output 落盘,暴露内部接口
|
|
GET /api/internal/llm_calls/{trace_id}(X-Internal-Secret 校验,与本服务 INTERNAL_API_SECRET 同值)。
|
|
app-server 收到比价记录上报后同机拉一次,落 comparison_record.llm_calls。
|
|
|
|
best-effort:拉不到(pricebot 未部署该版本 / 旧 trace 无明细 / 网络)一律返 [],绝不阻断
|
|
比价记录上报。多实例下用 pick_pricebot(trace_id) 选对实例(同 trace 落同进程,明细在
|
|
该进程磁盘),与比价透传同一致性 hash 口径。
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
|
|
import httpx
|
|
|
|
from app.core.config import settings
|
|
from app.core.pricebot_router import pick_pricebot
|
|
|
|
logger = logging.getLogger("shagua.pricebot_llm")
|
|
|
|
|
|
def fetch_llm_calls(trace_id: str) -> list[dict]:
|
|
"""返回该次比价的 LLM 调用明细列表(每条 {scene,model,input_messages,output,usage,latency_ms,error});
|
|
未配密钥 / 无 trace_id / 拉取失败 → []。"""
|
|
secret = settings.INTERNAL_API_SECRET
|
|
if not secret or not trace_id:
|
|
return []
|
|
base = pick_pricebot(trace_id).rstrip("/")
|
|
url = f"{base}/api/internal/llm_calls/{trace_id}"
|
|
try:
|
|
resp = httpx.get(url, headers={"X-Internal-Secret": secret}, timeout=5.0)
|
|
if resp.status_code == 200:
|
|
return resp.json().get("calls", []) or []
|
|
logger.warning("fetch_llm_calls trace=%s status=%s", trace_id, resp.status_code)
|
|
except Exception as e: # noqa: BLE001 — best-effort,任何异常都不该影响上报
|
|
logger.warning("fetch_llm_calls trace=%s failed: %s", trace_id, e)
|
|
return []
|