mirror of
https://github.com/langgenius/dify-docs.git
synced 2026-03-27 13:28:32 +07:00
* move files & renames * rename files and doc entries * sync develop plugin files * update group label translations * some cleanups * update configs * update links * add remote debug doc * delete redundant slashes and unnecessary notes * update ja and zh links --------- Co-authored-by: Riskey <riskey47@dify.ai>
145 lines
4.9 KiB
Plaintext
145 lines
4.9 KiB
Plaintext
---
|
|
dimensions:
|
|
type:
|
|
primary: implementation
|
|
detail: advanced
|
|
level: intermediate
|
|
standard_title: Reverse Invocation App
|
|
language: en
|
|
title: App
|
|
description: This document details how plugins can reverse invoke App services within
|
|
the Dify platform. It covers three types of interfaces Chat interface (for Chatbot/Agent/Chatflow
|
|
applications), Workflow interface, and Completion interface, providing entry points,
|
|
invocation specifications, and practical code examples for each.
|
|
---
|
|
|
|
Reverse invoking an App means that a plugin can access data from an App within Dify. This module supports both streaming and non-streaming App calls. If you are unfamiliar with the basic concepts of reverse invocation, please first read [Reverse Invocation of Dify Services](/en/develop-plugin/features-and-specs/advanced-development/reverse-invocation).
|
|
|
|
**Interface Types:**
|
|
|
|
* For `Chatbot/Agent/Chatflow` type applications, they are all chat-based applications and thus share the same input and output parameter types. Therefore, they can be uniformly treated as the **Chat Interface.**
|
|
* For Workflow applications, they occupy a separate **Workflow Interface.**
|
|
* For Completion (text generation application) applications, they occupy a separate **Completion Interface**.
|
|
|
|
Please note that plugins are only allowed to access Apps within the Workspace where the plugin resides.
|
|
|
|
### Calling the Chat Interface
|
|
|
|
#### **Entry Point**
|
|
|
|
```python
|
|
self.session.app.chat
|
|
```
|
|
|
|
#### **Interface Specification**
|
|
|
|
```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
|
|
```
|
|
|
|
When `response_mode` is `streaming`, this interface will directly return `Generator[dict]`. Otherwise, it returns `dict`. For specific interface fields, please refer to the return results of `ServiceApi`.
|
|
|
|
#### **Use Case**
|
|
|
|
We can call a Chat type App within an `Endpoint` and return the result directly.
|
|
|
|
```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():
|
|
# 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")
|
|
```
|
|
|
|
### Calling the Workflow Interface
|
|
|
|
#### **Entry Point**
|
|
|
|
```python
|
|
self.session.app.workflow
|
|
```
|
|
|
|
#### **Interface Specification**
|
|
|
|
```python
|
|
def invoke(
|
|
self,
|
|
app_id: str,
|
|
inputs: dict,
|
|
response_mode: Literal["streaming", "blocking"],
|
|
files: list,
|
|
) -> Generator[dict, None, None] | dict:
|
|
pass
|
|
```
|
|
|
|
### Calling the Completion Interface
|
|
|
|
#### **Entry Point**
|
|
|
|
```python
|
|
self.session.app.completion
|
|
```
|
|
|
|
**Interface Specification**
|
|
|
|
```python
|
|
def invoke(
|
|
self,
|
|
app_id: str,
|
|
inputs: dict,
|
|
response_mode: Literal["streaming", "blocking"],
|
|
files: list,
|
|
) -> Generator[dict, None, None] | dict:
|
|
pass
|
|
```
|
|
|
|
## Related Resources
|
|
|
|
- [Reverse Invocation of Dify Services](/en/develop-plugin/features-and-specs/advanced-development/reverse-invocation) - Understand the fundamental concepts of reverse invocation
|
|
- [Reverse Invocation Model](/en/develop-plugin/features-and-specs/advanced-development/reverse-invocation-model) - Learn how to call model capabilities within the platform
|
|
- [Reverse Invocation Tool](/en/develop-plugin/features-and-specs/advanced-development/reverse-invocation-tool) - Learn how to call other plugins
|
|
- [Develop a Slack Bot Plugin](/en/develop-plugin/dev-guides-and-walkthroughs/develop-a-slack-bot-plugin) - A practical application case using reverse invocation
|
|
- [Develop Extension Plugins](/en/develop-plugin/dev-guides-and-walkthroughs/endpoint) - Learn how to develop extension plugins
|
|
|
|
{/*
|
|
Contributing Section
|
|
DO NOT edit this section!
|
|
It will be automatically generated by the script.
|
|
*/}
|
|
|
|
---
|
|
|
|
[Edit this page](https://github.com/langgenius/dify-docs/edit/main/en/develop-plugin/features-and-specs/advanced-development/reverse-invocation-app.mdx) | [Report an issue](https://github.com/langgenius/dify-docs/issues/new?template=docs.yml)
|
|
|