Last updated
Last updated
The endpoint methods and parameter descriptions that need to be implemented by the supplier and each model type are described here.
Inherit from the __base.model_provider.ModelProvider
base class, implement the following endpoint:
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 are divided into 5 different model types, each inheriting from different base classes and requiring implementation of different methods.
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 name
credentials
(object): Credential information Credential parameters are defined by the provider's YAML configuration file's provider_credential_schema
or model_credential_schema
, such as passing in api_key
. If validation fails, throw the errors.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 error
InvokeServerUnavailableError
: Invocation service unavailable
InvokeRateLimitError
: Invocation rate limit reached
InvokeAuthorizationError
: Invocation authentication failed
InvokeBadRequestError
: 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.
Inherit from __base.large_language_model.LargeLanguageModel
base class, implement the following interfaces:
Implement the core method for LLM invocation, supporting both streaming and synchronous returns.
Parameters:
model
(string): Model name
credentials
(object): Credential information Credential parameters are defined by the provider's YAML configuration file's provider_credential_schema
or model_credential_schema
, such as passing in api_key
prompt_messages
(array[PromptMessage]): Prompt list
For 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's parameter_rules
tools
(array[PromptMessageTool]) [optional]: Tool list, equivalent to function calling functions
stop
(array[string]) [optional]: Stop sequences. Model output will stop before the defined string
stream
(bool): Whether to stream output, default True. Streaming returns Generator[LLMResultChunk], non-streaming returns LLMResult
user
(string) [optional]: Unique user identifier to help providers monitor and detect abuse
Return:
Streaming returns Generator[LLMResultChunk]
Non-streaming returns LLMResult
If the model does not provide a pre-calculate tokens interface, directly return 0.
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.
Inherit from __base.text_embedding_model.TextEmbeddingModel
base class, implement the following interfaces:
Parameters:
model
(string): Model name
credentials
(object): Credential information Credential parameters are defined by the provider's YAML configuration file's provider_credential_schema
or model_credential_schema
texts
(array[string]): Text list, can be processed in batch
user
(string) [optional]: Unique user identifier to help providers monitor and detect abuse
Return:
TextEmbeddingResult entity
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.
Inherit from __base.rerank_model.RerankModel
base class, implement the following interfaces:
Parameters:
model
(string): Model name
credentials
(object): Credential information
query
(string): Search query content
docs
(array[string]): List of segments to be re-ranked
score_threshold
(float) [optional]: Score threshold
top_n
(int) [optional]: Take top n segments
user
(string) [optional]: Unique user identifier to help providers monitor and detect abuse
Return:
RerankResult entity
Inherit from __base.speech2text_model.Speech2TextModel
base class, implement the following interfaces:
Parameters:
model
(string): Model name
credentials
(object): Credential information
file
(File): File stream
user
(string) [optional]: Unique user identifier to help providers monitor and detect abuse
Return:
Converted text string from speech
Inherit from __base.text2speech_model.Text2SpeechModel
base class, implement the following interfaces:
Parameters:
model
(string): Model name
credentials
(object): Credential information
content_text
(string): Text content to be converted
streaming
(bool): Whether to stream output
user
(string) [optional]: Unique user identifier to help providers monitor and detect abuse
Return:
Audio stream converted from text
Inherit from __base.moderation_model.ModerationModel
base class, implement the following interfaces:
Parameters:
model
(string): Model name
credentials
(object): Credential information
text
(string): Text content
user
(string) [optional]: Unique user identifier to help providers monitor and detect abuse
Return:
False indicates the input text is safe, True indicates otherwise
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 .