Files

43 lines
1.2 KiB
Python

"""LLM answer generation with the客服 Agent fallback chain."""
from __future__ import annotations
import logging
from inspect import isawaitable
logger = logging.getLogger("rag.generation")
async def _config(config_getter, key: str, default=None):
value = config_getter(key, default)
if isawaitable(value):
value = await value
return value
async def generate_answer(
messages: list[dict],
*,
llm_client,
config_getter,
primary_model: str | None = None,
) -> str:
fallback_model = await _config(
config_getter, "agent.customer.llm.fallback_model", ""
)
template = await _config(
config_getter, "agent.customer.template.system_busy", None
)
models = [primary_model] if primary_model else [None]
if fallback_model and fallback_model not in models:
models.append(fallback_model)
for model in models:
try:
kwargs = {} if model is None else {"model": model}
return await llm_client.chat(messages, **kwargs)
except Exception:
logger.exception("LLM generation failed for model=%s", model or "default")
if not template:
raise RuntimeError("system busy template is not configured")
return template