Skip to content

Latest commit

 

History

History
210 lines (136 loc) · 8.19 KB

quickstart.rst

File metadata and controls

210 lines (136 loc) · 8.19 KB

Quickstart

This guide shows how to use vLLM to:

  • run offline batched inference on a dataset;
  • build an API server for a large language model;
  • start an OpenAI-compatible API server.

Be sure to complete the :ref:`installation instructions <installation>` before continuing with this guide.

Offline Batched Inference

We first show an example of using vLLM for offline batched inference on a dataset. In other words, we use vLLM to generate texts for a list of input prompts.

Import LLM and SamplingParams from vLLM. The LLM class is the main class for running offline inference with vLLM engine. The SamplingParams class specifies the parameters for the sampling process.

from vllm import LLM, SamplingParams

Define the list of input prompts and the sampling parameters for generation. The sampling temperature is set to 0.8 and the nucleus sampling probability is set to 0.95. For more information about the sampling parameters, refer to the class definition.

prompts = [
    "Hello, my name is",
    "The president of the United States is",
    "The capital of France is",
    "The future of AI is",
]
sampling_params = SamplingParams(temperature=0.8, top_p=0.95)

Initialize vLLM's engine for offline inference with the LLM class and the OPT-125M model. The list of supported models can be found at :ref:`supported models <supported_models>`.

llm = LLM(model="facebook/opt-125m")

Use model from www.modelscope.cn

export VLLM_USE_MODELSCOPE=True
llm = LLM(model="qwen/Qwen-7B-Chat", revision="v1.1.8", trust_remote_code=True)

Call llm.generate to generate the outputs. It adds the input prompts to vLLM engine's waiting queue and executes the vLLM engine to generate the outputs with high throughput. The outputs are returned as a list of RequestOutput objects, which include all the output tokens.

outputs = llm.generate(prompts, sampling_params)

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

The code example can also be found in examples/offline_inference.py.

API Server

vLLM can be deployed as an LLM service. We provide an example FastAPI server. Check vllm/entrypoints/api_server.py for the server implementation. The server uses AsyncLLMEngine class to support asynchronous processing of incoming requests.

Start the server:

$ python -m vllm.entrypoints.api_server

Use model from www.modelscope.cn

$ VLLM_USE_MODELSCOPE=True python -m vllm.entrypoints.api_server \
$    --model="qwen/Qwen-7B-Chat" \
$    --revision="v1.1.8" \
$    --trust-remote-code

By default, this command starts the server at http://localhost:8000 with the OPT-125M model.

Query the model in shell:

$ curl http://localhost:8000/generate \
$     -d '{
$         "prompt": "San Francisco is a",
$         "use_beam_search": true,
$         "n": 4,
$         "temperature": 0
$     }'

See examples/api_client.py for a more detailed client example.

OpenAI-Compatible Server

vLLM can be deployed as a server that mimics the OpenAI API protocol. This allows vLLM to be used as a drop-in replacement for applications using OpenAI API. By default, it starts the server at http://localhost:8000. You can specify the address with --host and --port arguments. The server currently hosts one model at a time (OPT-125M in the above command) and implements list models, create chat completion, and create completion endpoints. We are actively adding support for more endpoints.

Start the server:

$ python -m vllm.entrypoints.openai.api_server \
$     --model facebook/opt-125m

Use model from www.modelscope.cn

$ VLLM_USE_MODELSCOPE=True python -m vllm.entrypoints.openai.api_server \
$     --model="qwen/Qwen-7B-Chat" --revision="v1.1.8" --trust-remote-code

By default, the server uses a predefined chat template stored in the tokenizer. You can override this template by using the --chat-template argument:

$ python -m vllm.entrypoints.openai.api_server \
$     --model facebook/opt-125m \
$     --chat-template ./examples/template_chatml.json

This server can be queried in the same format as OpenAI API. For example, list the models:

$ curl http://localhost:8000/v1/models

Using OpenAI Completions API with vLLM

Query the model with input prompts:

$ curl http://localhost:8000/v1/completions \
$     -H "Content-Type: application/json" \
$     -d '{
$         "model": "facebook/opt-125m",
$         "prompt": "San Francisco is a",
$         "max_tokens": 7,
$         "temperature": 0
$     }'

Since this server is compatible with OpenAI API, you can use it as a drop-in replacement for any applications using OpenAI API. For example, another way to query the server is via the openai python package:

import openai
# Modify OpenAI's API key and API base to use vLLM's API server.
openai.api_key = "EMPTY"
openai.api_base = "http://localhost:8000/v1"
completion = openai.Completion.create(model="facebook/opt-125m",
                                      prompt="San Francisco is a")
print("Completion result:", completion)

For a more detailed client example, refer to examples/openai_completion_client.py.

Using OpenAI Chat API with vLLM

The vLLM server is designed to support the OpenAI Chat API, allowing you to engage in dynamic conversations with the model. The chat interface is a more interactive way to communicate with the model, allowing back-and-forth exchanges that can be stored in the chat history. This is useful for tasks that require context or more detailed explanations.

Querying the model using OpenAI Chat API:

You can use the create chat completion endpoint to communicate with the model in a chat-like interface:

$ curl http://localhost:8000/v1/chat/completions \
$     -H "Content-Type: application/json" \
$     -d '{
$         "model": "facebook/opt-125m",
$         "messages": [
$             {"role": "system", "content": "You are a helpful assistant."},
$             {"role": "user", "content": "Who won the world series in 2020?"}
$         ]
$     }'

Python Client Example:

Using the openai python package, you can also communicate with the model in a chat-like manner:

import openai
# Set OpenAI's API key and API base to use vLLM's API server.
openai.api_key = "EMPTY"
openai.api_base = "http://localhost:8000/v1"
chat_response = openai.ChatCompletion.create(
    model="facebook/opt-125m",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Tell me a joke."},
    ]
)
print("Chat response:", chat_response)

For more in-depth examples and advanced features of the chat API, you can refer to the official OpenAI documentation.