config.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. """
  2. LLM 配置 - 英语句子扩写智能体
  3. """
  4. import os
  5. import logging
  6. from dotenv import load_dotenv
  7. from hello_agents import HelloAgentsLLM
  8. load_dotenv()
  9. logger = logging.getLogger(__name__)
  10. def tool_listener(call_info):
  11. logger.info(f"Agent: {call_info['agent_name']}")
  12. logger.info(f"Tool: {call_info['tool_name']}")
  13. logger.info(f"Parameters: {call_info['parsed_parameters']}")
  14. logger.info(f"Result: {call_info['result']}")
  15. # LLM 配置
  16. class LLMConfig:
  17. """LLM 配置类"""
  18. # 从环境变量读取配置
  19. API_KEY = os.getenv("LLM_API_KEY", "")
  20. MODEL_ID = os.getenv("LLM_MODEL_ID", "")
  21. BASE_URL = os.getenv("LLM_BASE_URL", "")
  22. @classmethod
  23. def create_llm(cls) -> HelloAgentsLLM:
  24. """
  25. 创建 LLM 实例
  26. Returns:
  27. HelloAgentsLLM: 配置好的 LLM 实例
  28. """
  29. return HelloAgentsLLM(
  30. api_key=cls.API_KEY,
  31. model_id=cls.MODEL_ID,
  32. base_url=cls.BASE_URL
  33. )
  34. # 全局 LLM 实例(懒加载)
  35. _llm_instance = None
  36. def get_llm() -> HelloAgentsLLM:
  37. """
  38. 获取全局 LLM 实例(单例模式)
  39. Returns:
  40. HelloAgentsLLM: LLM 实例
  41. """
  42. global _llm_instance
  43. if _llm_instance is None:
  44. _llm_instance = LLMConfig.create_llm()
  45. return _llm_instance
  46. def reset_llm():
  47. """重置 LLM 实例(用于测试或配置变更)"""
  48. global _llm_instance
  49. _llm_instance = None