MCPcopy Create free account
hub / github.com/OpenBMB/ToolBench / parse

Method parse

toolbench/inference/LLM/davinci_model.py:73–126  ·  view source on GitHub ↗
(self,functions,process_id,**args)

Source from the content-addressed store, hash-verified

71 print("end_print"+"*"*50)
72
73 def parse(self,functions,process_id,**args):
74 conv = get_conversation_template("tool-llama-single-round")
75 roles = {"system": conv.roles[0], "user": conv.roles[1], "function": conv.roles[2], "assistant": conv.roles[3]}
76 conversation_history = self.conversation_history
77 question = ''
78 for message in conversation_history:
79 role = roles[message['role']]
80 content = message['content']
81 if role == "User":
82 question = content
83 break
84 func_str = ""
85 func_list = []
86 for function_dict in functions:
87 param_str = ""
88 api_name = function_dict["name"]
89 func_list.append(api_name)
90 if "Finish" in api_name:
91 param_str = f'"return_type": string, "final_answer": string, '
92 api_desc = "If you believe that you have obtained a result that can answer the task, please call this function to provide the final answer. ALWAYS call this function at the end of your attempt to answer the question finally."
93 func_str += f"{api_name}: {api_desc}. Your input should be a json (args json schema): {param_str} The Action to trigger this API should be {api_name} and the input parameters should be a json dict string. Pay attention to the type of parameters.\n\n"
94 else:
95 api_desc = function_dict["description"][function_dict["description"].find("The description of this function is: ")+len("The description of this function is: "):]
96 for param_name in function_dict["parameters"]["properties"]:
97 data_type = function_dict["parameters"]["properties"][param_name]["type"]
98 param_str += f'"{param_name}": {data_type}, '
99 param_str = "{{" + param_str + "}}"
100 func_str += f"{api_name}: {api_desc}. Your input should be a json (args json schema): {param_str} The Action to trigger this API should be {api_name} and the input parameters should be a json dict string. Pay attention to the type of parameters.\n\n"
101 func_list = str(func_list)
102 prompt = FORMAT_INSTRUCTIONS_SYSTEM_FUNCTION_ZEROSHOT.replace("{func_str}", func_str).replace("{func_list}", func_list).replace("{func_list}", func_list).replace("{question}", question)
103 prompt = prompt.replace("{{", "{").replace("}}", "}")
104 for message in conversation_history:
105 role = roles[message['role']]
106 content = message['content']
107 if role == "Assistant":
108 prompt += f"\n{content}\n"
109 elif role == "Function":
110 prompt += f"Observation: {content}\n"
111 if functions != []:
112 predictions, usage = self.prediction(prompt)
113 else:
114 predictions, usage = self.prediction(prompt)
115
116 # react format prediction
117 thought, action, action_input = react_parser(predictions)
118 message = {
119 "role": "assistant",
120 "content": thought,
121 "function_call": {
122 "name": action,
123 "arguments": action_input
124 }
125 }
126 return message, 0, usage["total_tokens"]
127
128
129if __name__ == "__main__":

Callers 3

DFSMethod · 0.45
do_chainMethod · 0.45
rank2_subfixFunction · 0.45

Calls 3

predictionMethod · 0.95
react_parserFunction · 0.90

Tested by

no test coverage detected