validate_provider_credential in your provider configuration file. During runtime, based on the user’s choice of model type or model name, Dify automatically calls the corresponding model layer’s validate_credentials method to verify credentials.
Integrating a Custom Model Plugin
Below are the steps to integrate a custom model:- Create a Model Provider File
Identify the model types your custom model will include. - Create Code Files by Model Type
Depending on the model’s type (e.g.,llmortext_embedding), create separate code files. Ensure that each model type is organized into distinct logical layers for easier maintenance and future expansion. - Develop the Model Invocation Logic
Within each model-type module, create a Python file named for that model type (for example,llm.py). Define a class in the file that implements the specific model logic, conforming to the system’s model interface specifications. - Debug the Plugin
Write unit and integration tests for the new provider functionality, ensuring that all components work as intended.
1. Create a Model Provider File
In your plugin’s/provider directory, create a xinference.yaml file.
The Xinference family of models supports LLM, Text Embedding, and Rerank model types, so your xinference.yaml must include all three.
Example:
provider_credential_schema. Since Xinference supports text-generation, embeddings, and reranking models, you can configure it as follows:
model_name:
2. Develop the Model Code
Since Xinference supports llm, rerank, speech2text, and tts, you should create corresponding directories under /models, each containing its respective feature code. Below is an example for an llm-type model. You’d create a file named llm.py, then define a class—such as XinferenceAILargeLanguageModel—that extends __base.large_language_model.LargeLanguageModel. This class should include:- LLM Invocation
yield as a generator returning type Generator, so it’s best to split them:
- Pre-calculating Input Tokens
self._get_num_tokens_by_gpt2(text: str) from the AIModel base class, which uses a GPT-2 tokenizer. Remember this is an approximation and may not match your model exactly.
- Validating Model Credentials
- Dynamic Model Parameters Schema
max_tokens, temperature, and top_p. Some other providers (e.g., OpenLLM) may support parameters like top_k only for certain models. This means you need to adapt your schema to each model’s capabilities:
- Error Mapping
3. Debug the Plugin
After finishing development, test the plugin to ensure it runs correctly. For more details, refer to:Debug Plugin
4. Publish the Plugin
If you’d like to list this plugin on the Dify Marketplace, see: Publish to Dify MarketplaceExplore More
Quick Start: Plugins Endpoint Docs:- Manifest Structure
- Endpoint Definitions
- Reverse-Invocation of the Dify Service
- Tools
- Models
Edit this page | Report an issue