File size: 1,378 Bytes
dc893fb |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
"""Base tool classes."""
from typing import Any
from pydantic import BaseModel
class ToolResult(BaseModel):
"""Tool execution result."""
success: bool
content: str = ""
error: str | None = None
class Tool:
"""Base class for all tools."""
@property
def name(self) -> str:
"""Tool name."""
raise NotImplementedError
@property
def description(self) -> str:
"""Tool description."""
raise NotImplementedError
@property
def parameters(self) -> dict[str, Any]:
"""Tool parameters schema (JSON Schema format)."""
raise NotImplementedError
async def execute(self, *args, **kwargs) -> ToolResult: # type: ignore
"""Execute the tool with arbitrary arguments."""
raise NotImplementedError
def to_schema(self) -> dict[str, Any]:
"""Convert tool to Anthropic tool schema."""
return {
"name": self.name,
"description": self.description,
"input_schema": self.parameters,
}
def to_openai_schema(self) -> dict[str, Any]:
"""Convert tool to OpenAI tool schema."""
return {
"type": "function",
"function": {
"name": self.name,
"description": self.description,
"parameters": self.parameters,
},
}
|