Interfaces

This section describes the interface methods and parameter explanations that need to be implemented by providers and various model types.

Provider

Inherit the __base.model_provider.ModelProvider base class and implement the following interfaces:

def validate_provider_credentials(self, credentials: dict) -> None:
    """
    Validate provider credentials
    You can choose any validate_credentials method of model type or implement validate method by yourself,
    such as: get model list api

    if validate failed, raise exception

    :param credentials: provider credentials, credentials form defined in `provider_credential_schema`.
    """
  • credentials (object) Credential information

    The parameters of credential information are defined by the provider_credential_schema in the provider's YAML configuration file. Inputs such as api_key are included.

If verification fails, throw the errors.validate.CredentialsValidateFailedError error.

Model

Models are divided into 5 different types, each inheriting from different base classes and requiring the implementation of different methods.

All models need to uniformly implement the following 2 methods:

  • Model Credential Verification

    Similar to provider credential verification, this step involves verification for an individual model.

    Parameters:

    • model (string) Model name

    • credentials (object) Credential information

      The parameters of credential information are defined by either the provider_credential_schema or model_credential_schema in the provider's YAML configuration file. Inputs such as api_key are included.

    If verification fails, throw the errors.validate.CredentialsValidateFailedError error.

  • Invocation Error Mapping Table

    When there is an exception in model invocation, it needs to be mapped to the InvokeError type specified by Runtime. This facilitates Dify's ability to handle different errors with appropriate follow-up actions.

    Runtime Errors:

    • InvokeConnectionError Invocation connection error

    • InvokeServerUnavailableError Invocation service provider unavailable

    • InvokeRateLimitError Invocation reached rate limit

    • InvokeAuthorizationError Invocation authorization failure

    • InvokeBadRequestError Invocation parameter error

​ You can refer to OpenAI's _invoke_error_mapping for an example.

LLM

Inherit the __base.large_language_model.LargeLanguageModel base class and implement the following interfaces:

  • LLM Invocation

    Implement the core method for LLM invocation, which can support both streaming and synchronous returns.

    • Parameters:

      • model (string) Model name

      • credentials (object) Credential information

        The parameters of credential information are defined by either the provider_credential_schema or model_credential_schema in the provider's YAML configuration file. Inputs such as api_key are included.

      • prompt_messages (array[PromptMessage]) List of prompts

        If the model is of the Completion type, the list only needs to include one UserPromptMessage element;

        If the model is of the Chat type, it requires a list of elements such as SystemPromptMessage, UserPromptMessage, AssistantPromptMessage, ToolPromptMessage depending on the message.

      • model_parameters (object) Model parameters

        The model parameters are defined by the parameter_rules in the model's YAML configuration.

      • tools (array[PromptMessageTool]) [optional] List of tools, equivalent to the function in function calling.

        That is, the tool list for tool calling.

      • stop (array[string]) [optional] Stop sequences

        The model output will stop before the string defined by the stop sequence.

      • stream (bool) Whether to output in a streaming manner, default is True

        Streaming output returns Generator[LLMResultChunk], non-streaming output returns LLMResult.

      • user (string) [optional] Unique identifier of the user

        This can help the provider monitor and detect abusive behavior.

    • Returns

      Streaming output returns Generator[LLMResultChunk], non-streaming output returns LLMResult.

  • Pre-calculating Input Tokens

    If the model does not provide a pre-calculated tokens interface, you can directly return 0.

    For parameter explanations, refer to the above section on LLM Invocation.

  • Fetch Custom Model Schema [Optional]

    When the provider supports adding custom LLMs, this method can be implemented to allow custom models to fetch model schema. The default return null.

TextEmbedding

Inherit the __base.text_embedding_model.TextEmbeddingModel base class and implement the following interfaces:

  • Embedding Invocation

    • Parameters:

      • model (string) Model name

      • credentials (object) Credential information

        The parameters of credential information are defined by either the provider_credential_schema or model_credential_schema in the provider's YAML configuration file. Inputs such as api_key are included.

      • texts (array[string]) List of texts, capable of batch processing

      • user (string) [optional] Unique identifier of the user

        This can help the provider monitor and detect abusive behavior.

    • Returns:

      TextEmbeddingResult entity.

  • Pre-calculating Tokens

    For parameter explanations, refer to the above section on Embedding Invocation.

Rerank

Inherit the __base.rerank_model.RerankModel base class and implement the following interfaces:

  • Rerank Invocation

    • Parameters:

      • model (string) Model name

      • credentials (object) Credential information

        The parameters of credential information are defined by either the provider_credential_schema or model_credential_schema in the provider's YAML configuration file. Inputs such as api_key are included.

      • query (string) Query request content

      • docs (array[string]) List of segments to be reranked

      • score_threshold (float) [optional] Score threshold

      • top_n (int) [optional] Select the top n segments

      • user (string) [optional] Unique identifier of the user

        This can help the provider monitor and detect abusive behavior.

    • Returns:

      RerankResult entity.

Speech2text

Inherit the __base.speech2text_model.Speech2TextModel base class and implement the following interfaces:

  • Invoke Invocation

    • Parameters:

      • model (string) Model name

      • credentials (object) Credential information

        The parameters of credential information are defined by either the provider_credential_schema or model_credential_schema in the provider's YAML configuration file. Inputs such as api_key are included.

      • file (File) File stream

      • user (string) [optional] Unique identifier of the user

        This can help the provider monitor and detect abusive behavior.

    • Returns:

      The string after speech-to-text conversion.

Text2speech

Inherit the __base.text2speech_model.Text2SpeechModel base class and implement the following interfaces:

  • Invoke Invocation

    • Parameters:

      • model (string) Model name

      • credentials (object) Credential information

        The parameters of credential information are defined by either the provider_credential_schema or model_credential_schema in the provider's YAML configuration file. Inputs such as api_key are included.

      • content_text (string) The text content that needs to be converted

      • streaming (bool) Whether to stream output

      • user (string) [optional] Unique identifier of the user

        This can help the provider monitor and detect abusive behavior.

    • Returns:

      Text converted speech stream。

Moderation

Inherit the __base.moderation_model.ModerationModel base class and implement the following interfaces:

  • Invoke Invocation

    • Parameters:

      • model (string) Model name

      • credentials (object) Credential information

        The parameters of credential information are defined by either the provider_credential_schema or model_credential_schema in the provider's YAML configuration file. Inputs such as api_key are included.

      • text (string) Text content

      • user (string) [optional] Unique identifier of the user

        This can help the provider monitor and detect abusive behavior.

    • Returns:

      False indicates that the input text is safe, True indicates otherwise.

Entities

PromptMessageRole

Message role

PromptMessageContentType

Message content types, divided into text and image.

PromptMessageContent

Message content base class, used only for parameter declaration and cannot be initialized.

Currently, two types are supported: text and image. It's possible to simultaneously input text and multiple images.

You need to initialize TextPromptMessageContent and ImagePromptMessageContent separately for input.

TextPromptMessageContent

If inputting a combination of text and images, the text needs to be constructed into this entity as part of the content list.

ImagePromptMessageContent

If inputting a combination of text and images, the images need to be constructed into this entity as part of the content list.

data can be either a url or a base64 encoded string of the image.

PromptMessage

The base class for all Role message bodies, used only for parameter declaration and cannot be initialized.

UserPromptMessage

UserMessage message body, representing a user's message.

AssistantPromptMessage

Represents a message returned by the model, typically used for few-shots or inputting chat history.

Where tool_calls are the list of tool calls returned by the model after invoking the model with the tools input.

SystemPromptMessage

Represents system messages, usually used for setting system commands given to the model.

ToolPromptMessage

Represents tool messages, used for conveying the results of a tool execution to the model for the next step of processing.

The base class's content takes in the results of tool execution.

PromptMessageTool


LLMResult

LLMResultChunkDelta

In streaming returns, each iteration contains the delta entity.

LLMResultChunk

Each iteration entity in streaming returns.

LLMUsage


TextEmbeddingResult

EmbeddingUsage


RerankResult

RerankDocument

Last updated