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

Method from_folder

src/smolagents/agents.py:1119–1158  ·  view source on GitHub ↗

Loads an agent from a local folder. Args: folder (`str` or `Path`): The folder where the agent is saved. **kwargs: Additional keyword arguments that will be passed to the agent's init.

(cls, folder: str | Path, **kwargs)

Source from the content-addressed store, hash-verified

1117
1118 @classmethod
1119 def from_folder(cls, folder: str | Path, **kwargs):
1120 """Loads an agent from a local folder.
1121
1122 Args:
1123 folder (`str` or `Path`): The folder where the agent is saved.
1124 **kwargs: Additional keyword arguments that will be passed to the agent's init.
1125 """
1126 # Load agent.json
1127 folder = Path(folder)
1128 agent_dict = json.loads((folder / "agent.json").read_text())
1129 # Handle HfApiModel -> InferenceClientModel rename for old agents
1130 if agent_dict.get("model", {}).get("class") == "HfApiModel":
1131 agent_dict["model"]["class"] = "InferenceClientModel"
1132 logger.warning(
1133 "The agent you're loading uses the deprecated 'HfApiModel' class: it was automatically updated to 'InferenceClientModel'."
1134 )
1135 # Load managed agents from their respective folders, recursively
1136 managed_agents = []
1137 for managed_agent_name, managed_agent_class_name in agent_dict["managed_agents"].items():
1138 agent_cls = AGENT_REGISTRY.get(managed_agent_class_name)
1139 if agent_cls is None:
1140 raise ValueError(
1141 f"Unknown agent class '{managed_agent_class_name}'. "
1142 f"Supported agents: {', '.join(sorted(AGENT_REGISTRY.keys()))}"
1143 )
1144 managed_agents.append(agent_cls.from_folder(folder / "managed_agents" / managed_agent_name))
1145 agent_dict["managed_agents"] = {}
1146
1147 # Load tools
1148 tools = []
1149 for tool_name in agent_dict["tools"]:
1150 tool_code = (folder / "tools" / f"{tool_name}.py").read_text()
1151 tools.append({"name": tool_name, "code": tool_code})
1152 agent_dict["tools"] = tools
1153
1154 # Add managed agents to kwargs to override the empty list in from_dict
1155 if managed_agents:
1156 kwargs["managed_agents"] = managed_agents
1157
1158 return cls.from_dict(agent_dict, **kwargs)
1159
1160 def push_to_hub(
1161 self,

Callers 3

from_hubMethod · 0.80
test_from_folderMethod · 0.80
test_multiagents_saveMethod · 0.80

Calls 3

loadsMethod · 0.80
appendMethod · 0.80
from_dictMethod · 0.45

Tested by 2

test_from_folderMethod · 0.64
test_multiagents_saveMethod · 0.64