teacher.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. """
  2. Teacher Model Tool - 教师模型工具
  3. 当遇到复杂问题或需要专家建议时,可以向教师模型提问获得帮助。
  4. 教师模型使用更强大的模型(默认 openai/gpt-5.4)来提供指导和建议。
  5. """
  6. import asyncio
  7. import os
  8. from typing import Any, Dict, Optional
  9. from agent.tools import tool
  10. from agent.llm import create_openrouter_llm_call
  11. # 默认教师模型配置
  12. DEFAULT_TEACHER_MODEL = "openai/gpt-5.4"
  13. # 可以通过环境变量覆盖
  14. TEACHER_MODEL = os.getenv("TEACHER_MODEL", DEFAULT_TEACHER_MODEL)
  15. @tool(
  16. description=(
  17. "向教师模型提问,获取专家级的建议和指导。"
  18. "适用场景:"
  19. "1. 遇到复杂问题需要深入分析时;"
  20. "2. 需要验证当前思路是否正确时;"
  21. "3. 需要专业建议来做决策时;"
  22. "4. 需要帮助理解复杂概念或任务时。"
  23. "教师模型会提供详细的分析和建议,但最终决策仍由你做出。"
  24. )
  25. )
  26. async def ask_teacher(
  27. question: str,
  28. context: Optional[str] = None,
  29. model: Optional[str] = None,
  30. **kwargs
  31. ) -> Dict[str, Any]:
  32. """
  33. 向教师模型提问,获取专家建议
  34. Args:
  35. question: 要提问的问题。应该清晰、具体地描述你的问题或困惑。
  36. context: 问题的上下文信息(可选)。包括:当前任务、已有的信息、已尝试的方法等。
  37. model: 使用的教师模型(可选,默认:openai/gpt-5.4)。可选值:openai/gpt-5.4, openai/o1, anthropic/claude-opus-4-5 等
  38. Returns:
  39. 包含教师模型回答的字典
  40. """
  41. try:
  42. # 使用指定的模型或默认模型
  43. teacher_model = model or TEACHER_MODEL
  44. # 构建系统提示
  45. system_prompt = """你是一个经验丰富的AI助手教师模型。你的职责是:
  46. 1. 提供清晰、准确的分析和建议
  47. 2. 帮助理解复杂的问题和概念
  48. 3. 提供多角度的思考方向
  49. 4. 指出潜在的问题和风险
  50. 5. 给出具体可行的建议
  51. 回答时请:
  52. - 结构清晰,分点说明
  53. - 提供具体的理由和依据
  54. - 如果有多种方案,说明各自的优缺点
  55. - 保持客观和专业
  56. - 如果不确定,明确说明不确定性"""
  57. # 构建用户消息
  58. user_message = question
  59. if context:
  60. user_message = f"**背景信息**:\n{context}\n\n**问题**:\n{question}"
  61. # 构建消息列表
  62. messages = [
  63. {"role": "system", "content": system_prompt},
  64. {"role": "user", "content": user_message}
  65. ]
  66. # 调用教师模型
  67. llm_call = create_openrouter_llm_call(model=teacher_model)
  68. response = await llm_call(
  69. messages=messages,
  70. temperature=0.7,
  71. max_tokens=4000
  72. )
  73. # 提取回答
  74. answer = response.get("content", "")
  75. # 返回结果
  76. return {
  77. "status": "success",
  78. "model": teacher_model,
  79. "question": question,
  80. "answer": answer,
  81. "usage": {
  82. "prompt_tokens": response.get("prompt_tokens", 0),
  83. "completion_tokens": response.get("completion_tokens", 0),
  84. "total_tokens": response.get("prompt_tokens", 0) + response.get("completion_tokens", 0)
  85. }
  86. }
  87. except Exception as e:
  88. return {
  89. "status": "error",
  90. "error": str(e),
  91. "question": question
  92. }