teacher.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. name="ask_teacher",
  17. description=(
  18. "向教师模型提问,获取专家级的建议和指导。"
  19. "适用场景:"
  20. "1. 遇到复杂问题需要深入分析时"
  21. "2. 需要验证当前思路是否正确时"
  22. "3. 需要专业建议来做决策时"
  23. "4. 需要帮助理解复杂概念或任务时"
  24. "教师模型会提供详细的分析和建议,但最终决策仍由你做出。"
  25. ),
  26. parameters={
  27. "type": "object",
  28. "properties": {
  29. "question": {
  30. "type": "string",
  31. "description": (
  32. "向教师模型提出的问题。应该清晰、具体地描述你的问题或困惑。"
  33. "可以包含背景信息、当前进展、遇到的困难等。"
  34. )
  35. },
  36. "context": {
  37. "type": "string",
  38. "description": (
  39. "问题的上下文信息(可选)。"
  40. "包括:当前任务、已有的信息、已尝试的方法等。"
  41. "提供更多上下文可以帮助教师模型给出更准确的建议。"
  42. )
  43. },
  44. "model": {
  45. "type": "string",
  46. "description": (
  47. f"使用的教师模型(可选,默认:{TEACHER_MODEL})。"
  48. "可选值:openai/gpt-5.4, openai/o1, anthropic/claude-opus-4-5 等"
  49. )
  50. }
  51. },
  52. "required": ["question"]
  53. }
  54. )
  55. async def ask_teacher(
  56. question: str,
  57. context: Optional[str] = None,
  58. model: Optional[str] = None,
  59. **kwargs
  60. ) -> Dict[str, Any]:
  61. """
  62. 向教师模型提问,获取专家建议
  63. Args:
  64. question: 要提问的问题
  65. context: 问题的上下文信息(可选)
  66. model: 使用的教师模型(可选)
  67. **kwargs: 其他参数(从工具调用传入)
  68. Returns:
  69. 包含教师模型回答的字典
  70. """
  71. try:
  72. # 使用指定的模型或默认模型
  73. teacher_model = model or TEACHER_MODEL
  74. # 构建系统提示
  75. system_prompt = """你是一个经验丰富的AI助手教师模型。你的职责是:
  76. 1. 提供清晰、准确的分析和建议
  77. 2. 帮助理解复杂的问题和概念
  78. 3. 提供多角度的思考方向
  79. 4. 指出潜在的问题和风险
  80. 5. 给出具体可行的建议
  81. 回答时请:
  82. - 结构清晰,分点说明
  83. - 提供具体的理由和依据
  84. - 如果有多种方案,说明各自的优缺点
  85. - 保持客观和专业
  86. - 如果不确定,明确说明不确定性"""
  87. # 构建用户消息
  88. user_message = question
  89. if context:
  90. user_message = f"**背景信息**:\n{context}\n\n**问题**:\n{question}"
  91. # 构建消息列表
  92. messages = [
  93. {"role": "system", "content": system_prompt},
  94. {"role": "user", "content": user_message}
  95. ]
  96. # 调用教师模型
  97. llm_call = create_openrouter_llm_call(model=teacher_model)
  98. response = await llm_call(
  99. messages=messages,
  100. temperature=0.7,
  101. max_tokens=4000
  102. )
  103. # 提取回答
  104. answer = response.get("content", "")
  105. # 返回结果
  106. return {
  107. "status": "success",
  108. "model": teacher_model,
  109. "question": question,
  110. "answer": answer,
  111. "usage": {
  112. "prompt_tokens": response.get("prompt_tokens", 0),
  113. "completion_tokens": response.get("completion_tokens", 0),
  114. "total_tokens": response.get("prompt_tokens", 0) + response.get("completion_tokens", 0)
  115. }
  116. }
  117. except Exception as e:
  118. return {
  119. "status": "error",
  120. "error": str(e),
  121. "question": question
  122. }