debug: add _call_llm logging for api_key/base/model tracing
This commit is contained in:
parent
ac2ff432ed
commit
a45b4503eb
@ -145,6 +145,7 @@ async def _call_llm(model_info, messages, temperature):
|
|||||||
api_base = model_info.api_base.rstrip('/')
|
api_base = model_info.api_base.rstrip('/')
|
||||||
api_key = model_info.api_key or ''
|
api_key = model_info.api_key or ''
|
||||||
model_id = model_info.model_id
|
model_id = model_info.model_id
|
||||||
|
debug(f'_call_llm: base={api_base} model={model_id} key_len={len(api_key)} key_prefix={api_key[:10]}')
|
||||||
|
|
||||||
headers = {
|
headers = {
|
||||||
"Authorization": f"Bearer {api_key}",
|
"Authorization": f"Bearer {api_key}",
|
||||||
@ -158,13 +159,16 @@ async def _call_llm(model_info, messages, temperature):
|
|||||||
|
|
||||||
timeout = aiohttp.ClientTimeout(total=120)
|
timeout = aiohttp.ClientTimeout(total=120)
|
||||||
async with aiohttp.ClientSession(timeout=timeout) as session:
|
async with aiohttp.ClientSession(timeout=timeout) as session:
|
||||||
async with session.post(f"{api_base}/chat/completions", headers=headers, json=payload) as resp:
|
url = f"{api_base}/chat/completions"
|
||||||
|
debug(f'_call_llm: POST {url}')
|
||||||
|
async with session.post(url, headers=headers, json=payload) as resp:
|
||||||
if resp.status != 200:
|
if resp.status != 200:
|
||||||
text = await resp.text()
|
text = await resp.text()
|
||||||
|
debug(f'_call_llm: FAIL status={resp.status} body={text[:200]}')
|
||||||
raise ValueError(f"LLM API error {resp.status}: {text[:300]}")
|
raise ValueError(f"LLM API error {resp.status}: {text[:300]}")
|
||||||
data = await resp.json()
|
data = await resp.json()
|
||||||
content = data["choices"][0]["message"]["content"]
|
content = data["choices"][0]["message"]["content"]
|
||||||
# Truncate overly long responses
|
debug(f'_call_llm: OK reply_len={len(content)}')
|
||||||
if len(content) > 8000:
|
if len(content) > 8000:
|
||||||
content = content[:8000] + "\n\n...(内容过长已截断)"
|
content = content[:8000] + "\n\n...(内容过长已截断)"
|
||||||
return content
|
return content
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user