MCPcopy
hub / github.com/huggingface/smolagents / ToolCallingAgent

Class ToolCallingAgent

src/smolagents/agents.py:1215–1502  ·  view source on GitHub ↗

This agent uses JSON-like tool calls, using method `model.get_tool_call` to leverage the LLM engine's tool calling capabilities. Args: tools (`list[Tool]`): [`Tool`]s that the agent can use. model (`Model`): Model that will generate the agent's actions. prompt_templ

Source from the content-addressed store, hash-verified

1213
1214
1215class ToolCallingAgent(MultiStepAgent):
1216 """
1217 This agent uses JSON-like tool calls, using method `model.get_tool_call` to leverage the LLM engine's tool calling capabilities.
1218
1219 Args:
1220 tools (`list[Tool]`): [`Tool`]s that the agent can use.
1221 model (`Model`): Model that will generate the agent's actions.
1222 prompt_templates ([`~agents.PromptTemplates`], *optional*): Prompt templates.
1223 planning_interval (`int`, *optional*): Interval at which the agent will run a planning step.
1224 stream_outputs (`bool`, *optional*, default `False`): Whether to stream outputs during execution.
1225 max_tool_threads (`int`, *optional*): Maximum number of threads for parallel tool calls.
1226 Higher values increase concurrency but resource usage as well.
1227 Defaults to `ThreadPoolExecutor`'s default.
1228 **kwargs: Additional keyword arguments.
1229 """
1230
1231 def __init__(
1232 self,
1233 tools: list[Tool],
1234 model: Model,
1235 prompt_templates: PromptTemplates | None = None,
1236 planning_interval: int | None = None,
1237 stream_outputs: bool = False,
1238 max_tool_threads: int | None = None,
1239 **kwargs,
1240 ):
1241 prompt_templates = prompt_templates or yaml.safe_load(
1242 importlib.resources.files("smolagents.prompts").joinpath("toolcalling_agent.yaml").read_text()
1243 )
1244 super().__init__(
1245 tools=tools,
1246 model=model,
1247 prompt_templates=prompt_templates,
1248 planning_interval=planning_interval,
1249 **kwargs,
1250 )
1251 # Streaming setup
1252 self.stream_outputs = stream_outputs
1253 if self.stream_outputs and not hasattr(self.model, "generate_stream"):
1254 raise ValueError(
1255 "`stream_outputs` is set to True, but the model class implements no `generate_stream` method."
1256 )
1257 # Tool calling setup
1258 self.max_tool_threads = max_tool_threads
1259
1260 @property
1261 def tools_and_managed_agents(self):
1262 """Returns a combined list of tools and managed agents."""
1263 return list(self.tools.values()) + list(self.managed_agents.values())
1264
1265 def initialize_system_prompt(self) -> str:
1266 system_prompt = populate_template(
1267 self.prompt_templates["system_prompt"],
1268 variables={
1269 "tools": self.tools,
1270 "managed_agents": self.managed_agents,
1271 "custom_instructions": self.instructions,
1272 },

Calls

no outgoing calls

Used in the wild real call sites across dependent graphs

searching dependent graphs…