58 lines
1.5 KiB
Python
58 lines
1.5 KiB
Python
"""工具装饰器工厂"""
|
|
from typing import Callable, Any, Dict
|
|
from alcor.tools.core import ToolDefinition, registry
|
|
|
|
|
|
def tool(
|
|
name: str,
|
|
description: str,
|
|
parameters: Dict[str, Any],
|
|
category: str = "general"
|
|
) -> Callable:
|
|
"""
|
|
工具注册装饰器
|
|
|
|
用法示例:
|
|
```python
|
|
@tool(
|
|
name="web_search",
|
|
description="Search the internet for information",
|
|
parameters={
|
|
"type": "object",
|
|
"properties": {
|
|
"query": {"type": "string", "description": "Search keywords"},
|
|
"max_results": {"type": "integer", "description": "Max results", "default": 5}
|
|
},
|
|
"required": ["query"]
|
|
},
|
|
category="crawler"
|
|
)
|
|
def web_search(arguments: dict) -> dict:
|
|
# 实现...
|
|
return {"results": []}
|
|
```
|
|
"""
|
|
def decorator(func: Callable) -> Callable:
|
|
tool_def = ToolDefinition(
|
|
name=name,
|
|
description=description,
|
|
parameters=parameters,
|
|
handler=func,
|
|
category=category
|
|
)
|
|
registry.register(tool_def)
|
|
return func
|
|
return decorator
|
|
|
|
|
|
def tool_function(
|
|
name: str,
|
|
description: str,
|
|
parameters: Dict[str, Any],
|
|
category: str = "general"
|
|
):
|
|
"""
|
|
工具装饰器的别名,提供更语义化的命名
|
|
"""
|
|
return tool(name=name, description=description, parameters=parameters, category=category)
|