fix(llm): resolve score runtime config from saved profiles

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
2026-06-26 20:34:01 +08:00
parent 754a30ad59
commit 1df4010acc
5 changed files with 117 additions and 6 deletions

View File

@@ -27,13 +27,48 @@ from ragas.metrics.collections import (
from .pipeline import MetricPipeline
def _resolve_openai_client_kwargs(
judge_model: str,
settings: EvaluationSettings,
) -> dict[str, Any]:
"""Return AsyncOpenAI kwargs, preferring a matching LLM Profile over .env settings.
Lookup order:
1. LLM Profile whose model name equals judge_model (exact match)
2. Fall back to EvaluationSettings (.env)
"""
try:
# Lazy import to avoid circular dependency (webapp -> rag_eval is one-way).
from webapp.services.profile_manager import profile_manager
profiles = profile_manager.list_all()
for profile in profiles:
if profile.model == judge_model:
kwargs: dict[str, Any] = {
"api_key": profile.api_key or "sk-placeholder",
"timeout": float(profile.timeout_seconds or 30),
}
if profile.base_url and profile.base_url.strip():
kwargs["base_url"] = profile.base_url.strip()
return kwargs
except Exception: # noqa: BLE001
# If profile lookup fails for any reason, fall through to .env settings.
pass
return settings.openai_client_kwargs
def build_models(
judge_model: str,
embedding_model: str,
settings: EvaluationSettings,
) -> tuple[Any, Any]:
"""Create the LLM and embedding clients required by the selected RAGAS metrics."""
client = AsyncOpenAI(**settings.openai_client_kwargs)
"""Create the LLM and embedding clients required by the selected RAGAS metrics.
Dynamically resolves connection settings from the stored LLM Profiles first
(matched by model name), falling back to .env settings when no profile matches.
"""
client_kwargs = _resolve_openai_client_kwargs(judge_model, settings)
client = AsyncOpenAI(**client_kwargs)
llm = llm_factory(judge_model, client=client)
embeddings = embedding_factory(provider="openai", model=embedding_model, client=client)
return llm, embeddings