llm_service.py 858 B

12345678910111213141516171819202122232425262728293031323334353637
  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.provider}")
  20. print(f" 模型: {_llm_instance.model}")
  21. return _llm_instance
  22. def reset_llm():
  23. """重置LLM实例(用于测试或重新配置)"""
  24. global _llm_instance
  25. _llm_instance = None