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)
| 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, |