跳到內容

生成式模型

vLLM 為生成式模型提供一流支援,涵蓋了大多數大型語言模型 (LLMs)。

在 vLLM 中,生成式模型實現了 VllmModelForTextGeneration 介面。基於輸入的最終隱藏狀態,這些模型輸出要生成的 token 的對數機率,然後這些機率透過 [Sampler][vllm.model_executor.layers.Sampler] 來獲得最終文字。

對於生成式模型,唯一支援的 --task 選項是 "generate"。通常,這會自動推斷,因此您無需指定。

離線推理

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

beam_search 方法在 generate 的基礎上實現了束搜尋。例如,使用 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 的輸入,並自動應用模型的聊天模板來格式化提示。

重要

一般來說,只有經過指令微調的模型才擁有聊天模板。基礎模型可能表現不佳,因為它們沒有經過訓練來響應聊天對話。

程式碼
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

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