| dimensions |
|
||||||||
|---|---|---|---|---|---|---|---|---|---|
| standard_title | Reverse Invocation Model | ||||||||
| language | ja | ||||||||
| title | モデルの逆呼び出し | ||||||||
| description | このドキュメントでは、プラグインがDifyプラットフォーム内でモデルサービスを逆呼び出しする方法について詳しく説明します。LLM、Summary、TextEmbedding、Rerank、TTS、Speech2Text、Moderationモデルの逆呼び出しの具体的な方法を網羅しています。各モデルの呼び出しには、エントリーポイント、インターフェースパラメータの説明、実用的なコード例、モデル呼び出しのベストプラクティス推奨事項が含まれています。 |
モデルの逆呼び出しとは、プラグインがDifyの内部LLM機能を呼び出す能力を指し、TTS、Rerankなど、プラットフォーム内のすべてのモデルタイプと機能を含みます。逆呼び出しの基本概念に慣れていない場合は、まずDifyサービスの逆呼び出しをお読みください。
ただし、モデルを呼び出すにはModelConfig型のパラメータを渡す必要があることに注意してください。その構造は一般仕様定義で参照でき、この構造はモデルの種類によってわずかに異なります。
例えば、LLM型のモデルでは、completion_paramsとmodeパラメータも含める必要があります。この構造を手動で構築するか、model-selector型のパラメータまたは設定を使用できます。
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この例では、modelのscopeがllmとして指定されていることに注意してください。これにより、ユーザーは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)このエンドポイントにリクエストして、テキストを要約できます。現在のワークスペース内のシステムモデルを使用してテキストを要約します。
エントリーポイント
self.session.model.summaryエンドポイント
textは要約するテキストです。instructionは追加したい追加の指示で、テキストをスタイル的に要約できます。
def invoke(
self, text: str, instruction: str,
) -> str:エントリーポイント
self.session.model.text_embeddingエンドポイント
def invoke(
self, model_config: TextEmbeddingResult, texts: list[str]
) -> TextEmbeddingResult:
passエントリーポイント
self.session.model.rerankエンドポイント
def invoke(
self, model_config: RerankModelConfig, docs: list[str], query: str
) -> RerankResult:
passエントリーポイント
self.session.model.ttsエンドポイント
def invoke(
self, model_config: TTSModelConfig, content_text: str
) -> Generator[bytes, None, None]:
passttsエンドポイントが返すbytesストリームはmp3オーディオバイトストリームであることに注意してください。各イテレーションは完全なオーディオセグメントを返します。より詳細な処理タスクを実行したい場合は、適切なライブラリを選択してください。
エントリーポイント
self.session.model.speech2textエンドポイント
def invoke(
self, model_config: Speech2TextModelConfig, file: IO[bytes]
) -> str:
passここでfileはmp3形式でエンコードされたオーディオファイルです。
エントリーポイント
self.session.model.moderationエンドポイント
def invoke(self, model_config: ModerationModelConfig, text: str) -> bool:
passこのエンドポイントがtrueを返す場合、textに機密コンテンツが含まれていることを示します。
- Difyサービスの逆呼び出し - 逆呼び出しの基本概念を理解する
- アプリの逆呼び出し - プラットフォーム内でアプリを呼び出す方法を学ぶ
- ツールの逆呼び出し - 他のプラグインを呼び出す方法を学ぶ
- モデルプラグイン開発ガイド - カスタムモデルプラグインの開発方法を学ぶ
- モデル設計ルール - モデルプラグインの設計原則を理解する
{/* Contributing Section DO NOT edit this section! It will be automatically generated by the script. */}