Skip to content

Commit 531febb

Browse files
committed
feat: update tool definition examples in plugin.md for clarity and best practices
1 parent 72cff88 commit 531febb

File tree

1 file changed

+41
-26
lines changed

1 file changed

+41
-26
lines changed

dev/star/plugin.md

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1145,41 +1145,56 @@ class STTProvider(AbstractProvider):
11451145

11461146
函数工具给了大语言模型调用外部工具的能力。在 AstrBot 中,函数工具有多种定义方式。
11471147

1148-
##### 以类的形式
1148+
##### 以类的形式(推荐)
11491149

1150-
这是最灵活的定义形式。
1150+
推荐在插件目录下新建 `tools` 文件夹,然后在其中编写工具类:
1151+
1152+
`tools/search.py`:
11511153

11521154
```py
1153-
from astrbot.api import ToolSet, FunctionTool
1154-
from dataclasses import dataclass, field
1155+
from astrbot.api import FunctionTool
11551156
from astrbot.api.event import AstrMessageEvent
1157+
from dataclasses import dataclass, field
11561158

11571159
@dataclass
1158-
class SearchTool(FunctionTool):
1159-
name: str = "get_current_weather" # tool 的名称
1160-
description: str = "Get the current weather in a given location." # tool 的描述
1161-
parameters: dict = field(default_factory=lambda: {
1162-
"type": "object",
1163-
"properties": {
1164-
"location": {
1165-
"type": "string",
1166-
"description": "The city and state, e.g. San Francisco, CA"
1160+
class HelloWorldTool(FunctionTool):
1161+
name: str = "hello_world"
1162+
description: str = "Say hello to the world."
1163+
parameters: dict = field(
1164+
default_factory=lambda: {
1165+
"type": "object",
1166+
"properties": {
1167+
"greeting": {
1168+
"type": "string",
1169+
"description": "The greeting message.",
1170+
},
11671171
},
1168-
"unit": {
1169-
"type": "string",
1170-
"enum": ["celsius", "fahrenheit"]
1171-
}
1172-
},
1173-
"required": ["location"]
1174-
}) # tool 的参数定义
1175-
1176-
async def run(self, event: AstrMessageEvent, location: str, unit: str):
1177-
# Your implementation here
1178-
...
1172+
"required": ["greeting"],
1173+
}
1174+
)
1175+
1176+
async def run(
1177+
self,
1178+
event: AstrMessageEvent,
1179+
greeting: str,
1180+
):
1181+
return f"{greeting}, World!" # 也支持 mcp.types.CallToolResult 类型
1182+
```
1183+
1184+
要将上述工具注册到 AstrBot,可以在插件主文件的 `__init__.py` 中添加以下代码:
1185+
1186+
```py
1187+
from .tools.search import SearchTool
11791188

1180-
tool = SearchTool()
1181-
tool_set = ToolSet([tool])
1189+
class MyPlugin(Star):
1190+
def __init__(self, context: Context):
1191+
super().__init__(context)
1192+
# >= v4.5.1 使用:
1193+
self.context.add_llm_tools(HelloWorldTool(), SecondTool(), ...)
11821194

1195+
# < v4.5.1 之前使用:
1196+
tool_mgr = self.context.provider_manager.llm_tools
1197+
tool_mgr.func_list.append(HelloWorldTool())
11831198
```
11841199

11851200
##### 以装饰器的形式

0 commit comments

Comments
 (0)