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

Class STQuestionAnswerer

bmtools/agent/singletool.py:67–222  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

65
66
67class STQuestionAnswerer:
68 def __init__(self, openai_api_key = "", stream_output=False, llm='ChatGPT'):
69 if len(openai_api_key) < 3: # not valid key (TODO: more rigorous checking)
70 openai_api_key = os.environ.get('OPENAI_API_KEY')
71
72 self.openai_api_key = openai_api_key
73 self.llm_model = llm
74
75 self.set_openai_api_key(openai_api_key)
76 self.stream_output = stream_output
77
78
79
80 def set_openai_api_key(self, key):
81 logger.info("Using {}".format(self.llm_model))
82
83 if self.llm_model == "GPT-3.5":
84 self.llm = OpenAI(temperature=0.0, openai_api_key=key) # use text-darvinci
85 elif self.llm_model == "ChatGPT":
86 self.llm = OpenAI(model_name="gpt-3.5-turbo", temperature=0.0, openai_api_key=key) # use chatgpt
87 else:
88 raise RuntimeError("Your model is not available.")
89
90 def load_tools(self, name, meta_info, prompt_type="react-with-tool-description", return_intermediate_steps=True):
91
92 self.all_tools_map = {}
93 self.all_tools_map[name] = import_all_apis(meta_info)
94
95 logger.info("Tool [{}] has the following apis: {}".format(name, self.all_tools_map[name]))
96
97 if prompt_type == "zero-shot-react-description":
98 subagent = initialize_agent(self.all_tools_map[name], self.llm, agent="zero-shot-react-description", verbose=True, return_intermediate_steps=return_intermediate_steps)
99 elif prompt_type == "react-with-tool-description":
100 # customllm = CustomLLM()
101 description_for_model = meta_info['description_for_model'].replace("{", "{{").replace("}", "}}").strip()
102
103 prefix = f"""Answer the following questions as best you can. General instructions are: {description_for_model}. Specifically, you have access to the following APIs:"""
104 #suffix = """Begin! Remember: (1) Follow the format, i.e,\nThought:\nAction:\nAction Input:\nObservation:\nFinal Answer:\n (2) Provide as much as useful information in your Final Answer. (3) YOU MUST INCLUDE all relevant IMAGES in your Final Answer using format ![img](url), and include relevant links. (3) Do not make up anything, and if your Observation has no link, DO NOT hallucihate one. (4) If you have enough information, please use \nThought: I have got enough information\nFinal Answer: \n\nQuestion: {input}\n{agent_scratchpad}"""
105 suffix = """Begin! Remember: (1) Follow the format, i.e,\nThought:\nAction:\nAction Input:\nObservation:\nFinal Answer:\n. The action you generate must be exact one of the given API names instead of a sentence or any other redundant text. The action input is one json format dict without any redundant text or bracket descriptions . (2) Provide as much as useful information (such as useful values/file paths in your observation) in your Final Answer. Do not describe the process you achieve the goal, but only provide the detailed answer or response to the task goal. (3) Do not make up anything. DO NOT generate observation content by yourself. (4) Read the observation carefully, and pay attention to the messages even if an error occurs. (5) Once you have enough information, please immediately use \nThought: I have got enough information\nFinal Answer: \n\nTask: {input}\n{agent_scratchpad}"""
106
107 prompt = ZeroShotAgent.create_prompt(
108 self.all_tools_map[name],
109 prefix=prefix,
110 suffix=suffix,
111 input_variables=["input", "agent_scratchpad"]
112 )
113 llm_chain = LLMChain(llm=self.llm, prompt=prompt)
114 # llm_chain = LLMChain(llm=customllm, prompt=prompt)
115 logger.info("Full prompt template: {}".format(prompt.template))
116 tool_names = [tool.name for tool in self.all_tools_map[name] ]
117 agent = ZeroShotAgent(llm_chain=llm_chain, allowed_tools=tool_names)
118 if self.stream_output:
119 agent_executor = Executor.from_agent_and_tools(agent=agent, tools=self.all_tools_map[name] , verbose=True, return_intermediate_steps=return_intermediate_steps)
120 else:
121 agent_executor = AgentExecutorWithTranslation.from_agent_and_tools(agent=agent, tools=self.all_tools_map[name], verbose=True, return_intermediate_steps=return_intermediate_steps)
122 return agent_executor
123 elif prompt_type == "babyagi":
124 # customllm = CustomLLM()

Callers 15

test.pyFile · 0.90
answer_by_toolsFunction · 0.90
test.pyFile · 0.90
test.pyFile · 0.90
test.pyFile · 0.90
test.pyFile · 0.90
test.pyFile · 0.90
test.pyFile · 0.90
test.pyFile · 0.90
test.pyFile · 0.90
test.pyFile · 0.90
test.pyFile · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected