quickstart.rst 7.55 KB
Newer Older
Zhuohan Li's avatar
Zhuohan Li committed
1
2
.. _quickstart:

3
==========
Woosuk Kwon's avatar
Woosuk Kwon committed
4
5
6
Quickstart
==========

7
This guide will help you quickly get started with vLLM to:
Zhuohan Li's avatar
Zhuohan Li committed
8

9
10
* :ref:`Run offline batched inference <offline_batched_inference>` 
* :ref:`Run OpenAI-compatible inference <openai_compatible_server>`
Zhuohan Li's avatar
Zhuohan Li committed
11

12
13
14
Prerequisites
--------------
- OS: Linux
15
- Python: 3.9 -- 3.12
16
- GPU: compute capability 7.0 or higher (e.g., V100, T4, RTX20xx, A100, L4, H100, etc.)
Woosuk Kwon's avatar
Woosuk Kwon committed
17

18
19
20
21
22
23
Installation
--------------

You can install vLLM using pip. It's recommended to use `conda <https://docs.conda.io/projects/conda/en/latest/user-guide/getting-started.html>`_ to create and manage Python environments.

.. code-block:: console
24

25
26
27
    $ conda create -n myenv python=3.10 -y
    $ conda activate myenv
    $ pip install vllm
28

29
Please refer to the :ref:`installation documentation <installation>` for more details on installing vLLM.
30

31
.. _offline_batched_inference:
32

Zhuohan Li's avatar
Zhuohan Li committed
33
34
35
Offline Batched Inference
-------------------------

36
37
38
With vLLM installed, you can start generating texts for list of input prompts (i.e. offline batch inferencing). The example script for this section can be found `here <https://github.com/vllm-project/vllm/blob/main/examples/offline_inference.py>`__.

The first line of this example imports the classes :class:`~vllm.LLM` and :class:`~vllm.SamplingParams`:
Zhuohan Li's avatar
Zhuohan Li committed
39

40
41
- :class:`~vllm.LLM` is the main class for running offline inference with vLLM engine.
- :class:`~vllm.SamplingParams` specifies the parameters for the sampling process.
Woosuk Kwon's avatar
Woosuk Kwon committed
42
43
44

.. code-block:: python

Woosuk Kwon's avatar
Woosuk Kwon committed
45
    from vllm import LLM, SamplingParams
Woosuk Kwon's avatar
Woosuk Kwon committed
46

47
The next section defines a list of input prompts and sampling parameters for text generation. The `sampling temperature <https://arxiv.org/html/2402.05201v1>`_ is set to ``0.8`` and the `nucleus sampling probability <https://en.wikipedia.org/wiki/Top-p_sampling>`_ is set to ``0.95``. You can find more information about the sampling parameters `here <https://docs.vllm.ai/en/stable/dev/sampling_params.html>`__.
Zhuohan Li's avatar
Zhuohan Li committed
48
49
50

.. code-block:: python

Woosuk Kwon's avatar
Woosuk Kwon committed
51
52
53
54
55
56
57
58
    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)

59
The :class:`~vllm.LLM` class initializes vLLM's engine and the `OPT-125M model <https://arxiv.org/abs/2205.01068>`_ for offline inference. The list of supported models can be found :ref:`here <supported_models>`.
Zhuohan Li's avatar
Zhuohan Li committed
60
61
62

.. code-block:: python

Woosuk Kwon's avatar
Woosuk Kwon committed
63
64
    llm = LLM(model="facebook/opt-125m")

65
66
67
68
69
.. note::

    By default, vLLM downloads models from `HuggingFace <https://huggingface.co/>`_. If you would like to use models from `ModelScope <https://www.modelscope.cn>`_, set the environment variable ``VLLM_USE_MODELSCOPE`` before initializing the engine.

Now, the fun part! The outputs are generated using ``llm.generate``. It adds the input prompts to the 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 of the output tokens.
Zhuohan Li's avatar
Zhuohan Li committed
70
71
72

.. code-block:: python

Woosuk Kwon's avatar
Woosuk Kwon committed
73
74
75
76
77
78
    outputs = llm.generate(prompts, sampling_params)

    for output in outputs:
        prompt = output.prompt
        generated_text = output.outputs[0].text
        print(f"Prompt: {prompt!r}, Generated text: {generated_text!r}")
Zhuohan Li's avatar
Zhuohan Li committed
79

80
.. _openai_compatible_server:
Zhuohan Li's avatar
Zhuohan Li committed
81
82
83
84

OpenAI-Compatible Server
------------------------

85
vLLM can be deployed as a server that implements the OpenAI API protocol. This allows vLLM to be used as a drop-in replacement for applications using OpenAI API.
86
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 and implements endpoints such as `list models <https://platform.openai.com/docs/api-reference/models/list>`_, `create chat completion <https://platform.openai.com/docs/api-reference/chat/completions/create>`_, and `create completion <https://platform.openai.com/docs/api-reference/completions/create>`_ endpoints. 
Zhuohan Li's avatar
Zhuohan Li committed
87

88
Run the following command to start the vLLM server with the `Qwen2.5-1.5B-Instruct <https://huggingface.co/Qwen/Qwen2.5-1.5B-Instruct>`_ model:
Zhuohan Li's avatar
Zhuohan Li committed
89
90
91

.. code-block:: console

92
    $ vllm serve Qwen/Qwen2.5-1.5B-Instruct
Zhuohan Li's avatar
Zhuohan Li committed
93

94
.. note::
95

96
    By default, the server uses a predefined chat template stored in the tokenizer. You can learn about overriding it `here <https://github.com/vllm-project/vllm/blob/main/docs/source/serving/openai_compatible_server.md#chat-template>`__.
Zhuohan Li's avatar
Zhuohan Li committed
97

98
This server can be queried in the same format as OpenAI API. For example, to list the models:
Zhuohan Li's avatar
Zhuohan Li committed
99
100
101
102
103

.. code-block:: console

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

104
105
You can pass in the argument ``--api-key`` or environment variable ``VLLM_API_KEY`` to enable the server to check for API key in the header.

106
107
OpenAI Completions API with vLLM
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
108

109
Once your server is started, you can query the model with input prompts:
Zhuohan Li's avatar
Zhuohan Li committed
110
111
112
113
114
115

.. code-block:: console

    $ curl http://localhost:8000/v1/completions \
    $     -H "Content-Type: application/json" \
    $     -d '{
116
    $         "model": "Qwen/Qwen2.5-1.5B-Instruct",
Zhuohan Li's avatar
Zhuohan Li committed
117
118
119
120
121
122
123
124
125
    $         "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:

.. code-block:: python

126
127
    from openai import OpenAI

Zhuohan Li's avatar
Zhuohan Li committed
128
    # Modify OpenAI's API key and API base to use vLLM's API server.
129
130
131
132
133
134
    openai_api_key = "EMPTY"
    openai_api_base = "http://localhost:8000/v1"
    client = OpenAI(
        api_key=openai_api_key,
        base_url=openai_api_base,
    )
135
    completion = client.completions.create(model="Qwen/Qwen2.5-1.5B-Instruct",
Zhuohan Li's avatar
Zhuohan Li committed
136
137
138
                                          prompt="San Francisco is a")
    print("Completion result:", completion)

139
A more detailed client example can be found `here <https://github.com/vllm-project/vllm/blob/main/examples/openai_completion_client.py>`__.
140

141
142
OpenAI Chat Completions API with vLLM
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
143

144
vLLM is designed to also support the OpenAI Chat Completions API. The chat interface is a more dynamic, 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.
145

146
You can use the `create chat completion <https://platform.openai.com/docs/api-reference/chat/completions/create>`_ endpoint to interact with the model:
147
148
149
150
151
152

.. code-block:: console

    $ curl http://localhost:8000/v1/chat/completions \
    $     -H "Content-Type: application/json" \
    $     -d '{
153
    $         "model": "Qwen/Qwen2.5-1.5B-Instruct",
154
155
156
157
158
159
    $         "messages": [
    $             {"role": "system", "content": "You are a helpful assistant."},
    $             {"role": "user", "content": "Who won the world series in 2020?"}
    $         ]
    $     }'

160
Alternatively, you can use the ``openai`` python package:
161
162
163

.. code-block:: python

164
    from openai import OpenAI
165
    # Set OpenAI's API key and API base to use vLLM's API server.
166
167
168
169
170
171
172
173
174
    openai_api_key = "EMPTY"
    openai_api_base = "http://localhost:8000/v1"

    client = OpenAI(
        api_key=openai_api_key,
        base_url=openai_api_base,
    )

    chat_response = client.chat.completions.create(
175
        model="Qwen/Qwen2.5-1.5B-Instruct",
176
177
178
179
180
181
        messages=[
            {"role": "system", "content": "You are a helpful assistant."},
            {"role": "user", "content": "Tell me a joke."},
        ]
    )
    print("Chat response:", chat_response)