module in the directory of the respective supplier.
The currently supported model types are as follows:
llmText Generation Modeltext_embeddingText Embedding ModelrerankRerank Modelspeech2textSpeech to TextttsText to SpeechmoderationModeration
Anthropic as an example, Anthropic only supports LLM, so we create a module named llm in model_providers.anthropic.
For predefined models, we first need to create a YAML file named after the model under the llm module, such as: claude-2.1.yaml.
Preparing the Model YAML
model_providers directory. The complete YAML rules can be found in: Schema.
Implementing Model Invocation Code
Next, create a Python file with the same namellm.py under the llm module to write the implementation code.
Create an Anthropic LLM class in llm.py, which we will name AnthropicLargeLanguageModel (name can be arbitrary), inheriting from the __base.large_language_model.LargeLanguageModel base class, and implement the following methods:
-
LLM Invocation
Implement the core method for LLM invocation, supporting both streaming and synchronous responses.
When implementing, note to use two functions to return data, one for handling synchronous responses and one for streaming responses. Since Python recognizes functions containing the
yieldkeyword as generator functions, returning a fixed data type ofGenerator, synchronous and streaming responses need to be implemented separately, like this (note the example below uses simplified parameters, actual implementation should follow the parameter list above): -
Precompute Input Tokens
If the model does not provide a precompute tokens interface, return 0 directly.
-
Model Credentials Validation
Similar to supplier credentials validation, this validates the credentials for a single model.
-
Invocation Error Mapping Table
When a model invocation error occurs, it needs to be mapped to the
InvokeErrortype specified by Runtime, facilitating Dify to handle different errors differently. Runtime Errors:InvokeConnectionErrorInvocation connection errorInvokeServerUnavailableErrorInvocation service unavailableInvokeRateLimitErrorInvocation rate limit reachedInvokeAuthorizationErrorInvocation authorization failedInvokeBadRequestErrorInvocation parameter error
Provider
provider(string) Supplier identifier, e.g.,openailabel(object) Supplier display name, i18n, can be set inen_USEnglish andzh_HansChinesezh_Hans(string) [optional] Chinese label name, ifzh_Hansis not set, it will default toen_US.en_US(string) English label name
description(object) [optional] Supplier description, i18nzh_Hans(string) [optional] Chinese descriptionen_US(string) English description
icon_small(string) [optional] Supplier small icon, stored in the_assetsdirectory under the respective supplier implementation directory, follows the same language strategy aslabelzh_Hans(string) [optional] Chinese iconen_US(string) English icon
icon_large(string) [optional] Supplier large icon, stored in the_assetsdirectory under the respective supplier implementation directory, follows the same language strategy aslabelzh_Hans(string) [optional] Chinese iconen_US(string) English icon
background(string) [optional] Background color value, e.g., #FFFFFF, if empty, the default color value will be displayed on the front end.help(object) [optional] Help informationtitle(object) Help title, i18nzh_Hans(string) [optional] Chinese titleen_US(string) English title
url(object) Help link, i18nzh_Hans(string) [optional] Chinese linken_US(string) English link
supported_model_types(array[ModelType]) Supported model typesconfigurate_methods(array[ConfigurateMethod]) Configuration methodsprovider_credential_schema(ProviderCredentialSchema) Supplier credential schemamodel_credential_schema(ModelCredentialSchema) Model credential schema