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

Class single_chain

toolbench/inference/Algorithms/single_chain.py:7–187  ·  view source on GitHub ↗

Implement of CoT method

Source from the content-addressed store, hash-verified

5from copy import deepcopy
6
7class single_chain(base_search_method):
8 """Implement of CoT method
9 """
10 def __init__(self,llm,io_func,extra_prefix="",process_id=0,start_message_list=None):
11 """extra_prefix and start_message_list is used in Reflection Algo"""
12 super(single_chain, self).__init__(llm,io_func, process_id, callbacks=None)
13 self.io_func = io_func
14 self.llm = llm
15 self.extra_prefix = extra_prefix
16 self.start_message_list = start_message_list
17 self.process_id = process_id
18
19 self.restart()
20 def restart(self):
21 self.status = 0
22 self.try_list = []
23 self.terminal_node = []
24
25 self.query_count = 0 # number of interactions with openai
26 self.total_tokens = 0
27 self.success_count = 0
28
29 def to_json(self, answer=False,process=True):
30 if process:
31 json_obj = {
32 "win": self.status == 1,
33 "try_count": len(self.try_list),
34 "trys": self.try_list,
35 "compare_candidates": [],
36 "forward_args":self.forward_args,
37 }
38 for node in self.terminal_node:
39 if node.pruned == False: # has final answer
40 json_obj["compare_candidates"].append(node.get_chain_result_from_this_node(use_messages=False))
41 else:
42 json_obj = {}
43
44 if answer:
45 json_obj["answer_generation"] = {
46 "valid_data": False,
47 "final_answer": "",
48 "function": self.io_func.functions,
49 "query_count": self.query_count,
50 "total_tokens": self.total_tokens,
51 "train_messages": [],
52 "chain": [],
53 }
54 for node in self.terminal_node:
55 if node.pruned == False:
56 json_obj["answer_generation"]["valid_data"] = True
57 json_obj["answer_generation"]["final_answer"] = node.description
58 json_obj["answer_generation"]["train_messages"] = node.get_train_messages_from_this_node()
59 break
60 return json_obj
61
62 def to_json_single(self):
63 """parse the last try
64 Though the nodes are formed as a tree, We still know they are actually a chain

Callers 1

method_converterMethod · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected