config.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. """
  2. agent67.core.config — 配置与常量
  3. """
  4. from __future__ import annotations
  5. import os
  6. import shutil
  7. BANNER = """
  8. ╔══════════════════════════════════════════════════════════╗
  9. ║ 🐑 lambda — 你的个人助理 ║
  10. ║ powered by lambdagent ║
  11. ╠══════════════════════════════════════════════════════════╣
  12. ║ ║
  13. ║ 我可以帮你: ║
  14. ║ 💻 执行终端命令 📁 管理文件 ║
  15. ║ 🌐 控制浏览器 🚀 启动/切换应用 ║
  16. ║ 📊 查询系统信息 📸 截取屏幕 ║
  17. ║ ║
  18. ║ 输入 'exit' 退出 | 'trace' 查看执行追踪 ║
  19. ╚══════════════════════════════════════════════════════════╝
  20. """
  21. WAKE_WORD = "lambda"
  22. # 后端类型常量
  23. BACKEND_CLAUDE_CODE = "claude_code"
  24. BACKEND_OLLAMA = "ollama"
  25. BACKEND_API = "api"
  26. def detect_backend() -> tuple[str, bool, str]:
  27. """
  28. 检测可用的 LLM 后端。
  29. 返回: (model_name, use_api, backend_type)
  30. - backend_type="ollama" → 本地 Ollama (Qwen2.5, GLM-4, Llama3 等)
  31. - backend_type="claude_code" → Claude Code CLI (Max Plan, 无需 API Key)
  32. - backend_type="api" → 云端 API (Anthropic/DashScope/OpenAI)
  33. 优先级: Ollama (本地) > Claude Code CLI > Anthropic API > DashScope > OpenAI
  34. """
  35. # 1. 检测 Ollama 本地模型
  36. try:
  37. from .ollama_lam import ollama_available
  38. models = ollama_available()
  39. if models:
  40. # 优先选中文模型
  41. preferred = ["qwen2.5:7b", "qwen2.5:14b", "glm4:9b", "llama3.1:8b"]
  42. selected = None
  43. for p in preferred:
  44. if p in models:
  45. selected = p
  46. break
  47. if not selected:
  48. selected = models[0]
  49. print("✅ 使用 Ollama 本地模型 ({})".format(selected))
  50. print(" 可用模型: {}".format(", ".join(models[:5])))
  51. return selected, False, BACKEND_OLLAMA
  52. except Exception:
  53. pass
  54. # 2. 检测 Claude Code CLI
  55. if shutil.which("claude"):
  56. print("✅ 使用 Claude Code Max Plan (无需 API Key)")
  57. print(" 模型: claude sonnet (通过 claude CLI)")
  58. return "sonnet", False, BACKEND_CLAUDE_CODE
  59. # 3. 回退到 API 模式
  60. if os.environ.get("ANTHROPIC_API_KEY"):
  61. model = "claude-sonnet-4-20250514"
  62. print("✅ 使用 Anthropic API ({})".format(model))
  63. return model, True, BACKEND_API
  64. elif os.environ.get("DASHSCOPE_API_KEY"):
  65. model = "qwen-max"
  66. print("✅ 使用 DashScope API ({})".format(model))
  67. return model, True, BACKEND_API
  68. elif os.environ.get("OPENAI_API_KEY"):
  69. model = "gpt-4o"
  70. print("✅ 使用 OpenAI API ({})".format(model))
  71. return model, True, BACKEND_API
  72. return "", False, ""