Skip to main content

Prerequisites

  • Dify CLI
  • Basic Python programming skills and understanding of object-oriented programming
  • Familiarity with the API documentation of the model provider you want to integrate

Step 1: Create and Configure a New Plugin Project

Initialize the Project

Choose Model Plugin Template

Select the LLM type plugin template from the available options. This template provides a complete code structure for model integration.
Plugin Type: LLM

Configure Plugin Permissions

For a model provider plugin, configure the following essential permissions:
  • Models - Base permission for model operations
  • LLM - Permission for large language model functionality
  • Storage - Permission for file operations (if needed)
Model Plugin Permission

Directory Structure Overview

After initialization, your plugin project will have a directory structure similar to this (assuming a provider named my_provider supporting LLM and Embedding):

Step 2: Understand Model Configuration Methods

Dify supports two model configuration methods that determine how users will interact with your provider’s models:

Predefined Models (predefined-model)

These are models that only require unified provider credentials to use. Once a user configures their API key or other authentication details for the provider, they can immediately access all predefined models. Example: The OpenAI provider offers predefined models like gpt-3.5-turbo-0125 and gpt-4o-2024-05-13. A user only needs to configure their OpenAI API key once to access all these models.

Custom Models (customizable-model)

These require additional configuration for each specific model instance. This approach is useful when models need individual parameters beyond the provider-level credentials. Example: Xinference supports both LLM and Text Embedding, but each model has a unique model_uid. Users must configure this model_uid separately for each model they want to use. These configuration methods can coexist within a single provider. For instance, a provider might offer some predefined models while also allowing users to add custom models with specific configurations.

Step 3: Create Model Provider Files

Creating a new model provider involves two main components:
  1. Provider Configuration YAML File - Defines the provider’s basic information, supported model types, and credential requirements
  2. Provider Class Implementation - Implements authentication validation and other provider-level functionality

3.1 Create Model Provider Configuration File

The provider configuration is defined in a YAML file that declares the provider’s basic information, supported model types, configuration methods, and credential rules. This file will be placed in the root directory of your plugin project. Here’s an annotated example of the anthropic.yaml configuration file:

Custom Model Configuration

If your provider supports custom models, you need to add a model_credential_schema section to define what additional fields users need to configure for each individual model. This is typical for providers that support fine-tuned models or require model-specific parameters. Here’s an example from the OpenAI provider:
For complete model provider YAML specifications, please refer to the Model Schema documentation.

3.2 Write Model Provider Code

Next, create a Python file for your provider class implementation. This file should be placed in the /provider directory with a name matching your provider (e.g., anthropic.py). The provider class must inherit from ModelProvider and implement at least the validate_provider_credentials method:
The validate_provider_credentials method is crucial as it’s called whenever a user tries to save their provider credentials in Dify. It should:
  1. Attempt to validate the credentials by making a simple API call
  2. Return silently if validation succeeds
  3. Raise CredentialsValidateFailedError with a helpful message if validation fails

For Custom Model Providers

For providers that exclusively use custom models (where each model requires its own configuration), you can implement a simpler provider class. For example, with Xinference:

Step 4: Implement Model-Specific Code

After setting up your provider, you need to implement the model-specific code that will handle API calls for each model type you support. This involves:
  1. Creating model configuration YAML files for each specific model
  2. Implementing the model type classes that handle API communication
For detailed instructions on these steps, please refer to:

4.1 Define Model Configuration (YAML)

For each specific model, create a YAML file in the appropriate model type directory (e.g., models/llm/) to define its properties, parameters, and features. Example (claude-3-5-sonnet-20240620.yaml):

4.2 Implement Model Calling Code (Python)

Create a Python file for each model type you’re supporting (e.g., llm.py in the models/llm/ directory). This class will handle API communication, parameter transformation, and result formatting. Here’s an example implementation structure for an LLM:
The most important method to implement is _invoke, which handles the core API communication. This method should:
  1. Transform Dify’s standardized inputs into the format required by the provider’s API
  2. Make the API call with proper error handling
  3. Transform the API response into Dify’s standardized output format
  4. Handle both streaming and non-streaming modes

Step 5: Debug and Test Your Plugin

Dify provides a remote debugging capability that allows you to test your plugin during development:
  1. In your Dify instance, go to “Plugin Management” and click “Debug Plugin” to get your debug key and server address
  2. Configure your local environment with these values in a .env file:
  1. Run your plugin locally with python -m main and test it in Dify

Step 6: Package and Publish

When your plugin is ready:
  1. Package it using the scaffolding tool:
  2. Test the packaged plugin locally before submitting
  3. Submit a pull request to the Dify official plugins repository
For more details on the publishing process, see the Publishing Overview.

Reference Resources


Edit this page | Report an issue