Files
dify-docs/plugin-dev-zh/9242-reverse-invocation-tool.mdx
2025-06-03 11:06:43 +08:00

105 lines
4.0 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
dimensions:
type:
primary: implementation
detail: advanced
level: intermediate
standard_title: Reverse Invocation Tool
language: zh
title: Tool
description: 本文档详细介绍了插件如何反向调用Dify平台中的工具服务。内容涉及三种不同类型的工具调用方法调用已安装的工具Built-in Tool、调用Workflow
as Tool以及调用自定义工具Custom Tool。每种调用方式都配有对应的入口和接口参数说明。
---
反向调用 Tool 指的是插件能够调用 Dify 平台内的其它工具类型插件。如果你对反向调用的基本概念还不熟悉,请先阅读[反向调用 Dify 服务](/plugin-dev-zh/9241-reverse-invocation)。
当遇到以下需求时:
* 某个工具类型插件已经实现好了一个功能,但效果未达预期,需要对数据进行二次加工。
* 某个任务需要使用到爬虫,希望能够自由选择爬虫服务。
* 需要集合多个工具的返回结果,但是通过 Workflow 应用不好处理。
此时需要在插件中调用其他已经实现好的工具,该工具可能是市场中的某个工具插件,可能是自主构建的 Workflow as a Tool亦或是自定义工具。
上述需求可以通过调用插件的 `self.session.tool` 字段来实现。
### 调用已安装的工具
允许插件调用已安装在当前 Workspace 内的各个工具,其中也包含其它工具类型的插件。
**入口**
```python
self.session.tool
```
**接口**
```python
def invoke_builtin_tool(
self, provider: str, tool_name: str, parameters: dict[str, Any]
) -> Generator[ToolInvokeMessage, None, None]:
pass
```
其中 provider 为 plugin 的 ID 加上工具供应商名称,格式形如 `langgenius/google/google`tool\_name 为具体的工具名称,`parameters` 为最后传递给该工具的参数。
### 调用 Workflow as Tool
如需了解关于 Workflow as Tool 的更多说明,请参考[工具插件文档](/plugin-dev-zh/9223-tool)。
**入口**
```python
self.session.tool
```
**接口**
```python
def invoke_workflow_tool(
self, provider: str, tool_name: str, parameters: dict[str, Any]
) -> Generator[ToolInvokeMessage, None, None]:
pass
```
此时的 provider 为该 tool 的 IDtool\_name 在创建该 tool 的时候会要求填写。
### 调用 Custom Tool
**入口**
```python
self.session.tool
```
**接口**
```python
def invoke_api_tool(
self, provider: str, tool_name: str, parameters: dict[str, Any]
) -> Generator[ToolInvokeMessage, None, None]:
pass
```
此时的 `provider` 为该 tool 的 ID`tool_name` 为 OpenAPI 中的 `operation_id`,若不存在,即为 Dify 自动生成的 `tool_name`,可以在工具管理页中看到具体的名称。
## 相关资源
- [反向调用 Dify 服务](/plugin-dev-zh/9241-reverse-invocation) - 了解反向调用的根本概念
- [反向调用 App](/plugin-dev-zh/9242-reverse-invocation-app) - 了解如何调用平台内的 App
- [反向调用 Model](/plugin-dev-zh/9242-reverse-invocation-model) - 了解如何调用平台内的模型能力
- [工具插件开发指南](/plugin-dev-zh/0211-getting-started-dify-tool) - 学习如何开发工具插件
- [高级工具插件](/plugin-dev-zh/9223-tool) - 了解 Workflow as Tool 等高级功能
{/*
Contributing Section
DO NOT edit this section!
It will be automatically generated by the script.
*/}
---
[编辑此页面](https://github.com/langgenius/dify-docs/edit/main/plugin-dev-zh/9242-reverse-invocation-tool.mdx) | [提交问题](https://github.com/langgenius/dify-docs/issues/new?title=文档问题%3A%20reverse-invocation-t&body=%23%23%20问题描述%0A%3C%21--%20请简要描述您发现的问题%20--%3E%0A%0A%23%23%20页面链接%0Ahttps%3A%2F%2Fgithub.com%2Flanggenius%2Fdify-docs%2Fblob%2Fmain%2Fplugin-dev-zh%2F9242-reverse-invocation-tool.mdx%0A%0A%23%23%20建议修改%0A%3C%21--%20如果有具体的修改建议请在此说明%20--%3E%0A%0A%3C%21--%20感谢您对文档质量的关注%20--%3E)