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 theLLM type plugin template from the available options. This template provides a complete code structure for model integration.
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)
Directory Structure Overview
After initialization, your plugin project will have a directory structure similar to this (assuming a provider namedmy_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:- Provider Configuration YAML File - Defines the provider’s basic information, supported model types, and credential requirements
- 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 theanthropic.yaml configuration file:
Custom Model Configuration
If your provider supports custom models, you need to add amodel_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:
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:
validate_provider_credentials method is crucial as it’s called whenever a user tries to save their provider credentials in Dify. It should:
- Attempt to validate the credentials by making a simple API call
- Return silently if validation succeeds
- Raise
CredentialsValidateFailedErrorwith 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, withXinference:
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:- Creating model configuration YAML files for each specific model
- Implementing the model type classes that handle API communication
- Model Design Rules - Standards for integrating predefined models
- Model Schema - Standards for model configuration files
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:
_invoke, which handles the core API communication. This method should:
- Transform Dify’s standardized inputs into the format required by the provider’s API
- Make the API call with proper error handling
- Transform the API response into Dify’s standardized output format
- 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:- In your Dify instance, go to “Plugin Management” and click “Debug Plugin” to get your debug key and server address
- Configure your local environment with these values in a
.envfile:
- Run your plugin locally with
python -m mainand test it in Dify
Step 6: Package and Publish
When your plugin is ready:-
Package it using the scaffolding tool:
- Test the packaged plugin locally before submitting
- Submit a pull request to the Dify official plugins repository
Reference Resources
- Quick Integration of a New Model - How to add new models to existing providers
- Basic Concepts of Plugin Development - Return to the plugin development getting started guide
- Model Schema - Learn detailed model configuration specifications
- General Specifications - Learn about plugin manifest file configuration
- Dify Plugin SDK Reference - Look up base classes, data structures, and error types
Edit this page | Report an issue