MCPcopy Create free account
hub / github.com/OpenBMB/BMTools / answer_by_tools

Function answer_by_tools

web_demo.py:67–112  ·  view source on GitHub ↗
(question, tools_chosen, model_chosen)

Source from the content-addressed store, hash-verified

65 return [gr.update(value='<span class="">'+imgs+'</span>', visible=True), gr.update(visible=True)]
66
67def answer_by_tools(question, tools_chosen, model_chosen):
68 global return_msg
69 return_msg += [(question, None), (None, '...')]
70 yield [gr.update(visible=True, value=return_msg), gr.update(), gr.update()]
71 OPENAI_API_KEY = os.environ.get('OPENAI_API_KEY', '')
72
73 if len(tools_chosen) == 0: # if there is no tools chosen, we use all todo (TODO: What if the pool is too large.)
74 tools_chosen = list(valid_tools_info.keys())
75
76 if len(tools_chosen) == 1:
77 answerer = STQuestionAnswerer(OPENAI_API_KEY.strip(), stream_output=True, llm=model_chosen)
78 agent_executor = answerer.load_tools(tools_chosen[0], valid_tools_info[tools_chosen[0]], prompt_type="react-with-tool-description", return_intermediate_steps=True)
79 else:
80 answerer = MTQuestionAnswerer(OPENAI_API_KEY.strip(), load_valid_tools({k: tools_mappings[k] for k in tools_chosen}), stream_output=True, llm=model_chosen)
81
82 agent_executor = answerer.build_runner()
83
84 global chat_history
85 chat_history += "Question: " + question + "\n"
86 question = chat_history
87 for inter in agent_executor(question):
88 if isinstance(inter, AgentFinish): continue
89 result_str = []
90 return_msg.pop()
91 if isinstance(inter, dict):
92 result_str.append("<font color=red>Answer:</font> {}".format(inter['output']))
93 chat_history += "Answer:" + inter['output'] + "\n"
94 result_str.append("...")
95 else:
96 not_observation = inter[0].log
97 if not not_observation.startswith('Thought:'):
98 not_observation = "Thought: " + not_observation
99 chat_history += not_observation
100 not_observation = not_observation.replace('Thought:', '<font color=green>Thought: </font>')
101 not_observation = not_observation.replace('Action:', '<font color=purple>Action: </font>')
102 not_observation = not_observation.replace('Action Input:', '<font color=purple>Action Input: </font>')
103 result_str.append("{}".format(not_observation))
104 result_str.append("<font color=blue>Action output:</font>\n{}".format(inter[1]))
105 chat_history += "\nAction output:" + inter[1] + "\n"
106 result_str.append("...")
107 return_msg += [(None, result) for result in result_str]
108 yield [gr.update(visible=True, value=return_msg), gr.update(), gr.update()]
109 return_msg.pop()
110 if return_msg[-1][1].startswith("<font color=red>Answer:</font> "):
111 return_msg[-1] = (return_msg[-1][0], return_msg[-1][1].replace("<font color=red>Answer:</font> ", "<font color=green>Final Answer:</font> "))
112 yield [gr.update(visible=True, value=return_msg), gr.update(visible=True), gr.update(visible=False)]
113
114def retrieve(tools_search):
115 if tools_search == "":

Callers

nothing calls this directly

Calls 5

load_toolsMethod · 0.95
build_runnerMethod · 0.95
STQuestionAnswererClass · 0.90
MTQuestionAnswererClass · 0.90
load_valid_toolsFunction · 0.90

Tested by

no test coverage detected