mirror of
https://github.com/langgenius/dify-docs.git
synced 2026-03-27 13:28:32 +07:00
139 lines
4.5 KiB
Plaintext
139 lines
4.5 KiB
Plaintext
---
|
||
dimensions:
|
||
type:
|
||
primary: implementation
|
||
detail: advanced
|
||
level: intermediate
|
||
standard_title: Reverse Invocation App
|
||
language: zh
|
||
title: App
|
||
description: 本文档详细介绍了插件如何反向调用Dify平台中的App服务。内容包括三种不同类型的接口:聊天接口(适用于Chatbot/Agent/Chatflow类型应用)、Workflow接口和Completion接口,并提供了每种接口的入口方式、调用规范以及实际的调用示例代码。
|
||
---
|
||
|
||
反向调用 App 指的是插件能够访问 Dify 中的 App 数据。该模块同时支持流式与非流式的 App 调用。如果你对反向调用的基本概念还不熟悉,请先阅读[反向调用 Dify 服务](/plugin-dev-zh/9241-reverse-invocation)。
|
||
|
||
**接口类型:**
|
||
|
||
* 对于 `Chatbot/Agent/Chatflow` 类型应用而言,它们都属于聊天类型的应用,因此拥有相同类型的输入参数和输出参数,因此可被统一视为**聊天接口。**
|
||
* 对于 Workflow 应用而言,它单独占据一个 **Workflow 接口。**
|
||
* 对于 Completion(文本生成应用)应用而言,它单独占据一个 **Completion 接口**。
|
||
|
||
请注意,插件仅允许访问插件所在的 Workspace 中的 App。
|
||
|
||
### 调用聊天接口
|
||
|
||
#### **入口**
|
||
|
||
```python
|
||
self.session.app.chat
|
||
```
|
||
|
||
#### **接口规范**
|
||
|
||
```python
|
||
def invoke(
|
||
self,
|
||
app_id: str,
|
||
inputs: dict,
|
||
response_mode: Literal["streaming", "blocking"],
|
||
conversation_id: str,
|
||
files: list,
|
||
) -> Generator[dict, None, None] | dict:
|
||
pass
|
||
```
|
||
|
||
当 `response_mode` 为 `streaming` 时,该接口将直接返回 `Generator[dict]`,否则直接返回 `dict`,具体的接口字段请参考 `ServiceApi` 的返回结果。
|
||
|
||
#### **用例**
|
||
|
||
我们可以在一个 `Endpoint` 中调用 Chat 类型的 App,并将结果直接返回。
|
||
|
||
```python
|
||
import json
|
||
from typing import Mapping
|
||
from werkzeug import Request, Response
|
||
from dify_plugin import Endpoint
|
||
|
||
class Duck(Endpoint):
|
||
def _invoke(self, r: Request, values: Mapping, settings: Mapping) -> Response:
|
||
"""
|
||
Invokes the endpoint with the given request.
|
||
"""
|
||
app_id = values["app_id"]
|
||
|
||
def generator():
|
||
response = self.session.app.chat.invoke(
|
||
app_id=app_id,
|
||
inputs={},
|
||
response_mode="streaming",
|
||
conversation_id="", # 可选,留空则创建新对话
|
||
files=[]
|
||
)
|
||
|
||
for data in response:
|
||
yield f"{json.dumps(data)} <br>"
|
||
|
||
return Response(generator(), status=200, content_type="text/html")
|
||
```
|
||
|
||
### 调用 Workflow 接口
|
||
|
||
#### **入口**
|
||
|
||
```python
|
||
self.session.app.workflow
|
||
```
|
||
|
||
#### **接口规范**
|
||
|
||
```python
|
||
def invoke(
|
||
self,
|
||
app_id: str,
|
||
inputs: dict,
|
||
response_mode: Literal["streaming", "blocking"],
|
||
files: list,
|
||
) -> Generator[dict, None, None] | dict:
|
||
pass
|
||
```
|
||
|
||
### 调用 Completion 接口
|
||
|
||
#### **入口**
|
||
|
||
```python
|
||
self.session.app.completion
|
||
```
|
||
|
||
**接口规范**
|
||
|
||
```python
|
||
def invoke(
|
||
self,
|
||
app_id: str,
|
||
inputs: dict,
|
||
response_mode: Literal["streaming", "blocking"],
|
||
files: list,
|
||
) -> Generator[dict, None, None] | dict:
|
||
pass
|
||
```
|
||
|
||
## 相关资源
|
||
|
||
- [反向调用 Dify 服务](/plugin-dev-zh/9241-reverse-invocation) - 了解反向调用的根本概念
|
||
- [反向调用 Model](/plugin-dev-zh/9242-reverse-invocation-model) - 了解如何调用平台内的模型能力
|
||
- [反向调用 Tool](/plugin-dev-zh/9242-reverse-invocation-tool) - 了解如何调用其它插件
|
||
- [开发 Slack Bot 插件](/plugin-dev-zh/0432-develop-a-slack-bot-plugin) - 使用反向调用的实际应用案例
|
||
- [开发 Extension 插件](/plugin-dev-zh/9231-extension-plugin) - 学习如何开发扩展插件
|
||
|
||
{/*
|
||
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-app.mdx) | [提交问题](https://github.com/langgenius/dify-docs/issues/new?title=文档问题%3A%20reverse-invocation-&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-app.mdx%0A%0A%23%23%20建议修改%0A%3C%21--%20如果有具体的修改建议,请在此说明%20--%3E%0A%0A%3C%21--%20感谢您对文档质量的关注!%20--%3E)
|
||
|