MCPcopy
hub / github.com/langroid/langroid / clone

Method clone

langroid/agent/chat_agent.py:308–338  ·  view source on GitHub ↗

Create i'th clone of this agent, ensuring tool use/handling is cloned. Important: We assume all member variables are in the __init__ method here and in the Agent class. TODO: We are attempting to clone an agent after its state has been changed in possibly many ways. B

(self, i: int = 0)

Source from the content-addressed store, hash-verified

306 return cast(ChatAgent, Agent.from_id(id))
307
308 def clone(self, i: int = 0) -> "ChatAgent":
309 """Create i'th clone of this agent, ensuring tool use/handling is cloned.
310 Important: We assume all member variables are in the __init__ method here
311 and in the Agent class.
312 TODO: We are attempting to clone an agent after its state has been
313 changed in possibly many ways. Below is an imperfect solution. Caution advised.
314 Revisit later.
315 """
316 agent_cls = type(self)
317 # Use model_copy to preserve Pydantic subclass types (like MockLMConfig)
318 # instead of deepcopy which loses subclass information
319 config_copy = self.config.model_copy(deep=True)
320 config_copy.name = f"{config_copy.name}-{i}"
321 new_agent = agent_cls(config_copy)
322 new_agent.system_tool_instructions = self.system_tool_instructions
323 new_agent.system_tool_format_instructions = self.system_tool_format_instructions
324 new_agent.llm_tools_map = self.llm_tools_map
325 new_agent.llm_tools_known = self.llm_tools_known
326 new_agent.llm_tools_handled = self.llm_tools_handled
327 new_agent.llm_tools_usable = self.llm_tools_usable
328 new_agent.llm_functions_map = self.llm_functions_map
329 new_agent.llm_functions_handled = self.llm_functions_handled
330 new_agent.llm_functions_usable = self.llm_functions_usable
331 new_agent.llm_function_force = self.llm_function_force
332 # Ensure each clone gets its own vecdb client when supported.
333 new_agent.vecdb = None if self.vecdb is None else self.vecdb.clone()
334 self._clone_extra_state(new_agent)
335 new_agent.id = ObjectRegistry.new_id()
336 if self.config.add_to_registry:
337 ObjectRegistry.register_object(new_agent)
338 return new_agent
339
340 def _clone_extra_state(self, new_agent: "ChatAgent") -> None:
341 """Hook for subclasses to copy additional state into clones."""

Callers

nothing calls this directly

Calls 4

_clone_extra_stateMethod · 0.95
model_copyMethod · 0.80
new_idMethod · 0.80
register_objectMethod · 0.80

Tested by

no test coverage detected