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

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

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

自定义模型指的是需要自行部署或配置的 LLM,默认包含模型类型和模型名称两个参数,无需在供应商 yaml 文件定义。供应商配置文件无需实现 validate_provider_credentialRuntime会根据用户选择的模型类型或模型名称,自动调用对应模型层的 validate_credentials方法进行验证。

一.xinference模型插件

Xorbits Inference (Xinference) 是一个开源平台,用于简化各种 AI 模型的运行和集成。借助 Xinference,可使用任何开源 LLM、嵌入模型和多模态模型在云端或本地环境中运行推理,并创建强大的 AI 应用。

1.多层模型分类

│  xinference_helper.py
│  
├─llm
│      llm.py
│      __init__.py
│      
├─rerank
│      rerank.py
│      __init__.py
│      
├─speech2text
│      speech2text.py
│      __init__.py
│
├─text_embedding
│      text_embedding.py
│      __init__.py
│
└─ttstts.py__init__.py

2.通过API秘钥配置

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

除社区支持开源版外,也提供企业版本,Xinference企业版和开源版本的对比[7],如下所示:

二.创建模型供应商

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

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

明确自定义模型中所包含的模型类型。在插件项目的 /provider 路径下,新建 xinference.yaml 文件。Xinference 家族模型支持 LLMText EmbeddingRerank 等模型类型,因此需要在xinference.yaml 文件中包含上述模型类型。

background: "#FAF5FF"
configurate_methods:- 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/xinference.py
help:title:en_US: How to deploy Xinferencezh_Hans: 如何部署 Xinferenceurl:en_US: https://github.com/xorbitsai/inference
icon_large:en_US: icon_l_en.svg
icon_small:en_US: icon_s_en.svg
label:en_US: Xorbits Inference
model_credential_schema:credential_form_schemas:- label:en_US: Server urlzh_Hans: 服务器URLplaceholder:en_US: Enter the url of your Xinference, e.g. http://192.168.1.100:9997zh_Hans: 在此输入Xinference的服务器地址,如 http://192.168.1.100:9997required: truetype: secret-inputvariable: server_url- label:en_US: Model uidzh_Hans: 模型UIDplaceholder:en_US: Enter the model uidzh_Hans: 在此输入您的Model UIDrequired: truetype: text-inputvariable: model_uid- label:en_US: API keyzh_Hans: API密钥placeholder:en_US: Enter the api keyzh_Hans: 在此输入您的API密钥required: falsetype: secret-inputvariable: api_key- default: "60"label:en_US: invoke timeout (unit:second)zh_Hans: 调用超时时间 (单位:秒)placeholder:en_US: Enter invoke timeout valuezh_Hans: 在此输入调用超时时间required: truetype: text-inputvariable: invoke_timeout- default: "3"label:en_US: max retrieszh_Hans: 调用重试次数placeholder:en_US: Enter max retrieszh_Hans: 在此输入调用重试次数required: truetype: text-inputvariable: max_retriesmodel:label:en_US: Model Namezh_Hans: 模型名称placeholder:en_US: Enter your model namezh_Hans: 输入模型名称
provider: xinference
supported_model_types:- llm- text-embedding- rerank- speech2text- tts

configurate_methodscustomizable-model,即Xinference为本地部署的供应商,并且没有预定义模型,需要用什么模型需要根据 Xinference 的文档进行部署,因此此处的方法为自定义模型。如果接入的供应商提供自定义模型,需要添加model_credential_schema 字段。

2.编写模型供应商代码

对于像 Xinference 这样的自定义模型供应商,可跳过完整实现的步骤。只需创建一个名为 XinferenceProvider 的空类,并在其中实现一个空的 validate_provider_credentials 方法。

import logging
from dify_plugin import ModelProviderlogger = logging.getLogger(__name__)class XinferenceAIProvider(ModelProvider):def validate_provider_credentials(self, credentials: dict) -> None:pass

XinferenceProvider 是一个占位类,用于标识自定义模型供应商。

validate_provider_credentials 方法虽然不会被实际调用,但必须存在,这是因为其父类是抽象类,要求所有子类都实现这个方法。通过提供一个空实现,可以避免因未实现抽象方法而导致的实例化错误。

三.接入预定义模型

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

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

2.编写模型调用代码

(1)llm.py

与预定义类型模型不同,由于没有在 yaml 文件中定义一个模型支持哪些参数,因此,需要动态时间模型参数的Schema。比如 Xinference支持max_tokenstemperaturetop_p 这三个模型参数。但是有的供应商根据不同的模型支持不同的参数,比如供应商 OpenLLM 支持top_k,但是并不是这个供应商提供的所有模型都支持 top_k,这里举例A模型支持 top_k,B 模型不支持top_k,那么需要在这里动态生成模型参数的Schema,如下所示:

def get_customizable_model_schema(self, model: str, credentials: dict) -> AIModelEntity | None:"""used to define customizable model schema"""rules = [ParameterRule(name='temperature', type=ParameterType.FLOAT,use_template='temperature',label=I18nObject(zh_Hans='温度', en_US='Temperature')),ParameterRule(name='top_p', type=ParameterType.FLOAT,use_template='top_p',label=I18nObject(zh_Hans='Top P', en_US='Top P')),ParameterRule(name='max_tokens', type=ParameterType.INT,use_template='max_tokens',min=1,default=512,label=I18nObject(zh_Hans='最大生成长度', en_US='Max Tokens'))]# if model is A, add top_k to rulesif model == 'A':rules.append(ParameterRule(name='top_k', type=ParameterType.INT,use_template='top_k',min=1,default=50,label=I18nObject(zh_Hans='Top K', en_US='Top K')))"""some NOT IMPORTANT code here"""entity = AIModelEntity(model=model,label=I18nObject(en_US=model),fetch_from=FetchFrom.CUSTOMIZABLE_MODEL,model_type=model_type,model_properties={ ModelPropertyKey.MODE:  ModelType.LLM,},parameter_rules=rules)return entity
(2)rerank.py

(3)speech2text.py

(4)text_embedding.py

(5)tts.py

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/xinference

[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] Xinference企业版和开源版本的对比:https://xorbits.cn/features

[8] Dify中的自定义模型插件开发例子:以xinference为例:https://z0yrmerhgi8.feishu.cn/wiki/Wi9Rw5lCPiNgUTkpkO5c3M5vn6d

http://www.lryc.cn/news/2386598.html

相关文章:

  • crud方法命名示例
  • 尚硅谷redis7 33-36 redis持久化之RDB优缺点及数据丢失案例
  • No such file or directory: ‘ffprobe‘
  • 计算机网络-WebSocket/DNS/Cookie/Session/Token/Jwt/Nginx
  • 功能“递归模式”在 C# 7.3 中不可用,请使用 8.0 或更高的语言版本的一种兼容处理方案
  • 第4章-操作系统知识
  • 将网页带格式转化为PDF
  • 【ArcGIS】ArcGIS AI 助手----复现
  • 使用 FFmpeg 将视频转换为高质量 GIF(保留原始尺寸和帧率)
  • 《Java vs Go vs C++ vs C:四门编程语言的深度对比》
  • 充电枪IEC62196/EN 62196测试内容
  • 有效的字母异位符--LeetCode
  • SAP ERP 系统拆分的七大挑战
  • AcrelEMS 3.0智慧能源管理平台:构建企业微电网数智化中枢
  • 【HTML-12】HTML表格常用属性详解:从基础到高级应用
  • Word转PDF--自动生成目录
  • MySQL组合索引优化策略
  • Spring MVC 的的核心原理与实践指南
  • 轻量级视觉语言模型 Dolphin:高效精准的文档结构化解析利器
  • 如何安全配置数据库(MySQL/PostgreSQL/MongoDB)
  • 将 Docker 镜像从服务器A迁移到服务器B的方法
  • git merge解冲突后,add、continue提交
  • Lines of Thought in Large Language Models
  • 八股战神-JVM知识速查
  • 机试 | STL | string | 文字处理软件
  • 运动规划实战案例 | 图解基于状态晶格(State Lattice)的路径规划(附ROS C++/Python仿真)
  • 深入浅出对抗学习:概念、攻击、防御与代码实践
  • Neo4j(二) - 使用Cypher操作Neo4j
  • 09、供应商管理数字化转型:从潜在评估到战略合作的系统化方法
  • AI时代新词-AI增强现实(AI - Enhanced Reality)