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

Method load_tools

bmtools/agent/singletool.py:90–222  ·  view source on GitHub ↗
(self, name, meta_info, prompt_type="react-with-tool-description", return_intermediate_steps=True)

Source from the content-addressed store, hash-verified

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()
125 tool_str = "; ".join([t.name for t in self.all_tools_map[name]])
126 prefix = """You are an AI who performs one task based on the following objective: {objective}. Take into account these previously completed tasks: {context}.\n You have access to the following APIs:"""
127 suffix = """YOUR CONSTRAINTS: (1) YOU MUST follow this format:
128 \nThought:\nAction:\nAction Input: \n or \nThought:\nFinal Answer:\n (2) Do not make up anything, and if your Observation has no link, DO NOT hallucihate one. (3) The Action: MUST be one of the following: """ + tool_str + """\nQuestion: {task}\n Agent scratchpad (history actions): {agent_scratchpad}."""
129
130 prompt = ZeroShotAgent.create_prompt(
131 self.all_tools_map[name],
132 prefix=prefix,
133 suffix=suffix,
134 input_variables=["objective", "task", "context","agent_scratchpad"]
135 )
136
137 logger.info("Full prompt template: {}".format(prompt.template))
138 # specify the maximum number of iterations you want babyAGI to perform
139 max_iterations = 10
140 baby_agi = BabyAGI.from_llm(
141 llm=self.llm,
142 # llm=customllm,
143 prompt=prompt,
144 verbose=False,
145 tools=self.all_tools_map[name],
146 stream_output=self.stream_output,
147 return_intermediate_steps=return_intermediate_steps,

Callers 15

load_toolsMethod · 0.95
test.pyFile · 0.45
test.pyFile · 0.45
test.pyFile · 0.45
test.pyFile · 0.45
test.pyFile · 0.45
test.pyFile · 0.45
test.pyFile · 0.45
test.pyFile · 0.45
test.pyFile · 0.45
test.pyFile · 0.45
test.pyFile · 0.45

Calls 4

import_all_apisFunction · 0.85
create_promptMethod · 0.80
from_llmMethod · 0.45
from_llm_and_toolsMethod · 0.45

Tested by

no test coverage detected