Tool configuration
| 35 | raise NotImplementedError("All tools must implement __call__") |
| 36 | |
| 37 | class ToolConfig(BaseModel): |
| 38 | """Tool configuration""" |
| 39 | model_config = ConfigDict(arbitrary_types_allowed=True, extra="allow") |
| 40 | |
| 41 | name: str = Field(description="The name of the tool") |
| 42 | description: str = Field(description="The description of the tool") |
| 43 | metadata: Optional[Dict[str, Any]] = Field(default={}, description="The metadata of the tool") |
| 44 | require_grad: bool = Field(default=False, description="Whether the tool requires gradients") |
| 45 | version: str = Field(default="1.0.0", description="Version of the tool") |
| 46 | |
| 47 | cls: Optional[Type[Tool]] = Field(default=None, description="The class of the tool") |
| 48 | config: Optional[Dict[str, Any]] = Field(default={}, description="The initialization configuration of the tool") |
| 49 | instance: Optional[Tool] = Field(default=None, description="The instance of the tool") |
| 50 | code: Optional[str] = Field(default=None, description="Source code for dynamically generated tool classes (used when cls cannot be imported from a module)") |
| 51 | |
| 52 | # Default representations |
| 53 | function_calling: Optional[Dict[str, Any]] = Field(default=None, description="Default function calling representation") |
| 54 | text: Optional[str] = Field(default=None, description="Default text representation") |
| 55 | args_schema: Optional[Type[BaseModel]] = Field(default=None, description="Default args schema (BaseModel type)") |
| 56 | |
| 57 | def model_dump(self, **kwargs) -> Dict[str, Any]: |
| 58 | """Dump the model to a dictionary, recursively serializing nested Pydantic models.""" |
| 59 | |
| 60 | result = { |
| 61 | "name": self.name, |
| 62 | "description": self.description, |
| 63 | "metadata": self.metadata, |
| 64 | "require_grad": self.require_grad, |
| 65 | "version": self.version, |
| 66 | |
| 67 | "cls": dynamic_manager.get_class_string(self.cls) if self.cls else None, |
| 68 | "config": self.config, |
| 69 | "instance": None, |
| 70 | "code": self.code, |
| 71 | |
| 72 | "function_calling": self.function_calling, |
| 73 | "text": self.text, |
| 74 | "args_schema": dynamic_manager.serialize_args_schema(self.args_schema) if self.args_schema else None, |
| 75 | } |
| 76 | |
| 77 | return result |
| 78 | |
| 79 | @classmethod |
| 80 | def model_validate(cls, data: Dict[str, Any]) -> 'ToolConfig': |
| 81 | """Validate the model from a dictionary.""" |
| 82 | name = data.get("name") |
| 83 | description = data.get("description") |
| 84 | metadata = data.get("metadata") |
| 85 | require_grad = data.get("require_grad", False) # Default to False if not provided |
| 86 | version = data.get("version") |
| 87 | |
| 88 | cls_ = None |
| 89 | code = data.get("code") |
| 90 | if code: |
| 91 | class_name = dynamic_manager.extract_class_name_from_code(code) |
| 92 | if class_name: |
| 93 | try: |
| 94 | cls_ = dynamic_manager.load_class( |
no outgoing calls
no test coverage detected