mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-10-29 20:05:25 +08:00
docs: add tools API, tools-selector config type, and OpenAPI tools endpoints
- Plugin SDK APIs: list_tools, get_tool_detail, call_tool (zh/en/ja)
- Config type: tools-selector for multi-select tool picker (zh/en/ja)
- OpenAPI: GET /api/v1/tools, GET /api/v1/tools/{tool_name} (zh/en/ja)
This commit is contained in:
@@ -249,6 +249,87 @@ parsers = await self.plugin.list_parsers(mime_type="application/pdf")
|
||||
# 返回的每项包含 plugin_id、plugin_author、plugin_name、name、description、supported_mime_types
|
||||
```
|
||||
|
||||
### 获取所有可用工具
|
||||
|
||||
列出当前 LangBot 实例中所有可用的工具(包括插件工具和 MCP 工具)。
|
||||
|
||||
```python
|
||||
async def list_tools(self) -> list[dict[str, Any]]:
|
||||
"""List all available tools
|
||||
|
||||
Returns:
|
||||
工具列表,每个元素包含:
|
||||
- name: 工具名称
|
||||
- label: 工具显示名称(多语言)
|
||||
- description: 工具描述(多语言)
|
||||
- icon: 工具图标
|
||||
- spec: 工具规格(包含 llm_prompt 和 parameters)
|
||||
"""
|
||||
|
||||
# 调用示例
|
||||
tools = await self.plugin.list_tools()
|
||||
for tool in tools:
|
||||
print(f"Tool: {tool['name']}")
|
||||
```
|
||||
|
||||
### 获取工具详情
|
||||
|
||||
获取特定工具的详细信息。
|
||||
|
||||
```python
|
||||
async def get_tool_detail(self, tool_name: str) -> dict[str, Any]:
|
||||
"""Get detailed information about a specific tool
|
||||
|
||||
Args:
|
||||
tool_name: 工具名称(metadata.name,例如 "get_weather_alerts")
|
||||
|
||||
Returns:
|
||||
工具详情 dict,包含 name、label、description、spec(含 parameters 和 llm_prompt)
|
||||
"""
|
||||
|
||||
# 调用示例
|
||||
detail = await self.plugin.get_tool_detail("get_weather_alerts")
|
||||
print(detail['spec']['parameters'])
|
||||
```
|
||||
|
||||
### 调用工具
|
||||
|
||||
调用指定的工具。
|
||||
|
||||
```python
|
||||
async def call_tool(
|
||||
self,
|
||||
tool_name: str,
|
||||
parameters: dict[str, Any],
|
||||
session: dict[str, Any],
|
||||
query_id: int,
|
||||
) -> dict[str, Any]:
|
||||
"""Call a specific tool
|
||||
|
||||
Args:
|
||||
tool_name: 工具名称(metadata.name)
|
||||
parameters: 工具参数
|
||||
session: 会话信息
|
||||
query_id: 查询 ID
|
||||
|
||||
Returns:
|
||||
工具返回的结果 dict
|
||||
"""
|
||||
|
||||
# 调用示例
|
||||
result = await self.plugin.call_tool(
|
||||
tool_name="get_weather_alerts",
|
||||
parameters={"state": "CA"},
|
||||
session={},
|
||||
query_id=0,
|
||||
)
|
||||
print(result)
|
||||
```
|
||||
|
||||
<Tip>
|
||||
工具名称使用 `metadata.name`(如 `echo_tool`、`get_weather_alerts`),不需要包含作者或插件名前缀。
|
||||
</Tip>
|
||||
|
||||
### 设置插件持久化数据
|
||||
|
||||
持久化存储插件数据。通过这个接口存储的数据,仅可被该插件访问。值需要自行转换为字节。
|
||||
|
||||
@@ -50,7 +50,7 @@ execution:
|
||||
spec:
|
||||
config:
|
||||
- name: github_token # 必填;配置项名称,用于在插件中获取
|
||||
type: string # 必填;配置项类型,支持 string, integer, float, boolean, select, prompt-editor, llm-model-selector, bot-selector 等
|
||||
type: string # 必填;配置项类型,支持 string, integer, float, boolean, select, prompt-editor, llm-model-selector, bot-selector, tools-selector 等
|
||||
label: # 必填;配置项显示名称,支持多语言。语言代码采用 RFC 4646 标准。
|
||||
en_US: Github Token
|
||||
zh_Hans: Github Token
|
||||
@@ -274,6 +274,33 @@ bot_uuid = self.get_config()['bot']
|
||||
print(bot_uuid) # 输出: '550e8400-e29b-41d4-a716-446655440000'
|
||||
```
|
||||
|
||||
### type: tools-selector
|
||||
|
||||
工具选择器。会展示一个多选工具选择器,可选择已注册的工具(插件工具和 MCP 工具),最终结果表示为工具名称数组。
|
||||
|
||||
```yaml
|
||||
- name: selected_tools
|
||||
type: tools-selector
|
||||
...
|
||||
```
|
||||
|
||||
在插件中使用选中的工具名称:
|
||||
|
||||
```python
|
||||
# 从配置中获取选中的工具名称列表
|
||||
selected_tools = self.get_config()['selected_tools']
|
||||
print(selected_tools) # 输出: ['get_weather_alerts', 'web_search']
|
||||
|
||||
# 调用选中的工具
|
||||
for tool_name in selected_tools:
|
||||
result = await self.plugin.call_tool(
|
||||
tool_name=tool_name,
|
||||
parameters={"query": "hello"},
|
||||
session={},
|
||||
query_id=0,
|
||||
)
|
||||
```
|
||||
|
||||
## 接下来做什么
|
||||
|
||||
该教程将指引您逐步完善插件功能。
|
||||
|
||||
Reference in New Issue
Block a user