> ## Documentation Index
> Fetch the complete documentation index at: https://dify-6c0370d8-add-new-agent.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# 应用

> 本文档详细介绍了插件如何在 Dify 平台内反向调用应用服务。涵盖三种接口类型：Chat 接口（用于 Chatbot/Agent/Chatflow 应用）、工作流接口和 Completion 接口，为每种接口提供入口点、调用规范和实用代码示例。

<Note> ⚠️ 本文档由 AI 自动翻译。如有任何不准确之处，请参考[英文原版](/en/develop-plugin/features-and-specs/advanced-development/reverse-invocation-app)。</Note>

反向调用应用意味着插件可以访问 Dify 内应用的数据。此模块支持流式和非流式应用调用。如果你不熟悉反向调用的基本概念，请先阅读[反向调用 Dify 服务](/zh/develop-plugin/features-and-specs/advanced-development/reverse-invocation)。

**接口类型：**

* 对于 `Chatbot/Agent/Chatflow` 类型的应用，它们都是基于对话的应用，因此共享相同的输入和输出参数类型。因此，它们可以统一视为 **Chat 接口。**
* 对于工作流应用，它们占用单独的**工作流接口。**
* 对于 Completion（文本生成应用）应用，它们占用单独的 **Completion 接口**。

请注意，插件只允许访问插件所在工作空间内的应用。

### 调用 Chat 接口

#### **入口点**

```python theme={null}
    self.session.app.chat
```

#### **接口规范**

```python theme={null}
    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 类型的应用并直接返回结果。

```python theme={null}
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():
            # Note: The original example incorrectly called self.session.app.workflow.invoke
            # It should call self.session.app.chat.invoke for a chat app.
            # Assuming a chat app is intended here based on the section title.
            response = self.session.app.chat.invoke(
                app_id=app_id, 
                inputs={}, # Provide actual inputs as needed
                response_mode="streaming", 
                conversation_id="some-conversation-id", # Provide a conversation ID if needed
                files=[]
            )

            for data in response:
                yield f"{json.dumps(data)} <br>"

        return Response(generator(), status=200, content_type="text/html")
```

### 调用工作流接口

#### **入口点**

```python theme={null}
    self.session.app.workflow
```

#### **接口规范**

```python theme={null}
    def invoke(
        self,
        app_id: str,
        inputs: dict,
        response_mode: Literal["streaming", "blocking"],
        files: list,
    ) -> Generator[dict, None, None] | dict:
        pass
```

### 调用 Completion 接口

#### **入口点**

```python theme={null}
    self.session.app.completion
```

**接口规范**

```python theme={null}
    def invoke(
        self,
        app_id: str,
        inputs: dict,
        response_mode: Literal["streaming", "blocking"],
        files: list,
    ) -> Generator[dict, None, None] | dict:
        pass
```

## 相关资源

* [反向调用 Dify 服务](/zh/develop-plugin/features-and-specs/advanced-development/reverse-invocation) - 了解反向调用的基本概念
* [反向调用模型](/zh/develop-plugin/features-and-specs/advanced-development/reverse-invocation-model) - 学习如何调用平台内的模型能力
* [反向调用工具](/zh/develop-plugin/features-and-specs/advanced-development/reverse-invocation-tool) - 学习如何调用其他插件
* [开发 Slack Bot 插件](/zh/develop-plugin/dev-guides-and-walkthroughs/develop-a-slack-bot-plugin) - 使用反向调用的实际应用案例
* [开发扩展插件](/zh/develop-plugin/dev-guides-and-walkthroughs/endpoint) - 学习如何开发扩展插件

***

[编辑此页面](https://github.com/langgenius/dify-docs/edit/main/en/develop-plugin/features-and-specs/advanced-development/reverse-invocation-app.mdx) | [报告问题](https://github.com/langgenius/dify-docs/issues/new?template=docs.yml)
