跳到内容

生成式模型

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

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

配置

模型运行器 (--runner)

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

提示

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

离线推理

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

LLM.generate

generate 方法适用于 vLLM 中的所有生成模型。它类似于 HF 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}")

重要

默认情况下,如果 HuggingFace 模型仓库中存在 generation_config.json,vLLM 将通过应用该配置来使用模型创建者推荐的采样参数。在大多数情况下,如果未指定 SamplingParams,这将为您提供最佳的默认结果。

但是,如果您偏好使用 vLLM 的默认采样参数,请在创建 LLM 实例时传递 generation_config="vllm"

代码示例请见此处: examples/basic/offline_inference/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 Chat Completions API 的输入,并自动应用模型的聊天模板 (chat template) 来格式化提示词。

重要

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

代码
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/basic/offline_inference/chat.py

如果模型没有聊天模板或者您想指定另一个模板,您可以显式传递聊天模板。

from vllm.entrypoints.chat_utils import load_chat_template

# You can find a list of existing chat templates under `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 相对应的端点。