跳到內容

生成模型

vLLM 為生成模型提供一流的支援,這涵蓋了大多數 LLM。

在 vLLM 中,生成模型實現了 VllmModelForTextGeneration 介面。基於輸入的最終隱藏狀態,這些模型輸出要生成的 token 的對數機率,然後透過 Sampler 進行處理以獲得最終文字。

配置

模型執行器 (--runner)

透過選項 --runner generate 以生成模式執行模型。

提示

在絕大多數情況下,無需設定此選項,因為 vLLM 可以透過 --runner auto 自動檢測要使用的模型執行器。

離線推理

LLM 類提供了各種離線推理方法。有關初始化模型時的選項列表,請參閱 配置

LLM.generate

vLLM 中所有生成模型都可以使用 generate 方法。它與 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}")

重要

預設情況下,vLLM 將使用模型建立者推薦的取樣引數,方法是應用 huggingface 模型儲存庫中的 generation_config.json(如果存在)。在大多數情況下,如果未指定 SamplingParams,這將在預設情況下為您提供最佳結果。

但是,如果您偏好 vLLM 的預設取樣引數,請在建立 LLM 例項時傳遞 generation_config="vllm"

程式碼示例可以在這裡找到: examples/offline_inference/basic/basic.py

beam_search 方法在 generate 的基礎上實現了 beam search。例如,使用 5 個 beam 進行搜尋並輸出最多 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 來格式化 prompt。

重要

通常,只有經過指令微調的模型才有 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/offline_inference/basic/chat.py

如果模型沒有 chat template 或者您想指定一個不同的,您可以顯式地傳遞一個 chat template。

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 相對應的端點。