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
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:
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.
Parameters:
model
(string): Model namecredentials
(object): Credential information Credential parameters are defined by the provider's YAML configuration file'sprovider_credential_schema
ormodel_credential_schema
, such as passing inapi_key
. If validation fails, throw theerrors.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 errorInvokeServerUnavailableError
: Invocation service unavailableInvokeRateLimitError
: Invocation rate limit reachedInvokeAuthorizationError
: Invocation authentication failedInvokeBadRequestError
: Incorrect invocation parameters
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.
Parameters:
model
(string): Model namecredentials
(object): Credential information Credential parameters are defined by the provider's YAML configuration file'sprovider_credential_schema
ormodel_credential_schema
, such as passing inapi_key
prompt_messages
(array[PromptMessage]): Prompt listFor 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'sparameter_rules
tools
(array[PromptMessageTool]) [optional]: Tool list, equivalent to function calling functionsstop
(array[string]) [optional]: Stop sequences. Model output will stop before the defined stringstream
(bool): Whether to stream output, default True. Streaming returns Generator[LLMResultChunk], non-streaming returns LLMResultuser
(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.
Optional: Get Custom Model Rules
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.
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.
Text Embedding
Inherit from __base.text_embedding_model.TextEmbeddingModel
base class, implement the following interfaces:
Embedding Invocation
Parameters:
model
(string): Model namecredentials
(object): Credential information Credential parameters are defined by the provider's YAML configuration file'sprovider_credential_schema
ormodel_credential_schema
texts
(array[string]): Text list, can be processed in batchuser
(string) [optional]: Unique user identifier to help providers monitor and detect abuse
Return:
TextEmbeddingResult entity
Pre-calculate Tokens
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
Parameters:
model
(string): Model namecredentials
(object): Credential informationquery
(string): Search query contentdocs
(array[string]): List of segments to be re-rankedscore_threshold
(float) [optional]: Score thresholdtop_n
(int) [optional]: Take top n segmentsuser
(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
Parameters:
model
(string): Model namecredentials
(object): Credential informationfile
(File): File streamuser
(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
Parameters:
model
(string): Model namecredentials
(object): Credential informationcontent_text
(string): Text content to be convertedstreaming
(bool): Whether to stream outputuser
(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
Parameters:
model
(string): Model namecredentials
(object): Credential informationtext
(string): Text contentuser
(string) [optional]: Unique user identifier to help providers monitor and detect abuse
Return:
False indicates the input text is safe, True indicates otherwise
Last updated