--- dimensions: type: primary: implementation detail: advanced level: intermediate standard_title: Reverse Invocation Model language: ja title: モデル description: このドキュメントでは、プラグインがDifyプラットフォーム内のモデルサービスをリバースコールする方法について詳しく説明します。内容は、LLM、Summary、TextEmbedding、Rerank、TTS、Speech2Text、Moderationなどのモデルをリバースコールする具体的な方法を含み、各モデルの呼び出しには、対応するエントリポイント、インターフェースパラメータの説明、および実際の使用例コードが付随しており、モデル呼び出しのベストプラクティスも提供しています。 --- リバースコールモデルとは、プラグインがDify内のLLM(TTS、Rerankなど、プラットフォーム内のすべてのモデルタイプと機能を含む)をリバースコールする能力を指します。リバースコールの基本概念にまだ慣れていない場合は、まず[Difyサービスのリバースコール](/plugin-dev-ja/9241-reverse-invocation)をお読みください。 ただし、モデルの呼び出しには `ModelConfig` 型のパラメータを渡す必要があり、その構造は[一般的な仕様定義](/plugin-dev-ja/0411-general-specifications)を参照できます。また、モデルの種類によって、この構造にはわずかな違いがあることに注意してください。 例えば、`LLM` タイプのモデルの場合、`completion_params` と `mode` パラメータも含まれている必要があり、この構造を手動で構築するか、`model-selector` タイプのパラメータまたは設定を使用できます。 ### LLMの呼び出し #### **エントリポイント** ```python self.session.model.llm ``` #### **エンドポイント** ```python 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` モデルを呼び出したい場合は、以下のサンプルコードを参照してください。 ```python 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` パラメータを追加できます。 ```yaml identity: name: llm author: Dify label: en_US: LLM zh_Hans: LLM pt_BR: LLM ja: 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 ja: 大規模言語モデルを呼び出すためのツール 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 ja: プロンプト文字列 human_description: en_US: used for searching zh_Hans: 用于搜索网页内容 pt_BR: used for searching ja: ウェブコンテンツの検索に使用 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 ja: 使用するモデル human_description: en_US: Model zh_Hans: 使用的模型 pt_BR: Model ja: 使用するモデル llm_description: which Model to invoke form: form extra: python: source: tools/llm.py ``` この例では `model` の `scope` が `llm` に指定されていることに注意してください。この場合、ユーザーは `llm` タイプのパラメータしか選択できず、上記の使用例のコードを次のように変更できます。 ```python 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') ) ], 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の呼び出し このエンドポイントにリクエストしてテキストを要約することができます。現在のワークスペース内のシステムモデルを使用してテキストを要約します。 **エントリポイント** ```python self.session.model.summary ``` **エンドポイント** * `text` は要約されるテキストです。 * `instruction` は追加したい指示であり、これによりテキストを特定のスタイルで要約できます。 ```python def invoke( self, text: str, instruction: str, ) -> str: ``` ### TextEmbeddingの呼び出し **エントリポイント** ```python self.session.model.text_embedding ``` **エンドポイント** ```python def invoke( self, model_config: TextEmbeddingResult, texts: list[str] ) -> TextEmbeddingResult: pass ``` ### Rerankの呼び出し **エントリポイント** ```python self.session.model.rerank ``` **エンドポイント** ```python def invoke( self, model_config: RerankModelConfig, docs: list[str], query: str ) -> RerankResult: pass ``` ### TTSの呼び出し **エントリポイント** ```python self.session.model.tts ``` **エンドポイント** ```python def invoke( self, model_config: TTSModelConfig, content_text: str ) -> Generator[bytes, None, None]: pass ``` `tts` エンドポイントが返す `bytes` ストリームは `mp3` オーディオバイトストリームであり、各イテレーションで返されるのは完全なオーディオであることに注意してください。より高度な処理タスクを実行したい場合は、適切なライブラリを選択してください。 ### Speech2Textの呼び出し **エントリポイント** ```python self.session.model.speech2text ``` **エンドポイント** ```python def invoke( self, model_config: Speech2TextModelConfig, file: IO[bytes] ) -> str: pass ``` ここで `file` は `mp3` 形式でエンコードされたオーディオファイルです。 ### Moderationの呼び出し **エントリポイント** ```python self.session.model.moderation ``` **エンドポイント** ```python def invoke(self, model_config: ModerationModelConfig, text: str) -> bool: pass ``` このエンドポイントが `true` を返した場合、`text` に機密コンテンツが含まれていることを示します。 ## 関連リソース - [Difyサービスのリバースコール](/plugin-dev-ja/9241-reverse-invocation) - リバースコールの基本概念を理解する - [Appのリバースコール](/plugin-dev-ja/9242-reverse-invocation-app) - プラットフォーム内のAppを呼び出す方法を理解する - [Toolのリバースコール](/plugin-dev-ja/9242-reverse-invocation-tool) - 他のプラグインを呼び出す方法を理解する - [モデルプラグイン開発ガイド](/plugin-dev-ja/0211-getting-started-new-model) - カスタムモデルプラグインの開発方法を学ぶ - [モデル設計規則](/plugin-dev-ja/0411-model-designing-rules) - モデルプラグインの設計原則を理解する {/* Contributing Section DO NOT edit this section! It will be automatically generated by the script. */} --- [このページを編集する](https://github.com/langgenius/dify-docs/edit/main/plugin-dev-ja/9242-reverse-invocation-model.mdx) | [問題を報告する](https://github.com/langgenius/dify-docs/issues/new?template=docs.yml)