自定义模型#

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

定义自定义大语言模型#

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

{
  "version": 1,
  "context_length": 2048,
  "model_name": "custom-llama-2",
  "model_lang": [
    "en"
  ],
  "model_ability": [
    "generate"
  ],
  "model_family": "llama-2",
  "model_specs": [
    {
      "model_format": "pytorch",
      "model_size_in_billions": 7,
      "quantizations": [
        "4-bit",
        "8-bit",
        "none"
      ],
      "model_id": "meta-llama/Llama-2-7b-hf",
      "model_uri": "file:///path/to/llama-2-7b-hf"
    },
    {
      "model_format": "ggmlv3",
      "model_size_in_billions": 7,
      "quantizations": [
        "q4_0",
        "q8_0"
      ],
      "model_id": "TheBloke/Llama-2-7B-GGML",
      "model_file_name_template": "llama-2-7b.ggmlv3.{quantization}.bin"
      "model_uri": "file:///path/to/ggml-file"
    }
  ]
}
  • model_name: 模型名称。名称必须以字母或数字开头,且只能包含字母、数字、下划线或短划线。

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

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

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

  • model_family: 必需字段,表示你要注册的模型的家族(类别)。可选值来自于 Xinference 所有内置模型的模型名。如果你要注册的模型不在其中,填入 other 。注意,此字段的值必须根据模型能力填入。例如,如果你注册的是自定义 llama-2 模型,千万不要填入 llama-2-chat

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

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

    • quantizations: 一个字符串列表,定义模型的量化方式。对于 PyTorch 模型,它可以是 “4-bit”、”8-bit” 或 “none”。对于 ggmlv3 模型,量化方式应与 model_file_name_template 中的值对应。

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

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

    • model_file_name_template: ggml 模型所需。一个 f-string 模板,用于根据量化定义模型文件名。注意,这里不要填入文件的路径。

  • prompt_style: 如果上述 model_family 字段不是 other ,则无需设置此字段。 prompt_style 是一个可选字段,表示 chat 模型需要的提示词样式。给定的示例将其设置为 None,但可以在引用的文件 xinference/model/llm/tests/test_utils.py 中找到更多详细信息。你也可以指定一个字符串,以使用内置模型的提示词样式。

{
    "model_specs": [...],
    "prompt_style": "chatglm3"
}

Xinference 支持这些内置、常用的提示词样式:

{
  "style_name": "NO_COLON_TWO",
  "system_prompt": "",
  "roles": [
    " <reserved_102> ",
    " <reserved_103> "
  ],
  "intra_message_sep": "",
  "inter_message_sep": "</s>",
  "stop_token_ids": [
    2,
    195
  ]
}

以上列举出了最常使用的提示词样式。完整的支持列表可以通过 Xinference 页面的 register model 面板查看。

定义自定义 embedding 模型#

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

{
    "model_name": "custom-bge-base-en",
    "dimensions": 768,
    "max_tokens": 512,
    "language": ["en"],
    "model_id": "BAAI/bge-base-en",
    "model_uri": "file:///path/to/bge-base-en"
}
  • model_name: 模型名称。名称必须以字母或数字开头,且只能包含字母、数字、下划线或短划线。

  • dimensions: 表示 embedding 维度的整型值。

  • max_tokens: 表示 embedding 模型支持的最大输入序列长度的整型值。

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

  • model_id: 一个表示模型标识的字符串,类似 HuggingFace 或 ModelScope 使用的标识符。

  • model_uri: 表示模型的 URI 的字符串,例如 “file:///path/to/llama-2-7b”。如果模型 URI 不存在,Xinference 将尝试使用 model_id 从 HuggingFace 或 ModelScope 下载模型。

注册一个自定义模型#

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

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> 替换为 LLM 或者 embedding

列举内置和自定义模型#

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

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