llm_service.py 756 B

123456789101112131415161718192021222324252627
  1. """LLM 进程内单例(多智能体共享同一模型客户端,避免重复初始化)。"""
  2. from hello_agents import HelloAgentsLLM
  3. from ..utils.logger import get_logger
  4. logger = get_logger("app.llm")
  5. _llm_instance: HelloAgentsLLM | None = None
  6. def get_llm() -> HelloAgentsLLM:
  7. """HelloAgentsLLM 自动读取 LLM_API_KEY / LLM_BASE_URL / LLM_MODEL_ID。"""
  8. global _llm_instance
  9. if _llm_instance is None:
  10. _llm_instance = HelloAgentsLLM()
  11. logger.info(
  12. "LLM 初始化: provider=%s model=%s",
  13. getattr(_llm_instance, "provider", "?"),
  14. getattr(_llm_instance, "model", "?"),
  15. )
  16. return _llm_instance
  17. def reset_llm() -> None:
  18. global _llm_instance
  19. _llm_instance = None