internlm_react.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. from prompt.react import ReAct
  2. INTERNLM_TOOL_DESCRIPTION = """用来执行Python代码。代码必须是一个函数,
  3. 函数名必须得是 'solution',代码对应你的思考过程。代码实例格式如下:
  4. ```python
  5. # import 依赖包
  6. import xxx
  7. def solution():
  8. # 初始化一些变量
  9. variable_names_with_real_meaning = xxx
  10. # 步骤一
  11. mid_variable = func(variable_names_with_real_meaning)
  12. # 步骤 x
  13. mid_variable = func(mid_variable)
  14. # 最后结果
  15. final_answer = func(mid_variable)
  16. return final_answer
  17. ```"""
  18. INTERNLM_TOOL = {'PythonInterpreter': INTERNLM_TOOL_DESCRIPTION}
  19. INTERNLM_REACT_PROMPT_ZH = """<|System|>:你是一个可以调用外部工具的助手,可以使用的工具包括:
  20. {tools_text}
  21. 如果使用工具请遵循以下格式回复:
  22. ```
  23. Thought:思考你当前步骤需要解决什么问题,是否需要使用工具
  24. Action:工具名称,你的工具必须从 [{tools_name_text}] 选择
  25. ActionInput:工具输入参数
  26. ```
  27. 工具返回按照以下格式回复:
  28. ```
  29. Response:调用工具后的结果
  30. ```
  31. 如果你已经知道了答案,或者你不需要工具,请遵循以下格式回复
  32. ```
  33. Thought:给出最终答案的思考过程
  34. FinalAnswer:最终答案
  35. ```
  36. 开始!<TOKENS_UNUSED_2>
  37. <|User|>:{query}<eoh>
  38. <|Bot|>:"""
  39. INTERNLM_REACT_PROMPT_EN = """<|System|>:You are a assistant who can utilize external tools.
  40. {tools_text}
  41. To use a tool, please use the following format:
  42. ```
  43. Thought: Think what you need to solve, do you need to use tools?
  44. Action: the tool name, should be one of [{tools_name_text}]
  45. ActionInput: the input to the action
  46. ```
  47. The response after utilizing tools should using the following format:
  48. ```
  49. Response: the results after call the tool.
  50. ``
  51. If you already know the answer, or you do not need to use tools,
  52. please using the following format to reply:
  53. ```
  54. Thought: the thought process to get the final answer
  55. FinalAnswer: final answer
  56. ```
  57. Begin!<TOKENS_UNUSED_2>
  58. <|User|>:{query}<eoh>
  59. <|Bot|>:"""
  60. class InternLMReAct(ReAct):
  61. def __init__(self, query, lang='en', upload_file_paths=[]):
  62. super().__init__(query, lang, upload_file_paths)
  63. self.react_template = INTERNLM_REACT_PROMPT_ZH if self.lang == 'zh' else INTERNLM_REACT_PROMPT_EN
  64. def build_prompt(self):
  65. planning_prompt = super().build_prompt()
  66. if '<|im_end|>' in self.query and planning_prompt.endswith('<eoh>\n<|Bot|>:'):
  67. planning_prompt = planning_prompt[: -len('<eoh>\n<|Bot|>:')]
  68. if '<|im_end|>' in self.query:
  69. planning_prompt = planning_prompt.replace('<|im_end|>\n<|im_start|>assistant\n', '<eoh>\n<|Bot|>:').replace('Observation:', '<eoa>\n<|System|>:Response:').replace('\nAction Input', '\nActionInput').replace('code_interpreter', 'PythonInterpreter')
  70. assert planning_prompt.endswith('Thought:')
  71. planning_prompt = planning_prompt[: -len('Thought:')] + '<TOKENS_UNUSED_2>\n<|Bot|>:'
  72. self.prompt = planning_prompt
  73. return planning_prompt
  74. def _build_tools_text(self):
  75. return INTERNLM_TOOL
  76. def _build_tools_name_text(self):
  77. return list(INTERNLM_TOOL.keys())
  78. def build_observation(self, observation):
  79. return f'<eoa>\n<|System|>:Response:{observation}\n<TOKENS_UNUSED_2>\n<|Bot|>:'
  80. def get_stop_words_list(self):
  81. return ['<eoa>']