Repository navigation
zh dev star guides ai tools
Tool 是大语言模型调用外部工具的能力。
from pydantic import Field
from pydantic.dataclasses import dataclass
# AstrBot v4.30.0 之前的导入方式:
# from astrbot.core.agent.run_context import ContextWrapper
# from astrbot.core.agent.tool import FunctionTool, ToolExecResult
# from astrbot.core.astr_agent_context import AstrAgentContext
from astrbot.api import FunctionTool, ToolExecResult
from astrbot.api.agents import AstrAgentContext, ContextWrapper
@dataclass
class BilibiliTool(FunctionTool[AstrAgentContext]):
name: str = "bilibili_videos" # 工具名称
description: str = "A tool to fetch Bilibili videos." # 工具描述
parameters: dict = Field(
default_factory=lambda: {
"type": "object",
"properties": {
"keywords": {
"type": "string",
"description": "Keywords to search for Bilibili videos.",
},
},
"required": ["keywords"],
}
)
async def call(
self, context: ContextWrapper[AstrAgentContext], **kwargs
) -> ToolExecResult:
return "1. 视频标题:如何使用AstrBot\n视频链接:xxxxxx"在上面定义好 Tool 之后,如果你需要实现的功能是让用户在使用 AstrBot 进行对话时自动调用该 Tool,那么你需要在插件的 init 方法中将 Tool 注册到 AstrBot 中:
class MyPlugin(Star):
def __init__(self, context: Context):
super().__init__(context)
# >= v4.5.1 使用:
self.context.add_llm_tools(BilibiliTool(), SecondTool(), ...)
# < v4.5.1 之前使用:
tool_mgr = self.context.provider_manager.llm_tools
tool_mgr.func_list.append(BilibiliTool())Warning
context.register_llm_tool() 已被弃用,请勿在新插件中使用。
如需通过该方法注册(旧插件兼容),func_args 必须是 字典列表,格式为:
func_args = [{"type": "string", "name": "arg_name", "description": "参数描述"}, ...]传入字符串列表或其他格式会导致 AttributeError: 'str' object has no attribute 'pop'。
除了上述的通过 @dataclass 定义 Tool 的方式之外,你也可以使用装饰器的方式注册 tool 到 AstrBot。请务必按照以下格式编写一个工具(包括函数注释,AstrBot 会解析该函数注释,请务必将注释格式写对):
@filter.llm_tool(name="get_weather") # 如果 name 不填,将使用函数名
async def get_weather(
self, event: AstrMessageEvent, location: str
) -> MessageEventResult:
"""获取天气信息。
Args:
location(string): 地点
"""
resp = self.get_weather_from_api(location)
yield event.plain_result("天气信息: " + resp)
在 location(string): 地点 中,location 是参数名,string 是参数类型,地点 是参数描述。
支持的参数类型有 string, number, object, boolean, array。在 v4.5.7 之后,支持对 array 类型参数指定子类型,例如 array[string]。
Warning
Args: 段是必须的,且格式不能写错。
@filter.llm_tool 装饰器通过解析函数的 docstring 来生成工具的参数 schema,不会读取函数签名中的类型注解。如果 docstring 缺少 Args: 段,或格式不符合 参数名(类型): 描述 的规范,框架生成的参数 schema 将为空,LLM 传入的参数会被静默丢弃,最终导致函数因缺少参数而报错。
此外,装饰器不支持通过 parameters=... 显式传入参数 schema,该写法会被忽略。如需手动控制 schema,请使用上方的 @dataclass + add_llm_tools() 方式。
- 首页
- 文档入口
- Top Level
- community events
- config
- deploy
- dev
- others
- platform
- 接入 OneBot v11 协议实现
- 接入钉钉 DingTalk
- 接入 Discord
- 接入 Kook
- 接入飞书
- 接入 LINE
- 接入 Matrix
- 接入 Mattermost
- 接入 Misskey 平台
- 接入 QQ 官方机器人平台
- 通过 QQ官方机器人 接入 QQ (Webhook)
- 通过 QQ官方机器人 接入 QQ (Websockets)
- 接入 Satori 协议
- 接入 server-satori (基于 Koishi)
- 接入 Slack
- 接入消息平台
- 接入 Telegram
- 接入 VoceChat
- AstrBot 接入企业微信
- 接入企业微信智能机器人平台
- AstrBot 接入微信公众平台
- 接入个人微信
- providers
- use
- Home
- Docs Entry
- Top Level
- config
- deploy
- Deploy AstrBot on 1Panel
- Deploy AstrBot on BT Panel
- Deploy AstrBot on CasaOS
- Deploy AstrBot from Source Code
- Community-Provided Deployment Methods
- Deploy via Compshare
- Deploy with AstrBot Desktop Client
- Deploy AstrBot with Docker
- Deploy AstrBot with Kubernetes
- Deploy AstrBot with AstrBot Launcher
- Other Deployments
- Script Deployment
- Installation via System Package Manager
- Preface
- dev
- AstrBot Configuration File
- API Scope–Endpoint Reference
- AstrBot HTTP API
- AstrBot Plugin Market JSON Specification
- AstrBot Plugin Market JSON Specification
- Developing a Platform Adapter
- plugin
- AI
- Agents
- Sessions and Conversations
- Models
- Other APIs
- Tools
- Documentation moved
- Text to Image
- Handling Message Events
- Plugin Configuration
- Plugin Internationalization
- Documentation moved
- Shared Bridge API
- Interactive message component
- Standalone plugin page
- Backend Web APIs
- Workbench page
- Sending Messages
- Session Control
- Minimal Example
- Plugin Storage
- AstrBot Plugin Development Guide 🌠
- Publishing Plugins to the Plugin Marketplace
- ospp
- others
- platform
- Connect OneBot v11 Protocol Implementations
- Connect to DingTalk
- Connecting to Discord
- Connect to KOOK
- Connecting to Lark
- Connecting to LINE
- Connecting to Matrix
- Connecting to Mattermost
- Connecting to Misskey Platform
- Connect QQ Official Bot
- Connect QQ via QQ Official Bot (Webhook)
- Connect QQ via QQ Official Bot (Websockets)
- Connect to Satori Protocol
- Connect server-satori (Koishi)
- Connecting to Slack
- Messaging Platforms
- Connecting to Telegram
- Connect to VoceChat
- Connect AstrBot to WeCom
- Connect to WeCom AI Bot Platform
- Connect AstrBot to WeChat Official Account Platform
- Connect Personal WeChat
- providers
- Connect 302.AI
- Agent Runners
- Built-in Agent Runner
- Connect to Coze
- Connect to Alibaba Cloud Bailian Application
- Connect to DeerFlow
- Connect to Dify
- Connect AIHubMix
- coze
- dashscope
- DeepSeek STT Integration
- DeepSeek TTS Integration
- dify
- Model input images
- Large Language Model Providers
- Connect MiraRouter
- NewAPI
- Connect PPIO Cloud
- Connect LM Studio to Use DeepSeek-R1 and Other Models
- Integrating Ollama
- Connect ShengSuanYun
- Connecting to SiliconFlow
- Connecting Model Services
- Connecting to TokenPony
- use
- Agent Execution Mode {#agent-runner}
- Agent Sandbox Environment
- astrbot sandbox
- AstrBot CLI {#cli-commands}
- Docker-based Code Interpreter
- Built-in Commands
- computer
- Context Compression
- Session Configuration Mappings (formerly Custom Rules)
- AstrBot Knowledge Base
- MCP
- AstrBot Plugins {#astrbot-star}
- Proactive Capabilities
- Skills {#anthropic-skills}
- Subagents {#agent-handsoff-and-subagent}
- Unified Webhook Mode
- Web Search
- DeepSeek Search API
- SerpApi
- WebUI