# SPDX-License-Identifier: Apache-2.0# SPDX-FileCopyrightText: Copyright contributors to the vLLM project"""An example shows how to generate chat completions from reasoning modelslike DeepSeekR1.To run this example, you need to start the vLLM serverwith the reasoning parser:```bashvllm serve deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B \ --reasoning-parser deepseek_r1```This example demonstrates how to generate chat completions from reasoning modelsusing the OpenAI Python client library."""fromopenaiimportOpenAI# 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"defmain():client=OpenAI(api_key=openai_api_key,base_url=openai_api_base,)models=client.models.list()model=models.data[0].id# Round 1messages=[{"role":"user","content":"9.11 and 9.8, which is greater?"}]# ruff: noqa: E501# For granite, add: `extra_body={"chat_template_kwargs": {"thinking": True}}`response=client.chat.completions.create(model=model,messages=messages)reasoning_content=response.choices[0].message.reasoning_contentcontent=response.choices[0].message.contentprint("reasoning_content for Round 1:",reasoning_content)print("content for Round 1:",content)# Round 2messages.append({"role":"assistant","content":content})messages.append({"role":"user","content":"How many Rs are there in the word 'strawberry'?",})response=client.chat.completions.create(model=model,messages=messages)reasoning_content=response.choices[0].message.reasoning_contentcontent=response.choices[0].message.contentprint("reasoning_content for Round 2:",reasoning_content)print("content for Round 2:",content)if__name__=="__main__":main()