Dify
English
English
  • Getting Started
    • Welcome to Dify
      • Features and Specifications
      • List of Model Providers
    • Dify Community
      • Deploy with Docker Compose
      • Start with Local Source Code
      • Deploy with aaPanel
      • Start Frontend Docker Container Separately
      • Environment Variables Explanation
      • FAQs
    • Dify Cloud
    • Dify Premium on AWS
    • Dify for Education
  • Guides
    • Model
      • Add New Provider
      • Predefined Model Integration
      • Custom Model Integration
      • Interfaces
      • Schema
      • Load Balancing
    • Application Orchestration
      • Create Application
      • Chatbot Application
        • Multiple Model Debugging
      • Agent
      • Application Toolkits
        • Moderation Tool
    • Workflow
      • Key Concepts
      • Variables
      • Node Description
        • Start
        • End
        • Answer
        • LLM
        • Knowledge Retrieval
        • Question Classifier
        • Conditional Branch IF/ELSE
        • Code Execution
        • Template
        • Doc Extractor
        • List Operator
        • Variable Aggregator
        • Variable Assigner
        • Iteration
        • Parameter Extraction
        • HTTP Request
        • Agent
        • Tools
        • Loop
      • Shortcut Key
      • Orchestrate Node
      • File Upload
      • Error Handling
        • Predefined Error Handling Logic
        • Error Type
      • Additional Features
      • Debug and Preview
        • Preview and Run
        • Step Run
        • Conversation/Run Logs
        • Checklist
        • Run History
      • Application Publishing
      • Structured Outputs
      • Bulletin: Image Upload Replaced by File Upload
    • Knowledge
      • Create Knowledge
        • 1. Import Text Data
          • 1.1 Import Data from Notion
          • 1.2 Import Data from Website
        • 2. Choose a Chunk Mode
        • 3. Select the Indexing Method and Retrieval Setting
      • Manage Knowledge
        • Maintain Documents
        • Maintain Knowledge via API
      • Metadata
      • Integrate Knowledge Base within Application
      • Retrieval Test / Citation and Attributions
      • Knowledge Request Rate Limit
      • Connect to an External Knowledge Base
      • External Knowledge API
    • Tools
      • Quick Tool Integration
      • Advanced Tool Integration
      • Tool Configuration
        • Google
        • Bing
        • SearchApi
        • StableDiffusion
        • Dall-e
        • Perplexity Search
        • AlphaVantage
        • Youtube
        • SearXNG
        • Serper
        • SiliconFlow (Flux AI Supported)
        • ComfyUI
    • Publishing
      • Publish as a Single-page Web App
        • Web App Settings
        • Text Generator Application
        • Conversation Application
      • Embedding In Websites
      • Developing with APIs
      • Re-develop Based on Frontend Templates
    • Annotation
      • Logs and Annotation
      • Annotation Reply
    • Monitoring
      • Data Analysis
      • Integrate External Ops Tools
        • Integrate LangSmith
        • Integrate Langfuse
        • Integrate Opik
    • Extension
      • API-Based Extension
        • External Data Tool
        • Deploy API Tools with Cloudflare Workers
        • Moderation
      • Code-Based Extension
        • External Data Tool
        • Moderation
    • Collaboration
      • Discover
      • Invite and Manage Members
    • Management
      • App Management
      • Team Members Management
      • Personal Account Management
      • Subscription Management
      • Version Control
  • Workshop
    • Basic
      • How to Build an AI Image Generation App
    • Intermediate
      • Build An Article Reader Using File Upload
      • Building a Smart Customer Service Bot Using a Knowledge Base
      • Generating analysis of Twitter account using Chatflow Agent
  • Community
    • Seek Support
    • Become a Contributor
    • Contributing to Dify Documentation
  • Plugins
    • Introduction
    • Quick Start
      • Install and Use Plugins
      • Develop Plugins
        • Initialize Development Tools
        • Tool Plugin
        • Model Plugin
          • Create Model Providers
          • Integrate the Predefined Model
          • Integrate the Customizable Model
        • Agent Strategy Plugin
        • Extension Plugin
        • Bundle
      • Debug Plugin
    • Manage Plugins
    • Schema Specification
      • Manifest
      • Endpoint
      • Tool
      • Agent
      • Model
        • Model Designing Rules
        • Model Schema
      • General Specifications
      • Persistent Storage
      • Reverse Invocation of the Dify Service
        • App
        • Model
        • Tool
        • Node
    • Best Practice
      • Develop a Slack Bot Plugin
      • Dify MCP Plugin Guide: Connect Zapier and Automate Email Delivery with Ease
    • Publish Plugins
      • Publish Plugins Automatically
      • Publish to Dify Marketplace
        • Plugin Developer Guidelines
        • Plugin Privacy Protection Guidelines
      • Publish to Your Personal GitHub Repository
      • Package the Plugin File and Publish it
      • Signing Plugins for Third-Party Signature Verification
    • FAQ
  • Development
    • Backend
      • DifySandbox
        • Contribution Guide
    • Models Integration
      • Integrate Open Source Models from Hugging Face
      • Integrate Open Source Models from Replicate
      • Integrate Local Models Deployed by Xinference
      • Integrate Local Models Deployed by OpenLLM
      • Integrate Local Models Deployed by LocalAI
      • Integrate Local Models Deployed by Ollama
      • Integrate Models on LiteLLM Proxy
      • Integrating with GPUStack for Local Model Deployment
      • Integrating AWS Bedrock Models (DeepSeek)
    • Migration
      • Migrating Community Edition to v1.0.0
  • Learn More
    • Use Cases
      • DeepSeek & Dify Integration Guide: Building AI Applications with Multi-Turn Reasoning
      • Private Deployment of Ollama + DeepSeek + Dify: Build Your Own AI Assistant
      • Build a Notion AI Assistant
      • Create a MidJourney Prompt Bot with Dify
      • Create an AI Chatbot with Business Data in Minutes
      • Integrating Dify Chatbot into Your Wix Website
      • How to connect with AWS Bedrock Knowledge Base?
      • Building the Dify Scheduler
      • Building an AI Thesis Slack Bot on Dify
    • Extended Reading
      • What is LLMOps?
      • Retrieval-Augmented Generation (RAG)
        • Hybrid Search
        • Re-ranking
        • Retrieval Modes
      • How to Use JSON Schema Output in Dify?
    • FAQ
      • Self-Host
      • LLM Configuration and Usage
      • Plugins
  • Policies
    • Open Source License
    • User Agreement
      • Terms of Service
      • Privacy Policy
      • Get Compliance Report
  • Features
    • Workflow
Powered by GitBook
On this page
  • Model Providers
  • Models
  • Large Language Model (LLM)
  • Text Embedding
  • Rerank
  • Speech2Text
  • Text2Speech
  • Moderation
  1. Plugins
  2. Schema Specification
  3. Model

Model Schema

The endpoint methods and parameter descriptions that need to be implemented by the supplier and each model type are described here.

Model Providers

Inherit from the __base.model_provider.ModelProvider base class, implement the following endpoint:

Provider Credentials Validation

def validate_provider_credentials(self, credentials: dict) -> None:
    """
    Validate provider credentials
    You can choose any validate_credentials method of model type or implement validate method by yourself,
    such as: get model list api

    if validate failed, raise exception

    :param credentials: provider credentials, credentials form defined in `provider_credential_schema`.
    """

Credentials (object): Credential information Credential parameters are defined by the provider's YAML configuration file's provider_credential_schema, such as passing in api_key. If validation fails, throw the errors.validate.CredentialsValidateFailedError error.

Note: Predefined models must fully implement this interface, while custom model providers can implement it simply as follows:

class XinferenceProvider(Provider):
    def validate_provider_credentials(self, credentials: dict) -> None:
        pass

Models

Models are divided into 5 different model types, each inheriting from different base classes and requiring implementation of different methods.

Common Interfaces

All models must uniformly implement the following 2 methods:

Model Credential Validation

Similar to provider credential validation, this is specifically for validating individual models.

def validate_credentials(self, model: str, credentials: dict) -> None:
    """
    Validate model credentials

    :param model: model name
    :param credentials: model credentials
    :return:
    """

Parameters:

  • model (string): Model name

  • credentials (object): Credential information Credential parameters are defined by the provider's YAML configuration file's provider_credential_schema or model_credential_schema, such as passing in api_key. If validation fails, throw the errors.validate.CredentialsValidateFailedError error.

Invocation Exception Error Mapping

When a model invocation encounters an exception, it needs to be mapped to the Runtime-specified InvokeError type to help Dify handle different errors accordingly.

Runtime Errors:

  • InvokeConnectionError: Invocation connection error

  • InvokeServerUnavailableError: Invocation service unavailable

  • InvokeRateLimitError: Invocation rate limit reached

  • InvokeAuthorizationError: Invocation authentication failed

  • InvokeBadRequestError: Incorrect invocation parameters

@property
def _invoke_error_mapping(self) -> dict[type[InvokeError], list[type[Exception]]]:
    """
    Map model invoke error to unified error
    The key is the error type thrown to the caller
    The value is the error type thrown by the model,
    which needs to be converted into a unified error type for the caller.

    :return: Invoke error mapping
    """

You can also directly throw corresponding Errors and define them so that in subsequent calls, you can directly throw InvokeConnectionError and other exceptions.

Large Language Model (LLM)

Inherit from __base.large_language_model.LargeLanguageModel base class, implement the following interfaces:

LLM Invocation

Implement the core method for LLM invocation, supporting both streaming and synchronous returns.

def _invoke(self, model: str, credentials: dict,
            prompt_messages: list[PromptMessage], model_parameters: dict,
            tools: Optional[list[PromptMessageTool]] = None, stop: Optional[list[str]] = None,
            stream: bool = True, user: Optional[str] = None) \
        -> Union[LLMResult, Generator]:
    """
    Invoke large language model

    :param model: model name
    :param credentials: model credentials
    :param prompt_messages: prompt messages
    :param model_parameters: model parameters
    :param tools: tools for tool calling
    :param stop: stop words
    :param stream: is stream response
    :param user: unique user id
    :return: full response or stream response chunk generator result
    """

Parameters:

  • model (string): Model name

  • credentials (object): Credential information Credential parameters are defined by the provider's YAML configuration file's provider_credential_schema or model_credential_schema, such as passing in api_key

  • prompt_messages (array[PromptMessage]): Prompt list

    • For Completion-type models, only one UserPromptMessage element needs to be passed

    • For Chat-type models, a list of SystemPromptMessage, UserPromptMessage, AssistantPromptMessage, ToolPromptMessage elements needs to be passed according to message type

  • model_parameters (object): Model parameters defined by the model's YAML configuration's parameter_rules

  • tools (array[PromptMessageTool]) [optional]: Tool list, equivalent to function calling functions

  • stop (array[string]) [optional]: Stop sequences. Model output will stop before the defined string

  • stream (bool): Whether to stream output, default True. Streaming returns Generator[LLMResultChunk], non-streaming returns LLMResult

  • user (string) [optional]: Unique user identifier to help providers monitor and detect abuse

Return:

  • Streaming returns Generator[LLMResultChunk]

  • Non-streaming returns LLMResult

Pre-calculate Input Tokens

If the model does not provide a pre-calculate tokens interface, directly return 0.

def get_num_tokens(self, model: str, credentials: dict, prompt_messages: list[PromptMessage],
                   tools: Optional[list[PromptMessageTool]] = None) -> int:
    """
    Get number of tokens for given prompt messages

    :param model: model name
    :param credentials: model credentials
    :param prompt_messages: prompt messages
    :param tools: tools for tool calling
    :return:
    """

Optional: Get Custom Model Rules

def get_customizable_model_schema(self, model: str, credentials: dict) -> Optional[AIModelEntity]:
    """
    Get customizable model schema

    :param model: model name
    :param credentials: model credentials
    :return: model schema
    """

When the vendor supports adding custom LLMs, this method can be implemented to make the model rules available to the custom model, returning None by default.

Text Embedding

Inherit from __base.text_embedding_model.TextEmbeddingModel base class, implement the following interfaces:

Embedding Invocation

def _invoke(self, model: str, credentials: dict,
            texts: list[str], user: Optional[str] = None) \
        -> TextEmbeddingResult:
    """
    Invoke large language model

    :param model: model name
    :param credentials: model credentials
    :param texts: texts to embed
    :param user: unique user id
    :return: embeddings result
    """

Parameters:

  • model (string): Model name

  • credentials (object): Credential information Credential parameters are defined by the provider's YAML configuration file's provider_credential_schema or model_credential_schema

  • texts (array[string]): Text list, can be processed in batch

  • user (string) [optional]: Unique user identifier to help providers monitor and detect abuse

Return:

  • TextEmbeddingResult entity

Pre-calculate Tokens

def get_num_tokens(self, model: str, credentials: dict, texts: list[str]) -> int:
    """
    Get number of tokens for given prompt messages

    :param model: model name
    :param credentials: model credentials
    :param texts: texts to embed
    :return:
    """

Similar to LargeLanguageModel, this interface needs to select an appropriate tokenizer based on the model. If the model does not provide a tokenizer, it can use the _get_num_tokens_by_gpt2(text: str) method in the AIModel base class.

Rerank

Inherit from __base.rerank_model.RerankModel base class, implement the following interfaces:

Rerank Invocation

def _invoke(self, model: str, credentials: dict,
            query: str, docs: list[str], score_threshold: Optional[float] = None, top_n: Optional[int] = None,
            user: Optional[str] = None) \
        -> RerankResult:
    """
    Invoke rerank model

    :param model: model name
    :param credentials: model credentials
    :param query: search query
    :param docs: docs for reranking
    :param score_threshold: score threshold
    :param top_n: top n
    :param user: unique user id
    :return: rerank result
    """

Parameters:

  • model (string): Model name

  • credentials (object): Credential information

  • query (string): Search query content

  • docs (array[string]): List of segments to be re-ranked

  • score_threshold (float) [optional]: Score threshold

  • top_n (int) [optional]: Take top n segments

  • user (string) [optional]: Unique user identifier to help providers monitor and detect abuse

Return:

  • RerankResult entity

Speech2Text

Inherit from __base.speech2text_model.Speech2TextModel base class, implement the following interfaces:

Invoke Invocation

def _invoke(self, model: str, credentials: dict,
            file: IO[bytes], user: Optional[str] = None) \
        -> str:
    """
    Invoke large language model

    :param model: model name
    :param credentials: model credentials
    :param file: audio file
    :param user: unique user id
    :return: text for given audio file
    """        

Parameters:

  • model (string): Model name

  • credentials (object): Credential information

  • file (File): File stream

  • user (string) [optional]: Unique user identifier to help providers monitor and detect abuse

Return:

  • Converted text string from speech

Text2Speech

Inherit from __base.text2speech_model.Text2SpeechModel base class, implement the following interfaces:

Invoke Invocation

def _invoke(self, model: str, credentials: dict, content_text: str, streaming: bool, user: Optional[str] = None):
    """
    Invoke large language model

    :param model: model name
    :param credentials: model credentials
    :param content_text: text content to be translated
    :param streaming: output is streaming
    :param user: unique user id
    :return: translated audio file
    """        

Parameters:

  • model (string): Model name

  • credentials (object): Credential information

  • content_text (string): Text content to be converted

  • streaming (bool): Whether to stream output

  • user (string) [optional]: Unique user identifier to help providers monitor and detect abuse

Return:

  • Audio stream converted from text

Moderation

Inherit from __base.moderation_model.ModerationModel base class, implement the following interfaces:

Invoke Invocation

def _invoke(self, model: str, credentials: dict,
            text: str, user: Optional[str] = None) \
        -> bool:
    """
    Invoke large language model

    :param model: model name
    :param credentials: model credentials
    :param text: text to moderate
    :param user: unique user id
    :return: false if text is safe, true otherwise
    """

Parameters:

  • model (string): Model name

  • credentials (object): Credential information

  • text (string): Text content

  • user (string) [optional]: Unique user identifier to help providers monitor and detect abuse

Return:

  • False indicates the input text is safe, True indicates otherwise

PreviousModel Designing RulesNextGeneral Specifications

Last updated 4 months ago

For most of the fine-tuned models under OpenAI vendor, you can get their base model by their fine-tuned model name, such as gpt-3.5-turbo-1106, and then return the predefined parameter rules of the base model, refer to the implementation of .

OpenAI