> ## Documentation Index
> Fetch the complete documentation index at: https://docs.dify.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# 应用

> 在插件中调用 Dify 应用的 Chat、Workflow 和 Completion 接口

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

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

**接口类型**：

* **Chat 接口**：`Chatbot`、`Agent` 和 `Chatflow` 应用都是基于对话的应用，输入和输出参数类型相同，因此共用此接口。
* **Workflow 接口**：供 Workflow 应用使用。
* **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():
            response = self.session.app.chat.invoke(
                app_id=app_id,
                inputs={},
                response_mode="streaming",
                conversation_id="some-conversation-id",
                files=[],
            )

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

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

## 调用 Workflow 接口

### 入口点

```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) - 学习如何开发扩展插件
