Skip to content

生成模型

vLLM 对生成模型提供了一流的支持,涵盖了大多数大型语言模型(LLM)。

在 vLLM 中,生成模型实现了 VllmModelForTextGeneration 接口。
这些模型基于输入的最终隐藏状态,输出生成 token 的对数概率,然后通过 Sampler 获得最终的文本。

配置

模型运行器 (--runner)

通过 --runner generate 选项以生成模式运行模型。

Tip

在绝大多数情况下无需设置此选项,因为 vLLM 可通过 --runner auto 自动检测要使用的模型运行器。

离线推理

LLM 类提供了多种用于离线推理的方法。
初始化模型时的选项列表,请参见 配置

LLM.generate

generate 方法可用于 vLLM 中的所有生成模型。
它与 Hugging Face Transformers 中的对应方法 类似,
但额外自动执行了分词(tokenization)和去分词(detokenization)。

from vllm import LLM

llm = LLM(model="facebook/opt-125m")
outputs = llm.generate("Hello, my name is")

for output in outputs:
    prompt = output.prompt
    generated_text = output.outputs[0].text
    print(f"Prompt: {prompt!r}, Generated text: {generated_text!r}")

你可以通过传入 SamplingParams 可选地控制语言生成过程。
例如,通过设置 temperature=0 可使用贪心采样:

from vllm import LLM, SamplingParams

llm = LLM(model="facebook/opt-125m")
params = SamplingParams(temperature=0)
outputs = llm.generate("Hello, my name is", params)

for output in outputs:
    prompt = output.prompt
    generated_text = output.outputs[0].text
    print(f"Prompt: {prompt!r}, Generated text: {generated_text!r}")

Important

默认情况下,如果 Hugging Face 模型仓库中存在 generation_config.json,vLLM 将使用模型创建者推荐的采样参数。在大多数情况下,如果未指定 SamplingParams,这将为你提供最佳结果。

然而,如果更倾向于使用 vLLM 的默认采样参数,请在创建 LLM 实例时传入 generation_config="vllm"
代码示例可参见: examples/offline_inference/basic/basic.py

beam_search 方法在 generate 的基础上实现了束搜索(beam search)
例如,使用 5 个束并最多输出 50 个 token:

from vllm import LLM
from vllm.sampling_params import BeamSearchParams

llm = LLM(model="facebook/opt-125m")
params = BeamSearchParams(beam_width=5, max_tokens=50)
outputs = llm.beam_search([{"prompt": "Hello, my name is "}], params)

for output in outputs:
    generated_text = output.sequences[0].text
    print(f"Generated text: {generated_text!r}")

LLM.chat

chat 方法在 generate 的基础上实现了聊天功能。
特别地,它接受类似于 OpenAI 聊天补全 API 的输入,
并自动应用模型的聊天模板(chat template)来格式化提示。

Important

通常只有经过指令微调的模型才具有聊天模板。
基础模型可能表现不佳,因为它们并未针对聊天对话进行训练。

Code
from vllm import LLM

llm = LLM(model="meta-llama/Meta-Llama-3-8B-Instruct")
conversation = [
    {
        "role": "system",
        "content": "You are a helpful assistant",
    },
    {
        "role": "user",
        "content": "Hello",
    },
    {
        "role": "assistant",
        "content": "Hello! How can I assist you today?",
    },
    {
        "role": "user",
        "content": "Write an essay about the importance of higher education.",
    },
]
outputs = llm.chat(conversation)

for output in outputs:
    prompt = output.prompt
    generated_text = output.outputs[0].text
    print(f"Prompt: {prompt!r}, Generated text: {generated_text!r}")

代码示例可参见: examples/offline_inference/basic/chat.py

如果模型没有聊天模板,或你想指定另一个模板,
可以显式传入一个聊天模板:

from vllm.entrypoints.chat_utils import load_chat_template

# 你可以在 `examples/` 下找到现有聊天模板的列表
custom_template = load_chat_template(chat_template="<path_to_template>")
print("Loaded chat template:", custom_template)

outputs = llm.chat(conversation, chat_template=custom_template)

在线服务

我们的 OpenAI 兼容服务器 提供了与离线 API 对应的端点: