Model Provider
Inherit the__base.model_provider.ModelProvider base class and implement the following interface:
credentials(object) Credential information
provider_credential_schema, passed in as api_key, etc. If validation fails, please throw a errors.validate.CredentialsValidateFailedError error. Note: Predefined models need to fully implement this interface, while custom model providers only need to implement it simply as follows:
Models
Models are divided into 5 different types, with different base classes to inherit from and different methods to implement for each type.Common Interfaces
All models need to implement the following 2 methods consistently:- Model credential validation
model(string) Model namecredentials(object) Credential information
provider_credential_schema or model_credential_schema, passed in as api_key, etc. If validation fails, please throw a errors.validate.CredentialsValidateFailedError error.
- Invocation error mapping table
InvokeError type in Runtime, which helps Dify handle different errors differently. Runtime Errors:
InvokeConnectionErrorConnection error during invocationInvokeServerUnavailableErrorService provider unavailableInvokeRateLimitErrorRate limit reachedInvokeAuthorizationErrorAuthentication failedInvokeBadRequestErrorIncorrect parameters passed
InvokeConnectionError.
LLM
Inherit the__base.large_language_model.LargeLanguageModel base class and implement the following interface:
- LLM Invocation
- Parameters:
model(string) Model namecredentials(object) Credential information
provider_credential_schema or model_credential_schema, passed in as api_key, etc.
prompt_messages(array[PromptMessage]) Prompt list
Completion type, the list only needs to include one UserPromptMessage element; if the model is of Chat type, different messages need to be passed in as a list of SystemPromptMessage, UserPromptMessage, AssistantPromptMessage, ToolPromptMessage elements
-
model_parameters(object) Model parameters defined by the model YAML configuration’sparameter_rules. -
tools(array[PromptMessageTool]) [optional] Tool list, equivalent tofunctioninfunction calling. This is the tool list passed to tool calling. -
stop(array[string]) [optional] Stop sequence. The model response will stop output before the string defined in the stop sequence. -
stream(bool) Whether to stream output, default is True For streaming output, it returns Generator[LLMResultChunk], for non-streaming output, it returns LLMResult. -
user(string) [optional] A unique identifier for the user that can help the provider monitor and detect abuse. - Return Value
- Pre-calculate input tokens
LLM Invocation above. This interface needs to calculate based on the appropriate tokenizer for the corresponding model. If the corresponding model does not provide a tokenizer, you can use the _get_num_tokens_by_gpt2(text: str) method in the AIModel base class for calculation.
- Get custom model rules [optional]
OpenAI provider, the base model can be obtained through the 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 specific implementation of OpenAI.
TextEmbedding
Inherit the__base.text_embedding_model.TextEmbeddingModel base class and implement the following interface:
- Embedding Invocation
- Parameters:
-
model(string) Model name -
credentials(object) Credential information
provider_credential_schema or model_credential_schema, passed in as api_key, etc.
-
texts(array[string]) Text list, can be processed in batch -
user(string) [optional] A unique identifier for the user Can help the provider monitor and detect abuse. - Return:
- Pre-calculate tokens
Embedding Invocation section above.
Similar to the LargeLanguageModel above, this interface needs to calculate based on the appropriate tokenizer for the corresponding model. If the corresponding model does not provide a tokenizer, you can use the _get_num_tokens_by_gpt2(text: str) method in the AIModel base class for calculation.
Rerank
Inherit the__base.rerank_model.RerankModel base class and implement the following interface:
- Rerank Invocation
- Parameters:
-
model(string) Model name -
credentials(object) Credential information The credential parameters are defined by the provider YAML configuration file’sprovider_credential_schemaormodel_credential_schema, passed in asapi_key, etc. -
query(string) Query request content -
docs(array[string]) List of segments that need to be reranked -
score_threshold(float) [optional] Score threshold -
top_n(int) [optional] Take the top n segments -
user(string) [optional] A unique identifier for the user Can help the provider monitor and detect abuse. - Return:
Speech2text
Inherit the__base.speech2text_model.Speech2TextModel base class and implement the following interface:
- Invoke
- Parameters:
-
model(string) Model name -
credentials(object) Credential information The credential parameters are defined by the provider YAML configuration file’sprovider_credential_schemaormodel_credential_schema, passed in asapi_key, etc. -
file(File) File stream -
user(string) [optional] A unique identifier for the user Can help the provider monitor and detect abuse. - Return:
Text2speech
Inherit the__base.text2speech_model.Text2SpeechModel base class and implement the following interface:
- Invoke
- Parameters:
-
model(string) Model name -
credentials(object) Credential information The credential parameters are defined by the provider YAML configuration file’sprovider_credential_schemaormodel_credential_schema, passed in asapi_key, etc. -
content_text(string) Text content to be converted -
streaming(bool) Whether to stream output -
user(string) [optional] A unique identifier for the user Can help the provider monitor and detect abuse. - Return:
Moderation
Inherit the__base.moderation_model.ModerationModel base class and implement the following interface:
- Invoke
- Parameters:
-
model(string) Model name -
credentials(object) Credential information The credential parameters are defined by the provider YAML configuration file’sprovider_credential_schemaormodel_credential_schema, passed in asapi_key, etc. -
text(string) Text content -
user(string) [optional] A unique identifier for the user Can help the provider monitor and detect abuse. - Return:
Entities
PromptMessageRole
Message rolePromptMessageContentType
Message content type, divided into plain text and images.PromptMessageContent
Message content base class, used only for parameter declaration, cannot be initialized.TextPromptMessageContent and ImagePromptMessageContent separately.
TextPromptMessageContent
content list.
ImagePromptMessageContent
content list.
data can be a url or an image base64 encoded string.
PromptMessage
Base class for all Role message bodies, used only for parameter declaration, cannot be initialized.UserPromptMessage
UserMessage message body, represents user messages.AssistantPromptMessage
Represents model response messages, typically used forfew-shots or chat history input.
tool_calls is the list of tool call returned by the model after passing in tools to the model.
SystemPromptMessage
Represents system messages, typically used to set system instructions for the model.ToolPromptMessage
Represents tool messages, used to pass results to the model for next-step planning after a tool has been executed.content passes in the tool execution result.
PromptMessageTool
LLMResult
LLMResultChunkDelta
Delta entity within each iteration in streaming responseLLMResultChunk
Iteration entity in streaming responseLLMUsage
TextEmbeddingResult
EmbeddingUsage
RerankResult
RerankDocument
Related Resources
- Model Design Rules - Understand the standards for model configuration
- Model Plugin Introduction - Quickly understand the basic concepts of model plugins
- Quickly Integrate a New Model - Learn how to add new models to existing providers
- Create a New Model Provider - Learn how to develop brand new model providers
Edit this page | Report an issue