Skip to main content
⚠️ 本文档由 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
请注意,在此示例中,modelscope 被指定为 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)

调用 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
其中 file 是以 mp3 格式编码的音频文件。

调用 Moderation

入口点
    self.session.model.moderation
接口
    def invoke(self, model_config: ModerationModelConfig, text: str) -> bool:
        pass
如果此接口返回 true,表示 text 包含敏感内容。

相关资源


编辑此页面 | 报告问题