Dify
日本語
日本語
  • 入門
    • Difyへようこそ
      • 特性と技術仕様
      • モデルプロバイダーリスト
    • クラウドサービス
    • コミュニティ版
      • Docker Compose デプロイ
      • ローカルソースコードで起動
      • aaPanelでのデプロイ方法
      • フロントエンドDockerコンテナを単独起動
      • 環境変数の説明
      • よくある質問
    • Dify Premium
    • Dify 教育版
  • マニュアル
    • モデル
      • 新しいプロバイダーの追加
      • 事前定義されたモデルの追加
      • カスタムモデルの追加
      • インタフェース
      • 配置ルール
      • 負荷分散
    • アプリ・オーケストレーション
      • アプリの作成
      • チャットボット
        • 複数モデルのデバッグ
      • エージェント
      • ツールキット
        • コンテンツモデレーション
    • ワークフロー
      • キーコンセプト
      • 変数
      • ノードの説明
        • 開始
        • 終了
        • 回答
        • LLM
        • 知識検索
        • 質問分類
        • 条件分岐
        • コード実行
        • テンプレート
        • テキスト抽出ツール
        • リスト処理
        • 変数集約
        • 変数代入
        • 反復処理(イテレーション)
        • パラメータ抽出
        • HTTPリクエスト
        • エージェント
        • ツール
        • 繰り返し処理(ループ)
      • ショートカットキー
      • オーケストレートノード
      • ファイルアップロード
      • エラー処理
        • 事前定義されたエラー処理ロジック
        • エラータイプの概要
      • 追加機能
      • プレビューとデバッグ
        • プレビューと実行
        • ステップ実行
        • 対話/実行ログ
        • チェックリスト
        • 実行履歴
      • アプリケーション公開
      • JSON形式での出力
      • 変更通知:画像アップロード機能がファイルアップロード機能に統合されました
    • ナレッジベース
      • ナレッジベース作成
        • 1. オンラインデータソースの活用
          • 1.1 Notion からデータをインポート
          • 1.2 Webサイトからデータをインポート
        • 2. チャンクモードの指定
        • 3. インデックス方式と検索オプションの設定
      • ナレッジベースの管理
        • ナレッジベース内ドキュメントの管理
        • APIを活用したナレッジベースのメンテナンス
      • メタデータ
      • アプリ内でのナレッジベース統合
      • リコールテスト/引用帰属
      • ナレッジベースの要求頻度制限
      • 外部ナレッジベースとの接続
      • 外部ナレッジベースAPI
    • ツール
      • クイック統合ツール
      • 高度統合ツール
      • ツールの設定
        • Google
        • Bing
        • SearchApi
        • StableDiffusion
        • Perplexity Search
        • AlphaVantage 株式分析
        • Dall-e
        • Youtube
        • Serper
        • SearXNG
        • SiliconFlow(Flux AI サポート)
        • ComfyUI
    • アプリ公開
      • シングルページWebアプリとして公開
        • Web アプリの設定
        • テキスト生成アプリ
        • 対話型アプリ
      • Webサイトへの埋め込み
      • API基づく開発
      • フロントエンドテンプレートに基づいた再開発
    • アノテーション
      • ログとアノテーション
      • アノテーション返信
    • モニタリング
      • データ分析
      • 外部Opsツール統合
        • LangSmithを統合
        • LangFuseを統合
        • Opikを統合
    • 拡張
      • API 拡張
        • Cloudflare Workers を使用した API ツールのデプロイ
        • コンテンツモデレーション
      • コード拡張
        • 外部データツール
        • コンテンツモデレーション
    • コラボレーション
      • 発見
      • メンバーの招待と管理
    • 管理
      • アプリの管理
      • チームメンバーの管理
      • 個人アカウントの管理
      • サブスクリプション管理
      • バージョン管理
  • ハンドオン工房
    • 初級編
      • ゼロからAI画像生成アプリの構築方法
      • AIエージェントの実践:個人のオンライン旅行アシスタントの構築方法
    • 中級編
      • チャットストリームエージェントを使用した Twitter アカウントの分析方法
      • ファイルアップロードを使用した記事理解アシスタントの構築方法
  • コミュニティ
    • サポートの求め
    • 貢献者ガイド
    • ドキュメントへの貢献
  • プラグイン
    • 機能紹介
    • クイックスタート
      • プラグインのインストールと活用
      • プラグイン開発の入門
        • 開発環境のセットアップ
        • ツール型プラグイン
        • モデル型プラグイン
          • モデルプロバイダーの構築
          • 定義済みモデルの組み込み
          • カスタムモデルの組み込み
        • エージェント戦略プラグイン
        • 拡張機能型プラグイン
        • バンドル
      • プラグインのデバッグ方法
    • プラグイン管理方法
    • スキーマ仕様
      • Manifest(マニフェスト)
      • Endpoint(エンドポイント)
      • Tool(ツール)
      • Agent(エージェント)
      • Model(モデル)
        • モデル設計規則
        • モデルスキーマ
      • 一般的な標準仕様
      • 永続化されたストレージ
      • Difyサービスの逆呼び出し
        • アプリ
        • モデル
        • ツール
        • ノード
    • ベストプラクティス
      • Slack Bot プラグインの開発
      • Dify MCP プラグインガイド:ワンクリックで Zapier に接続してメールを自動送信
    • プラグインの公開
      • プラグインの自動公開
      • Difyマーケットプレイスへの公開
        • プラグイン開発者ガイドライン
        • プラグインのプライバシー保護に関するガイドライン
      • 個人GitHubリポジトリへの公開
      • ローカルでの公開と共有
      • 第三者署名検証のためにプラグインに署名する
    • よくある質問
  • 開発
    • バックエンド
      • DifySandbox
        • 貢献ガイド
    • モデルの統合
      • Hugging Faceのオープンソースモデルを統合
      • Replicateのオープンソースモデルを統合
      • Xinferenceでデプロイしたローカルモデルを統合
      • OpenLLMでデプロイしたローカルモデルを統合
      • LocalAIでデプロイしたローカルモデルを統合
      • Ollamaでデプロイしたローカルモデルを統合
      • LiteLLM Proxyを使用してモデルを統合する
      • GPUStackとの統合によるローカルモデルのデプロイ
      • AWS Bedrock上のモデル(DeepSeek)の接続
    • 移行
      • コミュニティ版を v1.0.0 に移行する
  • もっと読む
    • 活用事例
      • DeepSeek & Dify連携ガイド:多段階推論を活用したAIアプリケーション構築
      • Ollama + DeepSeek + Dify のプライベートデプロイ:あなた自身のAIアシスタントの構築方法
      • あなた専用のQAチャットボットのトレーニング方法
      • コードなしでMidjourney プロンプトボットを作成する方法
      • Notion AI アシスタントを構築する
      • 数分で業務データを持つ公式サイトのAIチャットボットを作成する方法
      • DifyチャットボットをWixサイトに統合する方法
      • AWS Bedrockのナレッジベースに統合する方法
      • Difyで大規模言語モデルの「競技場」を体験する方法:DeepSeek R1 VS o1 を例に
      • Difyスケジューラーの構築
      • DifyクラウドでAI Thesis Slack Botを構築
    • さらに読む
      • LLMOpsとは何ですか?
      • 配列変数とは何ですか?
      • 検索拡張生成(RAG)
        • ハイブリッド検索
        • Rerank
        • リトリーバルモード
      • プロンプトエンジニアリング
      • DifyでJSONスキーマ出力を使用する方法
    • FAQ
      • ローカルデプロイに関するFAQ
      • LLM設定と使用に関するFAQ
      • プラグイン
  • ポリシー
    • オープンソースライセンス
    • ユーザ規約
      • 利用規約
      • プライバシーポリシー
      • 合規性レポートの入手方法
Powered by GitBook
On this page
  • サプライヤー
  • モデル
  • 一般インターフェース
  • LLM
  • TextEmbedding
  • Rerank
  • Speech2text
  • Text2speech
  • Moderation
  • エンティティ
  • PromptMessageRole
  • PromptMessageContentType
  • PromptMessageContent
  • TextPromptMessageContent
  • ImagePromptMessageContent
  • PromptMessage
  • UserPromptMessage
  • AssistantPromptMessage
  • SystemPromptMessage
  • ToolPromptMessage
  • PromptMessageTool
  • LLMResult
  • LLMResultChunkDelta
  • LLMResultChunk
  • LLMUsage
  • TextEmbeddingResult
  • EmbeddingUsage
  • RerankResult
  • RerankDocument
  • Model
  • LLM
  • TextEmbedding
  • Rerank
  • Speech2text
  • Text2speech
  • Moderation
  • Entities
  • PromptMessageRole
  • PromptMessageContentType
  • PromptMessageContent
  • TextPromptMessageContent
  • ImagePromptMessageContent
  • PromptMessage
  • UserPromptMessage
  • AssistantPromptMessage
  • SystemPromptMessage
  • ToolPromptMessage
  • PromptMessageTool
  • LLMResult
  • LLMResultChunkDelta
  • LLMResultChunk
  • LLMUsage
  • TextEmbeddingResult
  • EmbeddingUsage
  • RerankResult
  • RerankDocument
  1. マニュアル
  2. モデル

インタフェース

ここでは、サプライヤーと各モデルタイプが実装する必要があるインターフェース方法とそのパラメータについて説明します。

サプライヤー

__base.model_provider.ModelProvider基本クラスを継承し、以下のインターフェースを実装する必要があります:

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) 資格情報

    資格情報のパラメータは、サプライヤーのYAML構成ファイルの provider_credential_schema で定義され、api_keyなどが渡されます。

検証に失敗した場合は、errors.validate.CredentialsValidateFailedErrorエラーをスローします。

注:事前定義されたモデルはこのインターフェースを完全に実装する必要がありますが、カスタムモデルサプライヤーは以下の簡単な実装のみが必要です

class XinferenceProvider(Provider):
    def validate_provider_credentials(self, credentials: dict) -> None:
        pass

モデル

モデルには5つの異なるモデルタイプがあり、異なる基底クラスを継承し、実装する必要があるメソッドも異なります。

一般インターフェース

すべてのモデルには以下の2つのメソッドを実装する必要があります:

  • モデルの資格情報を検証する

    サプライヤーの資格情報検証と同様に、ここでは個々のモデルに対して検証を行います。

    def validate_credentials(self, model: str, credentials: dict) -> None:
        """
        Validate model credentials
    
        :param model: model name
        :param credentials: model credentials
        :return:
        """

    パラメータ:

    • model (string) モデル名

    • credentials (object) 資格情報

      資格情報のパラメータは、供給業者の YAML 構成ファイルの provider_credential_schema または model_credential_schema で定義されており、api_key などの詳細が含まれます。

    検証に失敗した場合は、errors.validate.CredentialsValidateFailedError エラーをスローします。

  • 例外エラーマッピングの呼び出し

    モデルの呼び出しが例外をスローした場合、Runtimeが指定する InvokeError タイプにマッピングする必要があり、異なるエラーに対して異なる後続処理を行うためのDifyにとって便利です。

    Runtime Errors:

    • InvokeConnectionError コール接続エラー

    • InvokeServerUnavailableError コールサービスが利用できない

    • InvokeRateLimitError コールが制限に達した

    • InvokeAuthorizationError コール認証エラー

    • InvokeBadRequestError コールパラメータが誤っています

    @property
    def _invoke_error_mapping(self) -> dict[type[InvokeError], list[type[Exception]]]:
        """
        Map model invoke error to unified error
        The key is the error type thrown to the caller
        The value is the error type thrown by the model,
        which needs to be converted into a unified error type for the caller.
    
        :return: Invoke error mapping
        """

    または、対応するエラーを直接スローし、以下のように定義することもできます。これにより、後続の呼び出しで InvokeConnectionError などの例外を直接スローできます。

    @property
    def _invoke_error_mapping(self) -> dict[type[InvokeError], list[type[Exception]]]:
        return {
            InvokeConnectionError: [
              InvokeConnectionError
            ],
            InvokeServerUnavailableError: [
              InvokeServerUnavailableError
            ],
            InvokeRateLimitError: [
              InvokeRateLimitError
            ],
            InvokeAuthorizationError: [
              InvokeAuthorizationError
            ],
            InvokeBadRequestError: [
              InvokeBadRequestError
            ],
        }

​ OpenAIの _invoke_error_mapping をご参照ください。

LLM

__base.large_language_model.LargeLanguageModel 基本クラスを継承し、以下のインターフェースを実装します:

  • LLMの呼び出し

    LLM呼び出しの核心メソッドを実装し、ストリーミングと同期応答の両方をサポートします。

    def _invoke(self, model: str, credentials: dict,
                prompt_messages: list[PromptMessage], model_parameters: dict,
                tools: Optional[list[PromptMessageTool]] = None, stop: Optional[list[str]] = None,
                stream: bool = True, user: Optional[str] = None) \
            -> Union[LLMResult, Generator]:
        """
        Invoke large language model
    
        :param model: model name
        :param credentials: model credentials
        :param prompt_messages: prompt messages
        :param model_parameters: model parameters
        :param tools: tools for tool calling
        :param stop: stop words
        :param stream: is stream response
        :param user: unique user id
        :return: full response or stream response chunk generator result
        """
    • パラメータ:

      • model (string) モデル名

      • credentials (object) 資格情報

        資格情報のパラメータは、供給業者の YAML 構成ファイルの provider_credential_schema または model_credential_schema で定義されており、api_key などの詳細が含まれます。

      • model_parameters (object) モデルのパラメータ

        モデルのパラメータは、モデルのYAML設定におけるparameter_rulesで定義されています。

      • つまり、tool calling のためのツールリストを指定します。

      • stop (array[string]) [optional] ストップシーケンス

        モデルが出力を返す際に、定義された文字列の前で出力を停止します。

      • stream (bool) ストリーム出力の有無、デフォルトはTrue

      • user (string) [optional] ユーザーの一意の識別子

        供給業者が不正行為を監視および検出するのに役立ちます。

    • 返り値

  • 入力tokenの事前計算

    モデルがtokenの事前計算インターフェースを提供していない場合、直接0を返すことができます。

    def get_num_tokens(self, model: str, credentials: dict, prompt_messages: list[PromptMessage],
                       tools: Optional[list[PromptMessageTool]] = None) -> int:
        """
        Get number of tokens for given prompt messages
    
        :param model: model name
        :param credentials: model credentials
        :param prompt_messages: prompt messages
        :param tools: tools for tool calling
        :return:
        """

    パラメータの説明は上記の LLMの呼び出し を参照してください。

    このインターフェースは、対応するmodelに基づいて適切なtokenizerを選択して計算する必要があります。対応するモデルがtokenizerを提供していない場合は、AIModelベースクラスの_get_num_tokens_by_gpt2(text: str)メソッドを使用して計算できます。

  • カスタムモデルスキーマの取得 [オプション]

    def get_customizable_model_schema(self, model: str, credentials: dict) -> Optional[AIModelEntity]:
        """
        Get customizable model schema
    
        :param model: model name
        :param credentials: model credentials
        :return: model schema
        """

供給業者がカスタムLLMを追加することをサポートしている場合、このメソッドを実装してカスタムモデルがモデル規則を取得できるようにすることができます。デフォルトではNoneを返します。

TextEmbedding

__base.text_embedding_model.TextEmbeddingModelベースクラスを継承し、次のインターフェースを実装します:

  • Embeddingの呼び出し

    def _invoke(self, model: str, credentials: dict,
                texts: list[str], user: Optional[str] = None) \
            -> TextEmbeddingResult:
        """
        Invoke large language model
    
        :param model: model name
        :param credentials: model credentials
        :param texts: texts to embed
        :param user: unique user id
        :return: embeddings result
        """
    • パラメータ:

      • model (string) モデル名

      • credentials (object) 資格情報

        資格情報のパラメータは、供給業者の YAML 構成ファイルの provider_credential_schema または model_credential_schema で定義されており、api_key などの詳細が含まれます。

      • texts (array[string]) テキストリスト,バッチで処理できる

      • user (string) [optional] ユーザーの一意の識別子

        供給業者が不正行為を監視および検出するのに役立ちます。

    • 返り値:

  • tokensの事前計算

    def get_num_tokens(self, model: str, credentials: dict, texts: list[str]) -> int:
        """
        Get number of tokens for given prompt messages
    
        :param model: model name
        :param credentials: model credentials
        :param texts: texts to embed
        :return:
        """

    パラメータの説明は上記の Embeddingの呼び出し を参照してください。

    上記のLargeLanguageModelと同様に、このインターフェースは、対応するmodelに基づいて適切なtokenizerを選択して計算する必要があります。対応するモデルがtokenizerを提供していない場合は、AIModelベースクラスの_get_num_tokens_by_gpt2(text: str)メソッドを使用して計算できます。

Rerank

`__base.rerank_model.RerankModelベースクラスを継承し、次のインターフェースを実装します:

  • rerankの呼び出し

    def _invoke(self, model: str, credentials: dict,
                query: str, docs: list[str], score_threshold: Optional[float] = None, top_n: Optional[int] = None,
                user: Optional[str] = None) \
            -> RerankResult:
        """
        Invoke rerank model
    
        :param model: model name
        :param credentials: model credentials
        :param query: search query
        :param docs: docs for reranking
        :param score_threshold: score threshold
        :param top_n: top n
        :param user: unique user id
        :return: rerank result
        """
    • パラメータ:

      • model (string) モデル名

      • credentials (object) 資格情報

        資格情報のパラメータは、供給業者の YAML 構成ファイルの provider_credential_schema または model_credential_schema で定義されており、api_key などの詳細が含まれます。

      • query (string) リクエスト内容をチェックする

      • docs (array[string]) 並べ替えが必要なセクションリスト

      • score_threshold (float) [optional] Scoreの閾値

      • top_n (int) [optional] トップのnセクションを取得します

      • user (string) [optional] ユーザーの一意の識別子

        供給業者が不正行為を監視および検出するのに役立ちます。

    • 返り値:

Speech2text

__base.speech2text_model.Speech2TextModel基底クラスを継承し、以下のインターフェースを実装します:

  • Invokeの呼び出し

    def _invoke(self, model: str, credentials: dict,
                file: IO[bytes], user: Optional[str] = None) \
            -> str:
        """
        Invoke large language model
    
        :param model: model name
        :param credentials: model credentials
        :param file: audio file
        :param user: unique user id
        :return: text for given audio file
        """	
    • パラメータ:

      • model (string) モデル名

      • credentials (object) 資格情報

        資格情報のパラメータは、供給業者の YAML 構成ファイルの provider_credential_schema または model_credential_schema で定義されており、api_key などの詳細が含まれます。

      • file (File) ファイルストリーム

      • user (string) [optional] ユーザーの一意の識別子

        供給業者が不正行為を監視および検出するのに役立ちます。

    • 返り値:

      音声をテキストに変換した結果を返します。

Text2speech

__base.text2speech_model.Text2SpeechModel基底クラスを継承し、以下のインターフェースを実装します:

  • Invokeの呼び出し

    def _invoke(self, model: str, credentials: dict, content_text: str, streaming: bool, user: Optional[str] = None):
        """
        Invoke large language model
    
        :param model: model name
        :param credentials: model credentials
        :param content_text: text content to be translated
        :param streaming: output is streaming
        :param user: unique user id
        :return: translated audio file
        """	
    • パラメータ:

      • model (string) モデル名

      • credentials (object) 資格情報

        資格情報のパラメータは、供給業者の YAML 構成ファイルの provider_credential_schema または model_credential_schema で定義されており、api_key などの詳細が含まれます。

      • content_text (string) 変換すべきテキストコンテンツ

      • streaming (bool) ストリーミング出力かどうか

      • user (string) [optional] ユーザーの一意の識別子

        供給業者が不正行為を監視および検出するのに役立ちます。

    • 返り値:

      テキストを音声に変換した結果を返します。

Moderation

__base.moderation_model.ModerationModel基底クラスを継承し、以下のインターフェースを実装します:

  • Invokeの呼び出し

    def _invoke(self, model: str, credentials: dict,
                text: str, user: Optional[str] = None) \
            -> bool:
        """
        Invoke large language model
    
        :param model: model name
        :param credentials: model credentials
        :param text: text to moderate
        :param user: unique user id
        :return: false if text is safe, true otherwise
        """
    • パラメータ:

      • model (string) モデル名

      • credentials (object) 資格情報

        資格情報のパラメータは、供給業者の YAML 構成ファイルの provider_credential_schema または model_credential_schema で定義されており、api_key などの詳細が含まれます。

      • text (string) テキスト内容

      • user (string) [optional] ユーザーの一意の識別子

        供給業者が不正行為を監視および検出するのに役立ちます。

    • 返り値:

      False の場合は入力したテキストは安全であり、True の場合はその逆。

エンティティ

PromptMessageRole

メッセージロールを定義する列挙型。

class PromptMessageRole(Enum):
    """
    Enum class for prompt message.
    """
    SYSTEM = "system"
    USER = "user"
    ASSISTANT = "assistant"
    TOOL = "tool"

PromptMessageContentType

メッセージコンテンツのタイプを定義し、テキストと画像の2種類がある。

class PromptMessageContentType(Enum):
    """
    Enum class for prompt message content type.
    """
    TEXT = 'text'
    IMAGE = 'image'

PromptMessageContent

メッセージコンテンツの基底クラスであり、パラメータのみを宣言するため初期化は行えない。

class PromptMessageContent(BaseModel):
    """
    Model class for prompt message content.
    """
    type: PromptMessageContentType
    data: str  # コンテンツデータ

現在、テキストと画像の2つのタイプがサポートされており、テキストと複数の画像を同時に渡すことができる。

テキストと画像を同時に渡す場合は、TextPromptMessageContent と ImagePromptMessageContent をそれぞれ初期化する必要がある。

TextPromptMessageContent

class TextPromptMessageContent(PromptMessageContent):
    """
    Model class for text prompt message content.
    """
    type: PromptMessageContentType = PromptMessageContentType.TEXT

画像とテキストを一緒に渡す場合、テキストは content リストの一部としてこのエンティティを構築する必要がある。

ImagePromptMessageContent

class ImagePromptMessageContent(PromptMessageContent):
    """
    Model class for image prompt message content.
    """
    class DETAIL(Enum):
        LOW = 'low'
        HIGH = 'high'

    type: PromptMessageContentType = PromptMessageContentType.IMAGE
    detail: DETAIL = DETAIL.LOW  # 解像度

画像とテキストを一緒に渡す場合、画像は content リストの一部としてこのエンティティを構築する必要がある。

data には url または画像の base64 でエンコードされた文字列を指定することができる。

PromptMessage

すべてのロールメッセージの基底クラスであり、パラメータのみを宣言するため初期化はできません。

class PromptMessage(ABC, BaseModel):
    """
    Model class for prompt message.
    """
    role: PromptMessageRole  # メッセージロール
    content: Optional[str | list[PromptMessageContent]] = None  # 2つのタイプ、文字列とコンテンツリストをサポート。コンテンツリストはマルチモーダルのニーズを満たすために使用されます。PromptMessageContentの説明を参照してください。
    name: Optional[str] = None  # 名前、オプション

UserPromptMessage

UserMessage ユーザーメッセージを表すクラス。

class UserPromptMessage(PromptMessage):
    """
    Model class for user prompt message.
    """
    role: PromptMessageRole = PromptMessageRole.USER

AssistantPromptMessage

モデルの返信メッセージを表し、通常は few-shots やチャット履歴が入力として使用されます。

class AssistantPromptMessage(PromptMessage):
    """
    Model class for assistant prompt message.
    """
    class ToolCall(BaseModel):
        """
        Model class for assistant prompt message tool call.
        """
        class ToolCallFunction(BaseModel):
            """
            Model class for assistant prompt message tool call function.
            """
            name: str  # ツール名
            arguments: str  # ツールパラメータ

        id: str  # ツールID。OpenAI tool callの場合のみ有効で、ツール呼び出しのユニークIDです。同じツールを複数回呼び出すことができます。
        type: str  # デフォルト function
        function: ToolCallFunction  # ツール呼び出し情報

    role: PromptMessageRole = PromptMessageRole.ASSISTANT
    tool_calls: list[ToolCall] = []  # モデルの返信としてのツール呼び出し結果(`tools`を渡した場合のみ、モデルがツール呼び出しが必要と判断した場合に返されます)

tool_calls は、モデルに tools を渡した後、モデルが返す tool call のリストです。

SystemPromptMessage

システムメッセージを表し、通常はモデルに与えられるシステム命令に使用されます。

class SystemPromptMessage(PromptMessage):
    """
    Model class for system prompt message.
    """
    role: PromptMessageRole = PromptMessageRole.SYSTEM

ToolPromptMessage

ツールメッセージを表し、ツールの実行結果をモデルに渡して次のステップの計画を行います。

class ToolPromptMessage(PromptMessage):
    """
    Model class for tool prompt message.
    """
    role: PromptMessageRole = PromptMessageRole.TOOL
    tool_call_id: str  # ツール呼び出しID。OpenAI tool callをサポートしない場合、ツール名を渡すこともできます。

基类的 content 传入工具执行结果。

PromptMessageTool

class PromptMessageTool(BaseModel):
    """
    Model class for prompt message tool.
    """
    name: str  # ツール名
    description: str  # ツールの説明
    parameters: dict  # ツールパラメータ dict

LLMResult

class LLMResult(BaseModel):
    """
    Model class for llm result.
    """
    model: str  # 使用された実際のモデル
    prompt_messages: list[PromptMessage]  # プロンプトメッセージのリスト
    message: AssistantPromptMessage  # 返信メッセージ
    usage: LLMUsage  # 使用したtokenとコスト情報
    system_fingerprint: Optional[str] = None  # リクエスト指紋。OpenAIのこのパラメータの定義を参照。

LLMResultChunkDelta

ストリーム化された各イテレーション内の delta エンティティ。

class LLMResultChunkDelta(BaseModel):
    """
    Model class for llm result chunk delta.
    """
    index: int  # インデックス
    message: AssistantPromptMessage  # 返信メッセージ
    usage: Optional[LLMUsage] = None  # 使用したトークンとコスト情報(最後の1つのみ)
    finish_reason: Optional[str] = None  # 終了理由(最後の1つのみ)

LLMResultChunk

ストリーム化された各イテレーションのエンティティ。

class LLMResultChunk(BaseModel):
    """
    Model class for llm result chunk.
    """
    model: str  # 実際に使用したモデル
    prompt_messages: list[PromptMessage]  # プロンプトメッセージのリスト
    system_fingerprint: Optional[str] = None  # リクエスト指紋。OpenAIのこのパラメータの定義を参照。
    delta: LLMResultChunkDelta  # 各イテレーションの変更が存在する内容

LLMUsage

class LLMUsage(ModelUsage):
    """
    Model class for llm usage.
    """
    prompt_tokens: int  # プロンプトで使用したトークン数
    prompt_unit_price: Decimal  # プロンプトの単価
    prompt_price_unit: Decimal  # プロンプト料金の単位(単価が基づいているトークンの量)
    prompt_price: Decimal  # プロンプトの料金
    completion_tokens: int  # 返答で使用したトークン数
    completion_unit_price: Decimal  # 返答の単価
    completion_price_unit: Decimal  # 返答料金の単位(単価が基づいているトークンの量)
    completion_price: Decimal  # 返答の料金
    total_tokens: int  # 総使用トークン数
    total_price: Decimal  # 総料金
    currency: str  # 通貨単位
    latency: float  # リクエスト処理時間(秒)

TextEmbeddingResult

class TextEmbeddingResult(BaseModel):
    """
    Model class for text embedding result.
    """
    model: str  # 実際に使用したモデル
    embeddings: list[list[float]]  # テキストリストに対応するembeddingベクトルのリスト
    usage: EmbeddingUsage  # 使用した情報

EmbeddingUsage

class EmbeddingUsage(ModelUsage):
    """
    Model class for embedding usage.
    """
    tokens: int  # 使用した token 数
    total_tokens: int  # 総使用 token 数
    unit_price: Decimal  # 単価
    price_unit: Decimal  # 価格の単位(単価が基づいているトークンの量)
    total_price: Decimal  # 総料金
    currency: str  # 通貨単位
    latency: float  # リクエスト処理時間(s)

RerankResult

class RerankResult(BaseModel):
    """
    Model class for rerank result.
    """
    model: str  # 実際に使用したモデル
    docs: list[RerankDocument]  # Rerankされたセグメントリスト

RerankDocument

class RerankDocument(BaseModel):
    """
    Model class for rerank document.
    """
    index: int  # 元の文書の順番
    text: str  # 文書のテキスト内容
    score: float  # スコア

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.

    def validate_credentials(self, model: str, credentials: dict) -> None:
        """
        Validate model credentials
    
        :param model: model name
        :param credentials: model credentials
        :return:
        """

    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

    @property
    def _invoke_error_mapping(self) -> dict[type[InvokeError], list[type[Exception]]]:
        """
        Map model invoke error to unified error
        The key is the error type thrown to the caller
        The value is the error type thrown by the model,
        which needs to be converted into a unified error type for the caller.
    
        :return: Invoke error mapping
        """

​ 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.

    def _invoke(self, model: str, credentials: dict,
                prompt_messages: list[PromptMessage], model_parameters: dict,
                tools: Optional[list[PromptMessageTool]] = None, stop: Optional[List[str]] = None,
                stream: bool = True, user: Optional[str] = None) \
            -> Union[LLMResult, Generator]:
        """
        Invoke large language model
    
        :param model: model name
        :param credentials: model credentials
        :param prompt_messages: prompt messages
        :param model_parameters: model parameters
        :param tools: tools for tool calling
        :param stop: stop words
        :param stream: is stream response
        :param user: unique user id
        :return: full response or stream response chunk generator result
        """
    • 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.

      • model_parameters (object) Model parameters

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

      • 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

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

        This can help the provider monitor and detect abusive behavior.

    • Returns

  • Pre-calculating Input Tokens

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

    def get_num_tokens(self, model: str, credentials: dict, prompt_messages: list[PromptMessage],
                       tools: Optional[list[PromptMessageTool]] = None) -> int:
        """
        Get number of tokens for given prompt messages
    
        :param model: model name
        :param credentials: model credentials
        :param prompt_messages: prompt messages
        :param tools: tools for tool calling
        :return:
        """

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

  • Fetch Custom Model Schema [Optional]

    def get_customizable_model_schema(self, model: str, credentials: dict) -> Optional[AIModelEntity]:
        """
        Get customizable model schema
    
        :param model: model name
        :param credentials: model credentials
        :return: model schema
        """

    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

    def _invoke(self, model: str, credentials: dict,
                texts: list[str], user: Optional[str] = None) \
            -> TextEmbeddingResult:
        """
        Invoke large language model
    
        :param model: model name
        :param credentials: model credentials
        :param texts: texts to embed
        :param user: unique user id
        :return: embeddings result
        """
    • 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:

  • Pre-calculating Tokens

    def get_num_tokens(self, model: str, credentials: dict, texts: list[str]) -> int:
        """
        Get number of tokens for given prompt messages
    
        :param model: model name
        :param credentials: model credentials
        :param texts: texts to embed
        :return:
        """

    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

    def _invoke(self, model: str, credentials: dict,
                query: str, docs: list[str], score_threshold: Optional[float] = None, top_n: Optional[int] = None,
                user: Optional[str] = None) \
            -> RerankResult:
        """
        Invoke rerank model
    
        :param model: model name
        :param credentials: model credentials
        :param query: search query
        :param docs: docs for reranking
        :param score_threshold: score threshold
        :param top_n: top n
        :param user: unique user id
        :return: rerank result
        """
    • 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:

Speech2text

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

  • Invoke Invocation

    def _invoke(self, model: str, credentials: dict, file: IO[bytes], user: Optional[str] = None) -> str:
        """
        Invoke large language model
    
        :param model: model name
        :param credentials: model credentials
        :param file: audio file
        :param user: unique user id
        :return: text for given audio file
        """	
    • 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

    def _invoke(self, model: str, credentials: dict, content_text: str, streaming: bool, user: Optional[str] = None):
        """
        Invoke large language model
    
        :param model: model name
        :param credentials: model credentials
        :param content_text: text content to be translated
        :param streaming: output is streaming
        :param user: unique user id
        :return: translated audio file
        """	
    • 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

    def _invoke(self, model: str, credentials: dict,
                text: str, user: Optional[str] = None) \
            -> bool:
        """
        Invoke large language model
    
        :param model: model name
        :param credentials: model credentials
        :param text: text to moderate
        :param user: unique user id
        :return: false if text is safe, true otherwise
        """
    • 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

class PromptMessageRole(Enum):
    """
    Enum class for prompt message.
    """
    SYSTEM = "system"
    USER = "user"
    ASSISTANT = "assistant"
    TOOL = "tool"

PromptMessageContentType

Message content types, divided into text and image.

class PromptMessageContentType(Enum):
    """
    Enum class for prompt message content type.
    """
    TEXT = 'text'
    IMAGE = 'image'

PromptMessageContent

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

class PromptMessageContent(BaseModel):
    """
    Model class for prompt message content.
    """
    type: PromptMessageContentType
    data: str

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

class TextPromptMessageContent(PromptMessageContent):
    """
    Model class for text prompt message content.
    """
    type: PromptMessageContentType = PromptMessageContentType.TEXT

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

ImagePromptMessageContent

class ImagePromptMessageContent(PromptMessageContent):
    """
    Model class for image prompt message content.
    """
    class DETAIL(Enum):
        LOW = 'low'
        HIGH = 'high'

    type: PromptMessageContentType = PromptMessageContentType.IMAGE
    detail: DETAIL = DETAIL.LOW  # Resolution

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.

class PromptMessage(ABC, BaseModel):
    """
    Model class for prompt message.
    """
    role: PromptMessageRole
    content: Optional[str | list[PromptMessageContent]] = None  # Supports two types: string and content list. The content list is designed to meet the needs of multimodal inputs. For more details, see the PromptMessageContent explanation.
    name: Optional[str] = None

UserPromptMessage

UserMessage message body, representing a user's message.

class UserPromptMessage(PromptMessage):
    """
    Model class for user prompt message.
    """
    role: PromptMessageRole = PromptMessageRole.USER

AssistantPromptMessage

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

class AssistantPromptMessage(PromptMessage):
    """
    Model class for assistant prompt message.
    """
    class ToolCall(BaseModel):
        """
        Model class for assistant prompt message tool call.
        """
        class ToolCallFunction(BaseModel):
            """
            Model class for assistant prompt message tool call function.
            """
            name: str  # tool name
            arguments: str  # tool arguments

        id: str  # Tool ID, effective only in OpenAI tool calls. It's the unique ID for tool invocation and the same tool can be called multiple times.
        type: str  # default: function
        function: ToolCallFunction  # tool call information

    role: PromptMessageRole = PromptMessageRole.ASSISTANT
    tool_calls: list[ToolCall] = []  # The result of tool invocation in response from the model (returned only when tools are input and the model deems it necessary to invoke a tool).

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.

class SystemPromptMessage(PromptMessage):
    """
    Model class for system prompt message.
    """
    role: PromptMessageRole = PromptMessageRole.SYSTEM

ToolPromptMessage

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

class ToolPromptMessage(PromptMessage):
    """
    Model class for tool prompt message.
    """
    role: PromptMessageRole = PromptMessageRole.TOOL
    tool_call_id: str  # Tool invocation ID. If OpenAI tool call is not supported, the name of the tool can also be inputted.

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

PromptMessageTool

class PromptMessageTool(BaseModel):
    """
    Model class for prompt message tool.
    """
    name: str
    description: str
    parameters: dict

LLMResult

class LLMResult(BaseModel):
    """
    Model class for llm result.
    """
    model: str  # Actual used modele
    prompt_messages: list[PromptMessage]  # prompt messages
    message: AssistantPromptMessage  # response message
    usage: LLMUsage  # usage info
    system_fingerprint: Optional[str] = None  # request fingerprint, refer to OpenAI definition

LLMResultChunkDelta

In streaming returns, each iteration contains the delta entity.

class LLMResultChunkDelta(BaseModel):
    """
    Model class for llm result chunk delta.
    """
    index: int
    message: AssistantPromptMessage  # response message
    usage: Optional[LLMUsage] = None  # usage info
    finish_reason: Optional[str] = None  # finish reason, only the last one returns

LLMResultChunk

Each iteration entity in streaming returns.

class LLMResultChunk(BaseModel):
    """
    Model class for llm result chunk.
    """
    model: str  # Actual used modele
    prompt_messages: list[PromptMessage]  # prompt messages
    system_fingerprint: Optional[str] = None  # request fingerprint, refer to OpenAI definition
    delta: LLMResultChunkDelta

LLMUsage

class LLMUsage(ModelUsage):
    """
    Model class for LLM usage.
    """
    prompt_tokens: int  # Tokens used for prompt
    prompt_unit_price: Decimal  # Unit price for prompt
    prompt_price_unit: Decimal  # Price unit for prompt, i.e., the unit price based on how many tokens
    prompt_price: Decimal  # Cost for prompt
    completion_tokens: int  # Tokens used for response
    completion_unit_price: Decimal  # Unit price for response
    completion_price_unit: Decimal  # Price unit for response, i.e., the unit price based on how many tokens
    completion_price: Decimal  # Cost for response
    total_tokens: int  # Total number of tokens used
    total_price: Decimal  # Total cost
    currency: str  # Currency unit
    latency: float  # Request latency (s)

TextEmbeddingResult

class TextEmbeddingResult(BaseModel):
    """
    Model class for text embedding result.
    """
    model: str  # Actual model used
    embeddings: list[list[float]]  # List of embedding vectors, corresponding to the input texts list
    usage: EmbeddingUsage  # Usage information

EmbeddingUsage

class EmbeddingUsage(ModelUsage):
    """
    Model class for embedding usage.
    """
    tokens: int  # Number of tokens used
    total_tokens: int  # Total number of tokens used
    unit_price: Decimal  # Unit price
    price_unit: Decimal  # Price unit, i.e., the unit price based on how many tokens
    total_price: Decimal  # Total cost
    currency: str  # Currency unit
    latency: float  # Request latency (s)

RerankResult

class RerankResult(BaseModel):
    """
    Model class for rerank result.
    """
    model: str  # Actual model used
    docs: list[RerankDocument]  # Reranked document list	

RerankDocument

class RerankDocument(BaseModel):
    """
    Model class for rerank document.
    """
    index: int  # original index
    text: str
    score: float
Previousカスタムモデルの追加Next配置ルール

Last updated 7 months ago

prompt_messages (array[]) Prompt リスト

モデルのタイプが Completion の場合、リストには1つの 要素のみを渡す必要があります;

モデルのタイプが Chat の場合、, , , 要素のリストをメッセージに応じて渡す必要があります。

tools (array[]) [optional] ツールのリスト,function calling 内の function に相当します。

ストリーム出力の場合は Generator[],出力ではないの場合は 。

ストリーム出力の場合は Generator[],出力ではないの場合は 。

ほとんどの微調整モデルはOpenAI供給業者の下で、微調整モデル名を使用してベースモデルを取得できます。例えば、gpt-3.5-turbo-1106のような微調整モデル名を使用して、基本モデルの事前定義されたパラメータルールを取得できます。具体的な実装については、を参照してください。

エンティティ。

エンティティ。

prompt_messages (array[]) List of prompts

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

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

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

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

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

entity.

entity.

PromptMessage
UserPromptMessage
SystemPromptMessage
UserPromptMessage
AssistantPromptMessage
ToolPromptMessage
PromptMessageTool
LLMResultChunk
LLMResult
LLMResultChunk
LLMResult
openai
TextEmbeddingResult
RerankResult
PromptMessage
UserPromptMessage
SystemPromptMessage
UserPromptMessage
AssistantPromptMessage
ToolPromptMessage
PromptMessageTool
LLMResultChunk
LLMResult
LLMResultChunk
LLMResult
TextEmbeddingResult
RerankResult