当前位置: 首页 > news >正文

Dify中的预定义模型插件开发例子:以siliconflow为例

本文使用Dify v1.0.0-beta.1版本。模型插件结构基本是模型供应商(模型公司,比如siliconflowxinference)- 模型分类(模型类型,比如llmrerankspeech2texttext_embeddingtts)- 具体模型(比如,deepseek-v2.5)。本文以siliconflow为例,介绍Dify中的预定义模型插件开发例子。

一.siliconflow模型插件

SiliconCloud (MaaS) 简化AI模型的部署,同时提供强大的性能支持。通过该插件,用户可访问多种模型(如大语言模型、文本嵌入、重排序、语音转文本、文本转语音等),并可通过模型名称、API 密钥及其它参数进行配置。

1.多层模型分类

├─llm
│      deepdeek-coder-v2-instruct.yaml
│      deepseek-v2-chat.yaml
│      deepseek-v2.5.yaml
│      gemma-2-27b-it.yaml
│      gemma-2-9b-it.yaml
│      glm4-9b-chat.yaml
│      hunyuan-a52b-instruct.yaml
│      internlm2_5-20b-chat.yaml
│      internlm2_5-7b-chat.yaml
│      Internvl2-26b.yaml
│      Internvl2-8b.yaml
│      internvl2-llama3-76b.yaml
│      meta-mlama-3-70b-instruct.yaml
│      meta-mlama-3-8b-instruct.yaml
│      meta-mlama-3.1-405b-instruct.yaml
│      meta-mlama-3.1-70b-instruct.yaml
│      meta-mlama-3.1-8b-instruct.yaml
│      mistral-7b-instruct-v0.2.yaml
│      mistral-8x7b-instruct-v0.1.yaml
│      qwen2-1.5b-instruct.yaml
│      qwen2-57b-a14b-instruct.yaml
│      qwen2-72b-instruct.yaml
│      qwen2-7b-instruct.yaml
│      qwen2-vl-72b-instruct.yaml
│      qwen2-vl-7b-Instruct.yaml
│      qwen2.5-14b-instruct.yaml
│      qwen2.5-32b-instruct.yaml
│      qwen2.5-72b-instruct.yaml
│      qwen2.5-7b-instruct.yaml
│      qwen2.5-coder-32b-instruct.yaml
│      qwen2.5-coder-7b-instruct.yaml
│      qwen2.5-math-72b-instruct.yaml
│      yi-1.5-34b-chat.yaml
│      yi-1.5-6b-chat.yaml
│      yi-1.5-9b-chat.yaml
│
├─rerank
│      bce-reranker-base_v1.yaml
│      bge-reranker-v2-m3.yaml
│
├─speech2text
│      funaudio-sense-voice-small.yaml
│      sense-voice-small.yaml
│
├─text_embedding
│      bce-embedding-base-v1.yaml
│      bge-large-en-v1.5.yaml
│      bge-large-zh-v1.5.yaml
│      bge-m3.yaml
│
└─ttsfish-speech-1.4.yaml

2.通过API秘钥配置

安装 SiliconFlow插件后,通过输入 API密钥进行配置。如下所示:

二.创建模型供应商

通过Dify插件脚手架工具,创建项目就不再介绍了,主要是选择模型插件模版和配置插件权限等操作[1][2]。

1.创建模型供应商配置文件

ManifestYAML格式文件,声明了模型供应商基础信息、所支持的模型类型、配置方式、凭据规则。插件项目模板将在 /providers 路径下自动生成配置文件。

background: "#ffecff"
configurate_methods:- predefined-model- customizable-model
extra:python:model_sources:- models/llm/llm.py- models/rerank/rerank.py- models/text_embedding/text_embedding.py- models/tts/tts.py- models/speech2text/speech2text.pyprovider_source: provider/siliconflow.py
help:title:en_US: Get your API Key from SiliconFlowzh_Hans: 从 SiliconFlow 获取 API Keyurl:en_US: https://cloud.siliconflow.cn/account/ak
icon_large:en_US: siliconflow.svg
icon_small:en_US: siliconflow_square.svg
label:en_US: SiliconFlowzh_Hans: 硅基流动
model_credential_schema:credential_form_schemas:- label:en_US: API Keyplaceholder:en_US: Enter your API Keyzh_Hans: 在此输入您的 API Keyrequired: truetype: secret-inputvariable: api_key- default: "4096"label:en_US: Model context sizezh_Hans: 模型上下文长度placeholder:en_US: Enter your Model context sizezh_Hans: 在此输入您的模型上下文长度required: truetype: text-inputvariable: context_size- default: "4096"label:en_US: Upper bound for max tokenszh_Hans: 最大 token 上限show_on:- value: llmvariable: __model_typetype: text-inputvariable: max_tokens- default: no_calllabel:en_US: Function callingoptions:- label:en_US: Not Supportzh_Hans: 不支持value: no_call- label:en_US: Supportzh_Hans: 支持value: function_callrequired: falseshow_on:- value: llmvariable: __model_typetype: selectvariable: function_calling_typemodel:label:en_US: Model Namezh_Hans: 模型名称placeholder:en_US: Enter your model namezh_Hans: 输入模型名称
models:llm:position: models/llm/_position.yamlpredefined:- models/llm/*.yamlrerank:predefined:- models/rerank/*.yamlspeech2text:predefined:- models/speech2text/*.yamltext_embedding:predefined:- models/text_embedding/*.yamltts:predefined:- models/tts/*.yaml
provider: siliconflow
provider_credential_schema:credential_form_schemas:- label:en_US: API Keyplaceholder:en_US: Enter your API Keyzh_Hans: 在此输入您的 API Keyrequired: truetype: secret-inputvariable: api_key
supported_model_types:- llm- text-embedding- rerank- speech2text- tts

如果接入的供应商提供自定义模型,比如siliconflow提供微调模型,需要添加model_credential_schema 字段。

2.编写模型供应商代码

供应商需要继承 __base.model_provider.ModelProvider 基类,实现 validate_provider_credentials 供应商统一凭据校验方法即可。如下所示:

import logging
from dify_plugin import ModelProvider
from dify_plugin.entities.model import ModelType
from dify_plugin.errors.model import CredentialsValidateFailedErrorlogger = logging.getLogger(__name__)class SiliconflowProvider(ModelProvider):def validate_provider_credentials(self, credentials: dict) -> None:"""Validate provider credentialsif validate failed, raise exception:param credentials: provider credentials, credentials form defined in `provider_credential_schema`."""try:model_instance = self.get_model_instance(ModelType.LLM)model_instance.validate_credentials(model="deepseek-ai/DeepSeek-V2.5", credentials=credentials)except CredentialsValidateFailedError as ex:raise exexcept Exception as ex:logger.exception(f"{self.get_provider_schema().provider} credentials validate failed")raise ex

三.接入预定义模型

1.按模型类型创建不同模块结构

模型供应商下可能提供了不同的模型类型,需在供应商模块下创建相应的子模块,确保每种模型类型有独立的逻辑分层,便于维护和扩展[3]。当前支持模型类型如下:

2.编写模型调用代码

(1)llm.py

(2)rerank.py

(3)speech2text.py

(4)text_embedding.py

(5)tts.py

3.添加预定义模型配置

如果供应商提供了预定义模型,为每个模型创建与模型名称同名的 YAML 文件(例如 deepseek-v2.5.yaml)。按照 AIModelEntity [6]的规范编写文件内容,描述模型的参数和功能。

model: deepseek-ai/DeepSeek-V2.5
label:en_US: deepseek-ai/DeepSeek-V2.5
model_type: llm
features:- agent-thought- tool-call- stream-tool-call
model_properties:mode: chatcontext_size: 32768
parameter_rules:- name: temperatureuse_template: temperature- name: max_tokensuse_template: max_tokenstype: intdefault: 512min: 1max: 4096help:zh_Hans: 指定生成结果长度的上限。如果生成结果截断,可以调大该参数。en_US: Specifies the upper limit on the length of generated results. If the generated results are truncated, you can increase this parameter.- name: top_puse_template: top_p- name: top_klabel:zh_Hans: 取样数量en_US: Top ktype: inthelp:zh_Hans: 仅从每个后续标记的前 K 个选项中采样。en_US: Only sample from the top K options for each subsequent token.required: false- name: frequency_penaltyuse_template: frequency_penalty- name: response_formatlabel:zh_Hans: 回复格式en_US: Response Formattype: stringhelp:zh_Hans: 指定模型必须输出的格式en_US: specifying the format that the model must outputrequired: falseoptions:- text- json_object
pricing:input: '1.33'output: '1.33'unit: '0.000001'currency: RMB

4.调试和发布插件

调试和发布插件不再介绍,具体操作参考文献[2]。

参考文献

[1] Model 插件:https://docs.dify.ai/zh-hans/plugins/quick-start/developing-plugins/model

[2] Dify中的GoogleSearch工具插件开发例子:https://z0yrmerhgi8.feishu.cn/wiki/Ib15wh1rSi8mWckvWROckoT2n6g

[3] https://github.com/langgenius/dify-official-plugins/tree/main/models/siliconflow

[4] 模型设计规则:https://docs.dify.ai/zh-hans/plugins/api-documentation/model/model-designing-specification

[5] 模型接口:https://docs.dify.ai/zh-hans/plugins/api-documentation/model/mo-xing-jie-kou

[6] AIModelEntity:https://docs.dify.ai/zh-hans/plugins/api-documentation/model/model-designing-specification#aimodelentity

[7] Dify中的预定义模型插件开发例子:以siliconflow为例:https://z0yrmerhgi8.feishu.cn/wiki/BwPYw0VajidKURkxxBIc0UH7n5f

相关文章:

  • PT_THREAD 的嵌套协程示例
  • Tomcat 使用与配置全解
  • EasyDarwin的配置与使用
  • 无限debugger
  • SDL2常用函数:SDL_RendererSDL_CreateRendererSDL_RenderCopySDL_RenderPresent
  • LVGL(lv_keyboard键盘)
  • Essential Studio for JavaScript:首款专用于LOB应用开发的JavaScript框架
  • node.js配置变量
  • x86 与 ARM 汇编深度对比:聚焦 x86 汇编的独特魅力
  • n8n 读写本地文件
  • 计算机网络学习20250526
  • 构建版本没mac上传APP方法
  • 电子电路:什么是电动势?
  • 数据库的索引概述与常见索引结构
  • 机器人工具中心点标定
  • UR10e 机器人如何通过扭矩控制接口实现高效装配
  • 【R语言科研绘图-最小二乘法】
  • 【后端高阶面经:MongoDB篇】40、怎么优化MongoDB的查询性能?
  • 万勋“机器人服务”全新品牌—汎汐®全球首发!独家服务高空建筑清洗、风机运维等场景
  • AI时代新词-AI驱动的自动化(AI - Driven Automation)
  • 汕头网站制作方法/中国疫情最新数据
  • 温州网站建设企业/旅行网站排名
  • 天翼云电脑免费领取/免费的关键词优化工具
  • 晋中住房保障和城乡建设局网站/百度拉新推广平台
  • 快速网站轻松排名/b站推出的短视频app哪个好
  • 辽宁网站设计/哪个搜索引擎最好