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

Method do_chain

toolbench/inference/Algorithms/single_chain.py:94–187  ·  view source on GitHub ↗
(self,now_node,single_chain_max_step)

Source from the content-addressed store, hash-verified

92
93
94 def do_chain(self,now_node,single_chain_max_step):
95
96 if self.start_message_list == None:
97 system = FORMAT_INSTRUCTIONS_SYSTEM_FUNCTION
98 system = system.replace("{task_description}",self.io_func.task_description)
99 self.tree.root.messages.append({"role":"system","content":system})
100
101 user = FORMAT_INSTRUCTIONS_USER_FUNCTION
102 user = user.replace("{input_description}",self.io_func.input_description)
103 self.tree.root.messages.append({"role":"user","content":user})
104 else:
105 """In Reflection Algo, we startswith former trials and reflections, so the caller will give the start messages"""
106 self.tree.root.messages = self.start_message_list
107
108 now_node = self.tree.root
109 while True:
110 # recursively parse message into nodes
111 self.llm.change_messages(now_node.messages)
112 new_message,error_code,total_tokens = self.llm.parse(functions=self.io_func.functions,process_id=self.process_id)
113 self.total_tokens += total_tokens
114 self.query_count += 1
115 assert new_message["role"] == "assistant"
116 if "content" in new_message.keys() and new_message["content"] != None:
117 temp_node = tree_node()
118 temp_node.node_type = "Thought"
119 temp_node.description = new_message["content"]
120 child_io_state = deepcopy(now_node.io_state)
121
122 temp_node.io_state = child_io_state
123 temp_node.is_terminal = child_io_state.check_success() != 0
124 temp_node.messages = now_node.messages.copy()
125 temp_node.father = now_node
126 now_node.children.append(temp_node)
127 temp_node.print(self.process_id)
128 now_node = temp_node
129
130 if error_code != 0:
131 now_node.observation_code = error_code
132 now_node.pruned = True
133
134 if "function_call" in new_message.keys():
135 function_name = new_message["function_call"]["name"]
136 temp_node = tree_node()
137 temp_node.node_type = "Action"
138 temp_node.description = function_name
139 child_io_state = deepcopy(now_node.io_state)
140
141 temp_node.io_state = child_io_state
142 temp_node.is_terminal = child_io_state.check_success() != 0
143 temp_node.messages = now_node.messages.copy()
144 temp_node.father = now_node
145 now_node.children.append(temp_node)
146
147 temp_node.print(self.process_id)
148 now_node = temp_node
149
150 function_input = new_message["function_call"]["arguments"]
151 temp_node = tree_node()

Callers 1

startMethod · 0.95

Calls 8

tree_nodeClass · 0.90
copyMethod · 0.80
printMethod · 0.80
get_depthMethod · 0.80
change_messagesMethod · 0.45
parseMethod · 0.45
check_successMethod · 0.45
stepMethod · 0.45

Tested by

no test coverage detected