Skip to main content
⚠️ 本文档由 AI 自动翻译。如有任何不准确之处,请参考英文原版
反向调用应用意味着插件可以访问 Dify 内应用的数据。此模块支持流式和非流式应用调用。如果你不熟悉反向调用的基本概念,请先阅读反向调用 Dify 服务 接口类型:
  • 对于 Chatbot/Agent/Chatflow 类型的应用,它们都是基于对话的应用,因此共享相同的输入和输出参数类型。因此,它们可以统一视为 Chat 接口。
  • 对于工作流应用,它们占用单独的工作流接口。
  • 对于 Completion(文本生成应用)应用,它们占用单独的 Completion 接口
请注意,插件只允许访问插件所在工作空间内的应用。

调用 Chat 接口

入口点

    self.session.app.chat

接口规范

    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_modestreaming 时,此接口将直接返回 Generator[dict]。否则,返回 dict。有关具体接口字段,请参阅 ServiceApi 的返回结果。

使用场景

我们可以在 Endpoint 内调用 Chat 类型的应用并直接返回结果。
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")

调用工作流接口

入口点

    self.session.app.workflow

接口规范

    def invoke(
        self,
        app_id: str,
        inputs: dict,
        response_mode: Literal["streaming", "blocking"],
        files: list,
    ) -> Generator[dict, None, None] | dict:
        pass

调用 Completion 接口

入口点

    self.session.app.completion
接口规范
    def invoke(
        self,
        app_id: str,
        inputs: dict,
        response_mode: Literal["streaming", "blocking"],
        files: list,
    ) -> Generator[dict, None, None] | dict:
        pass

相关资源


编辑此页面 | 报告问题