config.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. """
  2. 项目配置
  3. 定义项目的运行配置,包括模型、知识管理、日志等。
  4. """
  5. import logging
  6. from agent.core.runner import KnowledgeConfig
  7. # ===== 运行配置 =====
  8. class RunnerConfig:
  9. """运行配置"""
  10. def __init__(self):
  11. # 模型配置
  12. self.model = "sonnet-4.5"
  13. self.temperature = 0.3
  14. self.max_iterations = 1000
  15. # 路径配置
  16. self.skills_dir = "./skills"
  17. self.trace_store_path = ".trace"
  18. # 其他配置
  19. self.debug = True
  20. self.name = "Research Agent"
  21. # 知识管理配置
  22. self.knowledge = KnowledgeConfig(
  23. # 压缩时提取(消息量超阈值触发压缩时,在 Level 1 过滤前用完整 history 反思)
  24. enable_extraction=True,
  25. reflect_prompt="", # 空则使用默认,见 agent/core/prompts/knowledge.py:REFLECT_PROMPT
  26. # agent运行完成后提取(不代表任务完成,agent 可能中途退出等待人工评估)
  27. enable_completion_extraction=True,
  28. completion_reflect_prompt="", # 空则使用默认,见 agent/core/prompts/knowledge.py:COMPLETION_REFLECT_PROMPT
  29. # 知识注入(agent切换当前工作的goal时,自动注入相关知识)
  30. enable_injection=True,
  31. # 默认字段(保存/搜索时自动注入)
  32. owner="user@example.com", # 所有者(空则尝试从 git config user.email 获取,再空则用 agent:{agent_id})
  33. default_tags={"project": "research", "domain": "ai_agent"}, # 默认 tags(会与工具调用参数合并)
  34. default_scopes=["org:cybertogether"], # 默认 scopes
  35. default_search_types=["strategy", "tool"], # 默认搜索类型过滤
  36. default_search_owner="user@example.com" # 默认搜索 owner 过滤(空则不过滤)
  37. )
  38. # 日志配置
  39. class LoggingConfig:
  40. level = "INFO"
  41. console = True
  42. file = None
  43. self.logging = LoggingConfig()
  44. # 自动反思配置(项目层面控制)
  45. class AutoReflectConfig:
  46. enabled = True
  47. on_completion = True
  48. on_failure = True
  49. min_messages = 10
  50. focus_on_failure = "本次任务执行失败了,请重点反思失败的原因、踩坑点以及未来应如何避免。"
  51. self.auto_reflect = AutoReflectConfig()
  52. # 配置实例
  53. RUNNER_CONFIG = RunnerConfig()
  54. # ===== 工具函数 =====
  55. def setup_logging(config):
  56. """配置日志系统"""
  57. level = getattr(logging, config.level.upper(), logging.INFO)
  58. handlers = []
  59. if config.console:
  60. handlers.append(logging.StreamHandler())
  61. if config.file:
  62. handlers.append(logging.FileHandler(config.file, encoding="utf-8"))
  63. logging.basicConfig(
  64. level=level,
  65. format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
  66. handlers=handlers,
  67. force=True
  68. )
  69. # 设置第三方库日志级别
  70. logging.getLogger("httpx").setLevel(logging.WARNING)
  71. logging.getLogger("httpcore").setLevel(logging.WARNING)