dark 0cd43c37a7 1、api_key可以为空
2、环境命令补充
2026-06-02 14:06:22 +08:00

39 lines
1.1 KiB
Python

"""供 CLI 和外部嵌入使用的 LLM client 工厂。"""
from __future__ import annotations
import os
from .base import LlmClient
from .openai_compatible import OpenAICompatibleLlmClient
from .rule_based import RuleBasedLlmClient
def build_llm_client(
*,
base_url: str | None = None,
api_key: str | None = None,
model: str | None = None,
) -> LlmClient:
"""根据显式参数或环境变量构造 LLM client。"""
actual_base_url = base_url or os.getenv("PAM_LLM_BASE_URL", "")
actual_api_key = api_key or os.getenv("PAM_LLM_API_KEY", "")
actual_model = model or os.getenv("PAM_LLM_MODEL", "")
if not actual_base_url and not actual_api_key and not actual_model:
return RuleBasedLlmClient()
missing = []
if not actual_base_url:
missing.append("base_url")
if not actual_model:
missing.append("model")
if missing:
raise ValueError(f"LLM 配置不完整,缺少: {', '.join(missing)}")
return OpenAICompatibleLlmClient(
base_url=actual_base_url,
api_key=actual_api_key,
model=actual_model,
)