|
| 1 | +(reasoning-outputs)= |
| 2 | + |
| 3 | +# Reasoning Outputs |
| 4 | + |
| 5 | +vLLM offers support for reasoning models like [DeepSeek R1](https://huggingface.co/deepseek-ai/DeepSeek-R1), which are designed to generate outputs containing both reasoning steps and final conclusions. |
| 6 | + |
| 7 | +Reasoning models return a additional `reasoning_content` field in their outputs, which contains the reasoning steps that led to the final conclusion. This field is not present in the outputs of other models. |
| 8 | + |
| 9 | +## Supported Models |
| 10 | + |
| 11 | +vLLM currently supports the following reasoning models: |
| 12 | + |
| 13 | +- [DeepSeek R1 series](https://huggingface.co/collections/deepseek-ai/deepseek-r1-678e1e131c0169c0bc89728d) (`deepseek_r1`, which looks for `<think> ... </think>`) |
| 14 | + |
| 15 | +## Quickstart |
| 16 | + |
| 17 | +To use reasoning models, you need to specify the `--enable-reasoning` and `--reasoning-parser` flags when making a request to the chat completion endpoint. The `--reasoning-parser` flag specifies the reasoning parser to use for extracting reasoning content from the model output. |
| 18 | + |
| 19 | +```bash |
| 20 | +vllm serve deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B \ |
| 21 | + --enable-reasoning --reasoning-parser deepseek_r1 |
| 22 | +``` |
| 23 | + |
| 24 | +Next, make a request to the model that should return the reasoning content in the response. |
| 25 | + |
| 26 | +```python |
| 27 | +from openai import OpenAI |
| 28 | + |
| 29 | +# Modify OpenAI's API key and API base to use vLLM's API server. |
| 30 | +openai_api_key = "EMPTY" |
| 31 | +openai_api_base = "http://localhost:8000/v1" |
| 32 | + |
| 33 | +client = OpenAI( |
| 34 | + api_key=openai_api_key, |
| 35 | + base_url=openai_api_base, |
| 36 | +) |
| 37 | + |
| 38 | +models = client.models.list() |
| 39 | +model = models.data[0].id |
| 40 | + |
| 41 | +# Round 1 |
| 42 | +messages = [{"role": "user", "content": "9.11 and 9.8, which is greater?"}] |
| 43 | +response = client.chat.completions.create(model=model, messages=messages) |
| 44 | + |
| 45 | +reasoning_content = response.choices[0].message.reasoning_content |
| 46 | +content = response.choices[0].message.content |
| 47 | + |
| 48 | +print("reasoning_content:", reasoning_content) |
| 49 | +print("content:", content) |
| 50 | +``` |
| 51 | + |
| 52 | +The `reasoning_content` field contains the reasoning steps that led to the final conclusion, while the `content` field contains the final conclusion. |
| 53 | + |
| 54 | +## Streaming chat completions |
| 55 | + |
| 56 | +Streaming chat completions are also supported for reasoning models. The `reasoning_content` field is available in the `delta` field in [chat completion response chunks](https://platform.openai.com/docs/api-reference/chat/streaming). |
| 57 | + |
| 58 | +```json |
| 59 | +{ |
| 60 | + "id": "chatcmpl-123", |
| 61 | + "object": "chat.completion.chunk", |
| 62 | + "created": 1694268190, |
| 63 | + "model": "deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B", |
| 64 | + "system_fingerprint": "fp_44709d6fcb", |
| 65 | + "choices": [ |
| 66 | + { |
| 67 | + "index": 0, |
| 68 | + "delta": { |
| 69 | + "role": "assistant", |
| 70 | + "reasoning_content": "is", |
| 71 | + }, |
| 72 | + "logprobs": null, |
| 73 | + "finish_reason": null |
| 74 | + } |
| 75 | + ] |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +Please note that it is not compatible with the OpenAI Python client library. You can use the `requests` library to make streaming requests. |
| 80 | + |
| 81 | +## How to support a new reasoning model |
| 82 | + |
| 83 | +You can add a new `ReasoningParser` similar to `vllm/entrypoints/openai/reasoning_parsers/deepseek_r1_reasoning_parser.py`. |
| 84 | + |
| 85 | +```python |
| 86 | +# import the required packages |
| 87 | + |
| 88 | +from vllm.entrypoints.openai.reasoning_parsers.abs_reasoning_parsers import ( |
| 89 | + ReasoningParser, ReasoningParserManager) |
| 90 | +from vllm.entrypoints.openai.protocol import (ChatCompletionRequest, |
| 91 | + DeltaMessage) |
| 92 | + |
| 93 | +# define a reasoning parser and register it to vllm |
| 94 | +# the name list in register_module can be used |
| 95 | +# in --reasoning-parser. |
| 96 | +@ReasoningParserManager.register_module(["example"]) |
| 97 | +class ExampleParser(ReasoningParser): |
| 98 | + def __init__(self, tokenizer: AnyTokenizer): |
| 99 | + super().__init__(tokenizer) |
| 100 | + |
| 101 | + def extract_reasoning_content_streaming( |
| 102 | + self, |
| 103 | + previous_text: str, |
| 104 | + current_text: str, |
| 105 | + delta_text: str, |
| 106 | + previous_token_ids: Sequence[int], |
| 107 | + current_token_ids: Sequence[int], |
| 108 | + delta_token_ids: Sequence[int], |
| 109 | + ) -> Union[DeltaMessage, None]: |
| 110 | + """ |
| 111 | + Instance method that should be implemented for extracting reasoning |
| 112 | + from an incomplete response; for use when handling reasoning calls and |
| 113 | + streaming. Has to be an instance method because it requires state - |
| 114 | + the current tokens/diffs, but also the information about what has |
| 115 | + previously been parsed and extracted (see constructor) |
| 116 | + """ |
| 117 | + |
| 118 | + def extract_reasoning_content( |
| 119 | + self, model_output: str, request: ChatCompletionRequest |
| 120 | + ) -> Tuple[Optional[str], Optional[str]]: |
| 121 | + """ |
| 122 | + Extract reasoning content from a complete model-generated string. |
| 123 | +
|
| 124 | + Used for non-streaming responses where we have the entire model response |
| 125 | + available before sending to the client. |
| 126 | +
|
| 127 | + Parameters: |
| 128 | + model_output: str |
| 129 | + The model-generated string to extract reasoning content from. |
| 130 | +
|
| 131 | + request: ChatCompletionRequest |
| 132 | + The request object that was used to generate the model_output. |
| 133 | +
|
| 134 | + Returns: |
| 135 | + Tuple[Optional[str], Optional[str]] |
| 136 | + A tuple containing the reasoning content and the content. |
| 137 | + """ |
| 138 | +``` |
| 139 | + |
| 140 | +After defining the reasoning parser, you can use it by specifying the `--reasoning-parser` flag when making a request to the chat completion endpoint. |
| 141 | + |
| 142 | +```bash |
| 143 | +vllm serve <model_tag> \ |
| 144 | + --enable-reasoning --reasoning-parser example |
| 145 | +``` |
| 146 | + |
| 147 | +## Limitations |
| 148 | + |
| 149 | +- The reasoning content is only available for online serving's chat completion endpoint (`/v1/chat/completions`). |
| 150 | +- It is not compatible with the [`structured_outputs`](#structured_outputs) and [`tool_calling`](#tool_calling) features. |
| 151 | +- The reasoning content is not available for all models. Check the model's documentation to see if it supports reasoning. |
0 commit comments