自定义模型#

Xinference 提供了一种灵活而全面的方式来集成、管理和应用自定义模型。

无需注册而直接启动自定义模型#

v0.14.0 版本开始,如果你需要注册的模型的家族是 Xinference 内置支持的模型,你可以直接通过 launch 接口中的 model_path 参数来启动它,从而免去注册步骤的麻烦。现在非常推荐使用这种方式。

例如:

xinference launch --model-path <model_file_path> --model-engine <engine> -n qwen1.5-chat

上面的例子展示了当我已有 qwen1.5-chat 模型文件时,如何直接 launch 它。

对于分布式场景,将你的模型文件置于某个 worker ,然后通过 launch 接口的 worker_ipmodel_path 参数来达到直接 launch 的效果。

备注

对于命令行界面(CLI)的使用,请优先使用 --model-path``(分号分隔的大小写混合形式)。--model_path`` 兼容旧版规范,但不建议使用。

定义一个自定义模型#

Web UI:自动解析大型语言模型配置#

Added in version v2.0.0.

通过Web UI注册自定义LLM时,Xinference可自动解析模型配置并为您预填关键字段。

您仅需要提供:

  • Model path / Model ID (where the model lives, local path or hub ID)

  • Model Family

解析后,用户界面可自动填充以下字段:

  • Context Length

  • Model_Languages

  • Model_Abilities

  • Model_Specs

在保存自定义模型之前,您可以查看并编辑这些字段。

基于以下模板定义一个自定义模型:

{
    "version": 2,
    "context_length": 32768,
    "model_name": "custom-qwen-2.5",
    "model_lang": [
        "en",
        "zh"
    ],
    "model_ability": [
        "generate"
    ],
    "model_description": "This is a custom model description.",
    "model_family": "my-custom-qwen-2.5",
    "model_specs": [
        {
            "model_format": "pytorch",
            "model_size_in_billions": "0_5",
            "quantization": "none",
            "model_id": null,
            "model_hub": "huggingface",
            "model_uri": "file:///path/to/models--Qwen--Qwen2.5-0.5B",
            "model_revision": null,
            "activated_size_in_billions": null
        }
    ],
    "chat_template": null,
    "stop_token_ids": null,
    "stop": null,
    "reasoning_start_tag": null,
    "reasoning_end_tag": null,
    "cache_config": null,
    "virtualenv": {
        "packages": [],
        "inherit_pip_config": true,
        "index_url": null,
        "extra_index_url": null,
        "find_links": null,
        "trusted_host": null,
        "no_build_isolation": null
    },
    "is_builtin": false
}
  • model_name: 模型名称。名称必须以字母或数字开头,且只能包含字母、数字、下划线或短划线。

  • context_length: 一个可选的整数,模型支持的最大上下文长度,包括输入和输出长度。如果未定义,默认值为2048个token(约1,500个词)。

  • dimensions: 一个整数,用于定义嵌入模型输出的向量大小。

  • max_tokens: 一个整数,定义嵌入模型在单次请求中可处理的最大输入token数量。

  • model_lang: 一个字符串列表,表示模型支持的语言。例如:['en'],表示该模型支持英语。

  • model_ability: 一个字符串列表,定义模型的能力。它可以包括像 'embed'、'generate' 和 'chat' 这样的选项。示例表示模型具有 'generate' 的能力。

  • model_family: 一个必要的字符串,表示要注册的模型族。该参数名称不得与任何内置模型名称冲突。

  • model_specs: 一个包含定义模型规格的对象数组。这些规格包括:
    • model_format: 一个定义模型格式的字符串,可以是 'pytorch' 或 'ggufv2'。

  • model_size_in_billions: 一个整数,定义模型的参数量,以十亿为单位。

  • quantizations: 一个字符串列表,定义模型的量化方式。对于 PyTorch 模型,它可以是 "4-bit"、"8-bit" 或 "none"。对于 ggufv2 模型,量化方式应与 model_file_name_template 中的值对应。某些引擎还支持 fp4 / fp8 / bnb 格式(后端支持详情请参见 安装 )。

    • model_id:代表模型 id 的字符串,可以是该模型对应的 HuggingFace 仓库 id。如果 model_uri 字段缺失,Xinference 将尝试从此id指示的HuggingFace仓库下载该模型。

    • model_hub: 一个可选字符串,表示从何处下载模型,例如 HuggingFace 或 modelscope。

    • model_uri:表示模型文件位置的字符串,例如本地目录:"file:///path/to/llama-2-7b"。当 model_format 是 ggufv2 ,此字段必须是具体的模型文件路径。而当 model_format 是 pytorch 时,此字段必须是一个包含所有模型文件的目录。

    • model_revision: 一个字符串,表示从存储库中使用的模型文件的具体版本或提交哈希值。

  • chat_template:如果 model_ability 中包含 chat ,那么此选项必须配置以生成合适的完整提示词。这是一个 Jinja 模版字符串。通常,你可以在模型目录的 tokenizer_config.json 文件中找到。

  • stop_token_ids:如果 model_ability 中包含 chat ,那么推荐配置此选项以合理控制对话的停止。这是一个包含整数的列表,你可以在模型目录的 generation_config.jsontokenizer_config.json 文件中提取相应的值。

  • stop:如果 model_ability 中包含 chat ,那么推荐配置此选项以合理控制对话的停止。这是一个包含字符串的列表,你可以在模型目录的 tokenizer_config.json 文件中找到 token 值对应的字符串。

  • reasoning_start_tag: 一个特殊的 token 或 prompt,用于明确指示大语言模型在其输出中思维链或推理过程的起点。

  • reasoning_end_tag: 一个特殊的 token 或 prompt,用于明确指示大语言模型在其输出中思维链或推理过程的终点。

  • cache_config: 一个字符串,表示系统存储和管理临时数据(缓存)的参数。

  • virtualenv: A settings object for model dependency isolation. Please refer to this document for details.

注册一个自定义模型#

以代码的方式注册自定义模型

import json
from xinference.client import Client

with open('model.json') as fd:
    model = fd.read()

# replace with real xinference endpoint
endpoint = 'http://localhost:9997'
client = Client(endpoint)
client.register_model(model_type="<model_type>", model=model, persist=False)

以命令行的方式

xinference register --model-type <model_type> --file model.json --persist

注意将以下部分的 <model_type> 替换为 LLMembeddingrerank

列举内置和自定义模型#

以代码的方式列举内置和自定义模型

registrations = client.list_model_registrations(model_type="<model_type>")

以命令行的方式

xinference registrations --model-type <model_type>

启动自定义模型#

以代码的方式启动自定义模型

uid = client.launch_model(model_name='custom-llama-2', model_format='pytorch')

以命令行的方式

xinference launch --model-name custom-llama-2 --model-format pytorch

使用自定义模型#

以代码的方式调用模型

model = client.get_model(model_uid=uid)
model.generate('What is the largest animal in the world?')

结果为:

{
   "id":"cmpl-a4a9d9fc-7703-4a44-82af-fce9e3c0e52a",
   "object":"text_completion",
   "created":1692024624,
   "model":"43e1f69a-3ab0-11ee-8f69-fa163e74fa2d",
   "choices":[
      {
         "text":"\nWhat does an octopus look like?\nHow many human hours has an octopus been watching you for?",
         "index":0,
         "logprobs":"None",
         "finish_reason":"stop"
      }
   ],
   "usage":{
      "prompt_tokens":10,
      "completion_tokens":23,
      "total_tokens":33
   }
}

或者以命令行的方式,用实际的模型 UID 替换 ${UID}

xinference generate --model-uid ${UID}

注销自定义模型#

以代码的方式注销自定义模型

model = client.unregister_model(model_type="<model_type>", model_name='custom-llama-2')

以命令行的方式

xinference unregister --model-type <model_type> --model-name custom-llama-2