tools_prompt.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. from typing import List, Dict, Union, Optional
  2. def format_react_en(tool_names, tool_descs):
  3. REACT_PROMPT = """Answer the following questions as best as you can. You have access to the following tools:
  4. {tool_list}
  5. Use the following format:
  6. Thought: you should always think about what to do
  7. Action: the action to take, should be one of [{tool_names}]
  8. Action Input: the input to the action
  9. Observation: the result of the action
  10. ... (this Thought/Action/Action Input/Observation can be repeated zero or more times)
  11. Final Answer: the final answer to the original input question
  12. Begin!
  13. """
  14. return REACT_PROMPT.format(tool_list='\n\n'.join(tool_descs), tool_names=','.join(tool_names))
  15. def format_react_zh(tool_names, tool_descs):
  16. REACT_ZH_PROMPT = """尽你所能回答以下问题。你拥有如下工具:
  17. {tool_list}
  18. 使用以下格式回答:
  19. Thought: 思考你应该做什么
  20. Action: 工具的名称,必须是[{tool_names}]之一
  21. Action Input: 工具的输入
  22. Observation: 工具返回的结果
  23. ... (Thought/Action/Action Input/Observation的过程可以重复零次或多次)
  24. Final Answer: 对输入问题的最终答案
  25. 开始!
  26. """
  27. return REACT_ZH_PROMPT.format(tool_list='\n\n'.join(tool_descs), tool_names=','.join(tool_names))
  28. def format_glm4(tool_names, tool_descs):
  29. GLM4_PROMPT = '''你是一个名为 ChatGLM 的人工智能助手。你是基于智谱AI训练的语言模型 GLM-4 模型开发的,你的任务是针对用户的问题和要求提供适当的答复和支持。
  30. # 可用工具
  31. {tool_list}'''
  32. tool_list = ''
  33. for name, tool in zip(tool_names, tool_descs):
  34. tool_list += f'## {name}\n\n{tool}\n\n'
  35. return GLM4_PROMPT.format(tool_list=tool_list)
  36. def format_toolbench(tool_names, tool_descs):
  37. TOOLBENCH_PROMPT = '''You can use many tools(functions) to do the following task.
  38. First I will give you the task description, and your task start.
  39. At each step, you need to give your thought to analyze the status now and what to do next, \
  40. with a function call to actually execute your step. Your output should follow this format:
  41. Thought:
  42. Action:
  43. Action Input:
  44. After the call, you will get the call result, and you are now in a new state.
  45. Then you will analyze your status now, then decide what to do next...
  46. After many (Thought-call) pairs, you finally perform the task, then you can give your final answer.
  47. Remember:
  48. 1.the state change is irreversible, you can't go back to one of the former state, if you want to restart the task, \
  49. say \"I give up and restart\".
  50. 2.All the thought is short, at most in 5 sentence.
  51. 3.You can do more then one trys, so if your plan is to continuously try some conditions, \
  52. you can do one of the conditions per try.
  53. Let's Begin!
  54. Task description: You should use functions to help handle the real time user querys. Remember:
  55. 1.ALWAYS call \"Finish\" function at the end of the task. And the final answer should contain enough information \
  56. to show to the user,If you can't handle the task, \
  57. or you find that function calls always fail(the function is not valid now), \
  58. use function Finish->give_up_and_restart.
  59. 2.Do not use origin tool names, use only subfunctions' names.
  60. Specifically, you have access to the following APIs: {tool_list}'''
  61. return TOOLBENCH_PROMPT.format(tool_list='\n\n'.join(tool_descs))
  62. tools_prompt = {
  63. 'react_en': format_react_en,
  64. 'react_zh': format_react_zh,
  65. 'glm4': format_glm4,
  66. 'toolbench': format_toolbench,
  67. }
  68. def get_tools_prompt(TOOLS: List[Dict[str, Union[str, dict]]], prompt_format: str = 'react_en') -> Optional[str]:
  69. tool_descs = []
  70. tool_names = []
  71. for info in TOOLS: # info: Dict[str, Union[str, dict]]
  72. try:
  73. if 'function' in info:
  74. info = info['function']
  75. tool_names.append(info['name'])
  76. tool_descs.append(str(info)) # info: dict
  77. except KeyError:
  78. print('invalid tools format, please check'
  79. 'https://github.com/modelscope/swift/blob/main/docs/source_en/LLM/Agent-deployment-best-practice.md')
  80. return None
  81. prompt_format = tools_prompt.get(prompt_format) or format_toolbench
  82. return prompt_format(tool_names, tool_descs)