-
-
Notifications
You must be signed in to change notification settings - Fork 10.4k
[v0][structured output] Support reasoning output #12955
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
515a0c7
[v0][structured output] Support reasoning output
gaocegege 7c7d835
chore: Add docs
gaocegege 1dc8f57
resolve the conflicts and address comments
gaocegege 1d634b0
make pre commit happy
gaocegege 2abb04e
fix test cases
gaocegege aa5c218
refactor: Simplify the code base
gaocegege c04291f
address comments
gaocegege 807be61
fix docs
gaocegege 2c40d3a
fix import
gaocegege 732c583
fix
gaocegege 17969af
fix: Fix log
gaocegege f895fa2
fix format
gaocegege File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
examples/online_serving/openai_chat_completion_structured_outputs_with_reasoning.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# SPDX-License-Identifier: Apache-2.0 | ||
""" | ||
An example shows how to generate structured outputs from reasoning models | ||
like DeepSeekR1. The thinking process will not be guided by the JSON | ||
schema provided by the user. Only the final output will be structured. | ||
|
||
To run this example, you need to start the vLLM server with the reasoning | ||
parser: | ||
|
||
```bash | ||
vllm serve deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B \ | ||
--enable-reasoning --reasoning-parser deepseek_r1 | ||
``` | ||
|
||
This example demonstrates how to generate chat completions from reasoning models | ||
using the OpenAI Python client library. | ||
""" | ||
|
||
from enum import Enum | ||
|
||
from openai import OpenAI | ||
from pydantic import BaseModel | ||
|
||
# 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" | ||
|
||
client = OpenAI( | ||
api_key=openai_api_key, | ||
base_url=openai_api_base, | ||
) | ||
|
||
models = client.models.list() | ||
model = models.data[0].id | ||
|
||
|
||
# Guided decoding by JSON using Pydantic schema | ||
class CarType(str, Enum): | ||
sedan = "sedan" | ||
suv = "SUV" | ||
truck = "Truck" | ||
coupe = "Coupe" | ||
|
||
|
||
class CarDescription(BaseModel): | ||
brand: str | ||
model: str | ||
car_type: CarType | ||
|
||
|
||
json_schema = CarDescription.model_json_schema() | ||
|
||
prompt = ("Generate a JSON with the brand, model and car_type of" | ||
"the most iconic car from the 90's, think in 100 tokens") | ||
completion = client.chat.completions.create( | ||
model=model, | ||
messages=[{ | ||
"role": "user", | ||
"content": prompt, | ||
}], | ||
extra_body={"guided_json": json_schema}, | ||
) | ||
print("content", completion.choices[0].message.content) | ||
print("reasoning_content: ", completion.choices[0].message.reasoning_content) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.