41 lines
946 B
Python
41 lines
946 B
Python
"""投顾 Agent 对共享 LLM 客户端的安全调用封装。"""
|
|||
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
import inspect
|
||
|
|
|
||
|
|
from agent.advisor_agent.fallback import call_with_fallback
|
||
|
|
|
||
|
|
|
||
|
|
async def generate_text(
|
||
|
|
llm_client,
|
||
|
|
*,
|
||
|
|
system_prompt: str,
|
||
|
|
user_prompt: str,
|
||
|
|
fallback,
|
||
|
|
timeout: float = 5.0,
|
||
|
|
) -> str:
|
||
|
|
"""调用共享 LLM,超时或异常时返回本地安全兜底文本。"""
|
||
|
|
|
||
|
|
async def primary():
|
||
|
|
return await llm_client.chat(
|
||
|
|
[
|
||
|
|
{"role": "system", "content": system_prompt},
|
||
|
|
{"role": "user", "content": user_prompt},
|
||
|
|
]
|
||
|
|
)
|
||
|
|
|
||
|
|
async def secondary():
|
||
|
|
value = fallback()
|
||
|
|
return await value if inspect.isawaitable(value) else value
|
||
|
|
|
||
|
|
result = await call_with_fallback(
|
||
|
|
primary,
|
||
|
|
secondary,
|
||
|
|
timeout=timeout,
|
||
|
|
degraded_code=50001,
|
||
|
|
)
|
||
|
|
return str(result.value)
|
||
|
|
|
||
|
|
|
||
|
|
__all__ = ["generate_text"]
|