llm_service.py 801 B

123456789101112131415161718192021222324252627282930313233343536
  1. """LLM服务模块"""
  2. from hello_agents import HelloAgentsLLM
  3. from ..config import get_settings
  4. # 全局LLM实例
  5. _llm_instance = None
  6. def get_llm() -> HelloAgentsLLM:
  7. """
  8. 获取LLM实例(单例模式)
  9. Returns:
  10. HelloAgentsLLM实例
  11. """
  12. global _llm_instance
  13. if _llm_instance is None:
  14. settings = get_settings()
  15. # HelloAgentsLLM会自动从环境变量读取配置
  16. # 包括OPENAI_API_KEY, OPENAI_BASE_URL, OPENAI_MODEL等
  17. _llm_instance = HelloAgentsLLM()
  18. print(f"✅ LLM服务初始化成功")
  19. print(f" 模型: {_llm_instance.model}")
  20. return _llm_instance
  21. def reset_llm():
  22. """重置LLM实例(用于测试或重新配置)"""
  23. global _llm_instance
  24. _llm_instance = None