基于 Bytebot Tool Definition 模式实现的 LLM 函数调用工具定义系统。
Version: 1.0.0
Features: JSON Schema 定义、多 LLM 格式转换、工具注册中心、参数验证
让 OpenClaw 能够:
python
from llm_tools import ToolRegistry, Tool
# 创建工具注册表
registry = ToolRegistry()
# 定义工具
@registry.register(
name="get_weather",
description="Get current weather for a location",
parameters={
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "City name"
},
"unit": {
"type": "string",
"enum": ["celsius", "fahrenheit"],
"default": "celsius"
}
},
"required": ["location"]
}
)
def get_weather(location: str, unit: str = "celsius"):
return {"temperature": 22, "unit": unit}
python
# OpenAI format
openai_tools = registry.to_openai()
# Anthropic format
anthropic_tools = registry.to_anthropic()
# Google Gemini format
gemini_tools = registry.to_gemini()
# Ollama format
ollama_tools = registry.to_ollama()
python
# 验证参数
is_valid, error = registry.validate_call(
"get_weather",
{"location": "Beijing", "unit": "celsius"}
)
# 执行工具
result = registry.execute("get_weather", {"location": "Beijing"})
bash
# 从 JSON 定义转换
python3 scripts/main.py convert --input tools.json --format openai
python3 scripts/main.py convert --input tools.json --format anthropic
# 验证工具定义
python3 scripts/main.py validate --input tools.json
# 列出所有工具
python3 scripts/main.py list --input tools.json
json
{
"tools": [
{
"name": "search_web",
"description": "Search the web for information",
"parameters": {
"type": "object",
"properties": {
"query": {
"type": "string",
"description": "Search query"
},
"limit": {
"type": "integer",
"default": 10
}
},
"required": ["query"]
}
}
]
}
ProviderFormatFeaturesOpenAIFunction Callingtools / functionsAnthropicTool Usecomputer_* 命名空间GoogleFunction Callingfunction_declarationsOllamaToolsNative tool supportMistralFunction CallingOpenAI-compatibleCohereTool UseCustom format
bash
pip3 install -r requirements.txt
python
from llm_tools import ToolRegistry
registry = ToolRegistry()
# 注册工具
registry.register_tool(tool_definition)
# 装饰器方式
@registry.register(name="...", description="...", parameters={...})
def my_tool():
pass
# 批量注册
registry.register_from_dict({"tools": [...]})
registry.register_from_json_file("tools.json")
# 导出格式
openai_format = registry.to_openai()
anthropic_format = registry.to_anthropic()
gemini_format = registry.to_gemini()
ollama_format = registry.to_ollama()
# 验证和执行
registry.validate_call(name, arguments)
registry.execute(name, arguments)
python
from llm_tools import Tool
tool = Tool(
name="calculate",
description="Perform mathematical calculation",
parameters={
"type": "object",
"properties": {
"expression": {"type": "string"}
},
"required": ["expression"]
},
handler=lambda expr: eval(expr) # 可选
)
在 Skill 中使用:
python
from llm_tools import ToolRegistry
class MySkill:
def __init__(self):
self.tools = ToolRegistry()
@self.tools.register(name="read_file", ...)
def read_file(path: str):
return Path(path).read_text()
def get_llm_tools(self, provider: str):
if provider == "openai":
return self.tools.to_openai()
elif provider == "anthropic":
return self.tools.to_anthropic()
text
llm-tools/
├── SKILL.md
├── requirements.txt
├── lib/
│ ├── __init__.py
│ ├── registry.py # ToolRegistry 核心
│ ├── tool.py # Tool 类定义
│ ├── formats/
│ │ ├── __init__.py
│ │ ├── openai.py # OpenAI 格式转换
│ │ ├── anthropic.py # Anthropic 格式转换
│ │ ├── gemini.py # Google Gemini 格式
│ │ └── ollama.py # Ollama 格式
│ └── validators.py # JSON Schema 验证
├── scripts/
│ └── main.py # CLI 入口
└── examples/
├── tools.json # 示例工具定义
└── registry_example.py # 注册表示例
MIT License - 基于 Bytebot Tool Definition 模式实现