Reverse Invocation Model
This document details how plugins can reverse invoke model services within the Dify platform. It covers specific methods for reverse invoking LLM, Summary, TextEmbedding, Rerank, TTS, Speech2Text, and Moderation models. Each model invocation includes its entry point, interface parameter descriptions, practical usage code examples, and best practice recommendations for invoking models.
Reverse invoking a Model refers to the ability of a plugin to call Dify’s internal LLM capabilities, including all model types and functions within the platform, such as TTS, Rerank, etc. If you are not familiar with the basic concepts of reverse invocation, please read Reverse Invocation of Dify Services first.
However, please note that invoking a model requires passing a ModelConfig
type parameter. Its structure can be referenced in the General Specifications Definition, and this structure will have slight differences for different types of models.
For example, for LLM
type models, it also needs to include completion_params
and mode
parameters. You can manually construct this structure or use model-selector
type parameters or configurations.
Invoke LLM
Entry Point
Endpoint
Please note that if the model you are invoking does not have tool_call
capability, the tools
passed here will not take effect.
Use Case
If you want to invoke OpenAI’s gpt-4o-mini
model within a Tool
, please refer to the following example code:
Note that the query
parameter from tool_parameters
is passed in the code.
Best Practice
It is not recommended to manually construct LLMModelConfig
. Instead, allow users to select the model they want to use on the UI. In this case, you can modify the tool’s parameter list by adding a model
parameter as follows:
Please note that in this example, the scope
of model
is specified as llm
. This means the user can only select llm
type parameters. Thus, the code from the previous use case can be modified as follows:
Invoke Summary
You can request this endpoint to summarize a piece of text. It will use the system model within your current workspace to summarize the text.
Entry Point
Endpoint
text
is the text to be summarized.instruction
is the additional instruction you want to add, allowing you to summarize the text stylistically.
Invoke TextEmbedding
Entry Point
Endpoint
Invoke Rerank
Entry Point
Endpoint
Invoke TTS
Entry Point
Endpoint
Please note that the bytes
stream returned by the tts
endpoint is an mp3
audio byte stream. Each iteration returns a complete audio segment. If you want to perform more in-depth processing tasks, please choose an appropriate library.
Invoke Speech2Text
Entry Point
Endpoint
Where file
is an audio file encoded in mp3
format.
Invoke Moderation
Entry Point
Endpoint
If this endpoint returns true
, it indicates that the text
contains sensitive content.
Related Resources
- Reverse Invocation of Dify Services - Understand the fundamental concepts of reverse invocation
- Reverse Invocation of App - Learn how to invoke Apps within the platform
- Reverse Invocation of Tool - Learn how to invoke other plugins
- Model Plugin Development Guide - Learn how to develop custom model plugins
- Model Designing Rules - Understand the design principles of model plugins