mirror of
https://github.com/langgenius/dify-docs.git
synced 2026-03-27 13:28:32 +07:00
105 lines
3.6 KiB
Plaintext
105 lines
3.6 KiB
Plaintext
---
|
||
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 的 ID,tool\_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?template=docs.yml)
|
||
|