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

Method to_json

toolbench/inference/Algorithms/DFS.py:49–92  ·  view source on GitHub ↗
(self, answer=False, process=True)

Source from the content-addressed store, hash-verified

47 )
48
49 def to_json(self, answer=False, process=True):
50
51 if process:
52 json_obj = {
53 "win": self.status == 1,
54 "tree": self.tree.to_json_recursive(),
55 "forward_args": self.forward_args,
56 "compare_candidates": [],
57 }
58 for node in self.terminal_node:
59 if node.pruned == False: # has answer
60 json_obj["compare_candidates"].append(
61 node.get_chain_result_from_this_node(use_messages=False))
62 else:
63 json_obj = {}
64
65 if answer:
66 json_obj["answer_generation"] = {
67 "valid_data": False,
68 "query_count": self.query_count,
69 "total_tokens": self.total_tokens,
70 "final_answer": "",
71 "finish_type": "give_answer",
72 "function": self.io_func.functions,
73 "chain": [],
74 }
75 for node in self.terminal_node:
76 if node.pruned == False:
77 json_obj["answer_generation"]["valid_data"] = True
78 json_obj["answer_generation"]["finish_type"] = "give_answer"
79 json_obj["answer_generation"]["final_answer"] = node.description
80 json_obj["answer_generation"]["train_messages"] = node.get_train_messages_from_this_node(
81 )
82 break
83 # do not have final answer, look for give_up
84 if json_obj["answer_generation"]["valid_data"] == False:
85 if len(self.give_up_node) > 0:
86 random_pos = random.randint(0, len(self.give_up_node) - 1)
87 choose_give_up_node = self.give_up_node[random_pos]
88 json_obj["answer_generation"]["valid_data"] = True
89 json_obj["answer_generation"]["finish_type"] = "give_up"
90 json_obj["answer_generation"]["final_answer"] = choose_give_up_node.description
91 json_obj["answer_generation"]["train_messages"] = choose_give_up_node.get_train_messages_from_this_node()
92 return json_obj
93
94 def start(self, single_chain_max_step, tree_beam_size, max_query_count, answer=1, with_filter=True):
95 """ single_chain_max_step: The maximum depth of the tree

Callers

nothing calls this directly

Tested by

no test coverage detected