メインコンテンツへスキップ
⚠️ このドキュメントはAIによって自動翻訳されています。不正確な部分がある場合は、英語版を参照してください。
モデルの逆呼び出しとは、プラグインがDifyの内部LLM機能を呼び出す能力を指し、TTS、Rerankなど、プラットフォーム内のすべてのモデルタイプと機能を含みます。逆呼び出しの基本概念に慣れていない場合は、まずDifyサービスの逆呼び出しをお読みください。 ただし、モデルを呼び出すにはModelConfig型のパラメータを渡す必要があることに注意してください。その構造は一般仕様定義で参照でき、この構造はモデルの種類によってわずかに異なります。 例えば、LLM型のモデルでは、completion_paramsmodeパラメータも含める必要があります。この構造を手動で構築するか、model-selector型のパラメータまたは設定を使用できます。

LLMの呼び出し

エントリーポイント

    self.session.model.llm

エンドポイント

    def invoke(
        self,
        model_config: LLMModelConfig,
        prompt_messages: list[PromptMessage],
        tools: list[PromptMessageTool] | None = None,
        stop: list[str] | None = None,
        stream: bool = True,
    ) -> Generator[LLMResultChunk, None, None] | LLMResult:
        pass
呼び出すモデルにtool_call機能がない場合、ここで渡されるtoolsは有効にならないことに注意してください。

使用例

Tool内でOpenAIのgpt-4o-miniモデルを呼び出したい場合は、以下のサンプルコードを参照してください:
from collections.abc import Generator
from typing import Any

from dify_plugin import Tool
from dify_plugin.entities.model.llm import LLMModelConfig
from dify_plugin.entities.tool import ToolInvokeMessage
from dify_plugin.entities.model.message import SystemPromptMessage, UserPromptMessage

class LLMTool(Tool):
    def _invoke(self, tool_parameters: dict[str, Any]) -> Generator[ToolInvokeMessage]:
        response = self.session.model.llm.invoke(
            model_config=LLMModelConfig(
                provider='openai',
                model='gpt-4o-mini',
                mode='chat',
                completion_params={}
            ),
            prompt_messages=[
                SystemPromptMessage(
                    content='you are a helpful assistant'
                ),
                UserPromptMessage(
                    content=tool_parameters.get('query')
                )
            ],
            stream=True
        )

        for chunk in response:
            if chunk.delta.message:
                assert isinstance(chunk.delta.message.content, str)
                yield self.create_text_message(text=chunk.delta.message.content)
コード内でtool_parametersからqueryパラメータが渡されていることに注意してください。

ベストプラクティス

LLMModelConfigを手動で構築することは推奨されません。代わりに、ユーザーがUI上で使用したいモデルを選択できるようにしてください。この場合、以下のようにmodelパラメータを追加してツールのパラメータリストを変更できます:
identity:
  name: llm
  author: Dify
  label:
    en_US: LLM
    zh_Hans: LLM
    pt_BR: LLM
description:
  human:
    en_US: A tool for invoking a large language model
    zh_Hans: 用于调用大型语言模型的工具
    pt_BR: A tool for invoking a large language model
  llm: A tool for invoking a large language model
parameters:
  - name: prompt
    type: string
    required: true
    label:
      en_US: Prompt string
      zh_Hans: 提示字符串
      pt_BR: Prompt string
    human_description:
      en_US: used for searching
      zh_Hans: 用于搜索网页内容
      pt_BR: used for searching
    llm_description: key words for searching
    form: llm
  - name: model
    type: model-selector
    scope: llm
    required: true
    label:
      en_US: Model
      zh_Hans: 使用的模型
      pt_BR: Model
    human_description:
      en_US: Model
      zh_Hans: 使用的模型
      pt_BR: Model
    llm_description: which Model to invoke
    form: form
extra:
  python:
    source: tools/llm.py
この例では、modelscopellmとして指定されていることに注意してください。これにより、ユーザーはllm型のパラメータのみを選択できます。したがって、前の使用例のコードは以下のように変更できます:
from collections.abc import Generator
from typing import Any

from dify_plugin import Tool
from dify_plugin.entities.model.llm import LLMModelConfig
from dify_plugin.entities.tool import ToolInvokeMessage
from dify_plugin.entities.model.message import SystemPromptMessage, UserPromptMessage

class LLMTool(Tool):
    def _invoke(self, tool_parameters: dict[str, Any]) -> Generator[ToolInvokeMessage]:
        response = self.session.model.llm.invoke(
            model_config=tool_parameters.get('model'),
            prompt_messages=[
                SystemPromptMessage(
                    content='you are a helpful assistant'
                ),
                UserPromptMessage(
                    content=tool_parameters.get('query') # Assuming 'query' is still needed, otherwise use 'prompt' from parameters
                )
            ],
            stream=True
        )

        for chunk in response:
            if chunk.delta.message:
                assert isinstance(chunk.delta.message.content, str)
                yield self.create_text_message(text=chunk.delta.message.content)

Summaryの呼び出し

このエンドポイントにリクエストして、テキストを要約できます。現在のワークスペース内のシステムモデルを使用してテキストを要約します。 エントリーポイント
    self.session.model.summary
エンドポイント
  • textは要約するテキストです。
  • instructionは追加したい追加の指示で、テキストをスタイル的に要約できます。
    def invoke(
        self, text: str, instruction: str,
    ) -> str:

TextEmbeddingの呼び出し

エントリーポイント
    self.session.model.text_embedding
エンドポイント
    def invoke(
        self, model_config: TextEmbeddingResult, texts: list[str]
    ) -> TextEmbeddingResult:
        pass

Rerankの呼び出し

エントリーポイント
    self.session.model.rerank
エンドポイント
    def invoke(
        self, model_config: RerankModelConfig, docs: list[str], query: str
    ) -> RerankResult:
        pass

TTSの呼び出し

エントリーポイント
    self.session.model.tts
エンドポイント
    def invoke(
        self, model_config: TTSModelConfig, content_text: str
    ) -> Generator[bytes, None, None]:
        pass
ttsエンドポイントが返すbytesストリームはmp3オーディオバイトストリームであることに注意してください。各イテレーションは完全なオーディオセグメントを返します。より詳細な処理タスクを実行したい場合は、適切なライブラリを選択してください。

Speech2Textの呼び出し

エントリーポイント
    self.session.model.speech2text
エンドポイント
    def invoke(
        self, model_config: Speech2TextModelConfig, file: IO[bytes]
    ) -> str:
        pass
ここでfilemp3形式でエンコードされたオーディオファイルです。

Moderationの呼び出し

エントリーポイント
    self.session.model.moderation
エンドポイント
    def invoke(self, model_config: ModerationModelConfig, text: str) -> bool:
        pass
このエンドポイントがtrueを返す場合、textに機密コンテンツが含まれていることを示します。

関連リソース


Edit this page | Report an issue