mirror of
https://github.com/langgenius/dify-docs.git
synced 2026-03-27 13:28:32 +07:00
117 lines
4.4 KiB
Plaintext
117 lines
4.4 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.zh)。
|
||
|
||
当遇到以下需求时:
|
||
|
||
* 某个工具类型插件已经实现好了一个功能,但效果未达预期,需要对数据进行二次加工。
|
||
* 某个任务需要使用到爬虫,希望能够自由选择爬虫服务。
|
||
* 需要集合多个工具的返回结果,但是通过 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.zh)。
|
||
|
||
**入口**
|
||
|
||
```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.zh) - 了解反向调用的根本概念
|
||
- [反向调用 App](/plugin_dev_zh/9242-reverse-invocation-app.zh) - 了解如何调用平台内的 App
|
||
- [反向调用 Model](/plugin_dev_zh/9242-reverse-invocation-model.zh) - 了解如何调用平台内的模型能力
|
||
- [工具插件开发指南](/plugin_dev_zh/0211-getting-started-dify-tool.zh) - 学习如何开发工具插件
|
||
- [高级工具插件](/plugin_dev_zh/9223-tool.zh) - 了解 Workflow as Tool 等高级功能
|
||
|
||
{/*
|
||
Contributing Section
|
||
DO NOT edit this section!
|
||
It will be automatically generated by the script.
|
||
*/}
|
||
|
||
<CardGroup cols="2">
|
||
<Card
|
||
title="编辑此页面"
|
||
icon="pen-to-square"
|
||
href="https://github.com/langgenius/dify-docs-mintlify/edit/main/plugin_dev_zh/9242-reverse-invocation-tool.zh.mdx"
|
||
>
|
||
通过直接提交修改来帮助改进文档内容
|
||
</Card>
|
||
<Card
|
||
title="提交问题"
|
||
icon="github"
|
||
href="https://github.com/langgenius/dify-docs-mintlify/issues/new?title=文档问题%3A%20reverse-invocation-tool&body=%23%23%20问题描述%0A%3C%21--%20请简要描述您发现的问题%20--%3E%0A%0A%23%23%20页面链接%0Ahttps%3A%2F%2Fgithub.com%2Flanggenius%2Fdify-docs-mintlify%2Fblob%2Fmain%2Fplugin_dev_zh%2F9242-reverse-invocation-tool.zh.mdx%0A%0A%23%23%20建议修改%0A%3C%21--%20如果有具体的修改建议,请在此说明%20--%3E%0A%0A%3C%21--%20感谢您对文档质量的关注!%20--%3E"
|
||
>
|
||
发现错误或有改进建议?请提交问题反馈
|
||
</Card>
|
||
</CardGroup>
|