# 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 server with the reasoningparser:```bashvllm serve deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B \ --reasoning-parser deepseek_r1```Unlike openai_chat_completion_with_reasoning.py, this example demonstrates thestreaming chat completions feature.The streaming chat completions feature allows you to receive chat completionsin real-time as they are generated by the model. This is useful for scenarioswhere you want to display chat completions to the user as they are generatedby the model.Remember to check content and reasoning_content exist in `ChatCompletionChunk`,content may not exist leading to errors if you try to access it."""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"messages=[{"role":"user","content":"9.11 and 9.8, which is greater?"}]defmain():client=OpenAI(api_key=openai_api_key,base_url=openai_api_base,)models=client.models.list()model=models.data[0].id# ruff: noqa: E501# For granite: add: `extra_body={"chat_template_kwargs": {"thinking": True}}`stream=client.chat.completions.create(model=model,messages=messages,stream=True)print("client: Start streaming chat completions...")printed_reasoning_content=Falseprinted_content=Falseforchunkinstream:# Safely extract reasoning_content and content from delta,# defaulting to None if attributes don't exist or are empty stringsreasoning_content=(getattr(chunk.choices[0].delta,"reasoning_content",None)orNone)content=getattr(chunk.choices[0].delta,"content",None)orNoneifreasoning_contentisnotNone:ifnotprinted_reasoning_content:printed_reasoning_content=Trueprint("reasoning_content:",end="",flush=True)print(reasoning_content,end="",flush=True)elifcontentisnotNone:ifnotprinted_content:printed_content=Trueprint("\ncontent:",end="",flush=True)# Extract and print the contentprint(content,end="",flush=True)if__name__=="__main__":main()