-
Notifications
You must be signed in to change notification settings - Fork 1.9k
[TRTLLM-9242][doc] Add examples showcasing openai compatible APIs #9520
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
Open
JunyiXu-nv
wants to merge
2
commits into
NVIDIA:main
Choose a base branch
from
JunyiXu-nv:dev-junyi-add-examples-for-openai-api
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+726
−0
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| # OpenAI API Compatibility Examples | ||
|
|
||
| This directory contains individual, self-contained examples demonstrating TensorRT-LLM's OpenAI API compatibility. Examples are organized by API endpoint. | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| 1. **Start the trtllm-serve server:** | ||
| ```bash | ||
| trtllm-serve meta-llama/Llama-3.1-8B-Instruct | ||
| ``` | ||
|
|
||
| for reasoning model or model with tool calling ability. Specify `--tool_parser` and `--reasoning_parser`, e.g. | ||
|
|
||
| ```bash | ||
| trtllm-serve Qwen/Qwen3-8B --reasoning_parser "qwen3" --tool_parser "qwen3" | ||
| ``` | ||
|
|
||
|
|
||
| ## Running Examples | ||
|
|
||
| Each example is a standalone Python script. Run from the example's directory: | ||
|
|
||
| ```bash | ||
| # From chat_completions directory | ||
| cd chat_completions | ||
| python example_01_basic_chat.py | ||
| ``` | ||
|
|
||
| Or run with full path from the repository root: | ||
|
|
||
| ```bash | ||
| python examples/serve/compatibility/chat_completions/example_01_basic_chat.py | ||
| ``` | ||
|
|
||
| ### 📋 Complete Example List | ||
|
|
||
| All examples demonstrate the `/v1/chat/completions` endpoint: | ||
|
|
||
| | Example | File | Description | | ||
| |---------|------|-------------| | ||
| | **01** | `example_01_basic_chat.py` | Basic non-streaming chat completion | | ||
| | **02** | `example_02_streaming_chat.py` | Streaming responses with real-time delivery | | ||
| | **03** | `example_03_multi_turn_conversation.py` | Multi-turn conversation with context | | ||
| | **04** | `example_04_streaming_with_usage.py` | Streaming with continuous token usage stats | | ||
| | **05** | `example_05_json_mode.py` | Structured output with JSON schema | | ||
| | **06** | `example_06_tool_calling.py` | Function/tool calling with tools | | ||
| | **07** | `example_07_advanced_sampling.py` | TensorRT-LLM extended sampling parameters | | ||
|
|
||
| ## Configuration | ||
|
|
||
| All examples use these default settings: | ||
|
|
||
| ```python | ||
| base_url = "http://localhost:8000/v1" | ||
| api_key = "tensorrt_llm" # Can be any string | ||
| ``` | ||
|
|
||
| To use a different server: | ||
|
|
||
| ```python | ||
| client = OpenAI( | ||
| base_url="http://YOUR_SERVER:PORT/v1", | ||
| api_key="your_key", | ||
| ) | ||
| ``` | ||
|
|
||
| ## Model Requirements | ||
|
|
||
| Some examples require specific model capabilities: | ||
|
|
||
| | Example | Model Requirement | | ||
| |---------|------------------| | ||
| | 05 (JSON Mode) | xgrammar support | | ||
| | 06 (Tool Calling) | Tool-capable model (Llama 3.1+, Mistral Instruct, etc.) | | ||
| | Others | Any model | | ||
100 changes: 100 additions & 0 deletions
100
examples/serve/compatibility/chat_completions/README.md
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,100 @@ | ||
| # Chat Completions API Examples | ||
|
|
||
| Examples for the `/v1/chat/completions` endpoint - the most versatile API for conversational AI. | ||
|
|
||
| ## Quick Start | ||
|
|
||
| ```bash | ||
| # Run the basic example | ||
| python example_01_basic_chat.py | ||
| ``` | ||
|
|
||
| ## Examples Overview | ||
|
|
||
| ### Basic Examples | ||
|
|
||
| 1. **`example_01_basic_chat.py`** - Start here! | ||
| - Simple request/response | ||
| - Shows token usage | ||
| - Non-streaming mode | ||
|
|
||
| 2. **`example_02_streaming_chat.py`** - Real-time responses | ||
| - Stream tokens as generated | ||
| - Better UX for long responses | ||
| - Server-Sent Events (SSE) | ||
|
|
||
| 3. **`example_03_multi_turn_conversation.py`** - Context management | ||
| - Multiple conversation turns | ||
| - Conversation history | ||
| - Follow-up questions | ||
|
|
||
| 4. **`example_04_streaming_with_usage.py`** - Streaming + metrics | ||
| - Continuous token counts | ||
| - `stream_options` parameter | ||
| - Monitor resource usage | ||
|
|
||
| ### Advanced Examples | ||
|
|
||
| 5. **`example_05_json_mode.py`** - Structured output | ||
| - JSON schema validation | ||
| - Structured data extraction | ||
| - Requires xgrammar | ||
|
|
||
| 6. **`example_06_tool_calling.py`** - Function calling | ||
| - External tool integration | ||
| - Function definitions | ||
| - Requires compatible model (Qwen3, gpt_oss) | ||
|
|
||
| 7. **`example_07_advanced_sampling.py`** - Fine-tuned control | ||
| - `top_k`, `repetition_penalty` | ||
| - Custom stop sequences | ||
| - TensorRT-LLM extensions | ||
|
|
||
| ## Key Concepts | ||
|
|
||
| ### Non-Streaming vs Streaming | ||
|
|
||
| **Non-Streaming** (`stream=False`): | ||
| - Wait for complete response | ||
| - Single response object | ||
| - Simple to use | ||
|
|
||
| **Streaming** (`stream=True`): | ||
| - Tokens delivered as generated | ||
| - Better perceived latency | ||
| - Server-Sent Events (SSE) | ||
|
|
||
| ### Conversation Context | ||
|
|
||
| Messages accumulate in the `messages` array: | ||
| ```python | ||
| messages = [ | ||
| {"role": "system", "content": "You are helpful."}, | ||
| {"role": "user", "content": "Hello"}, | ||
| {"role": "assistant", "content": "Hi there!"}, | ||
| {"role": "user", "content": "How are you?"}, # Next turn | ||
| ] | ||
| ``` | ||
|
|
||
| ### Tool Calling | ||
|
|
||
| Define functions the model can call: | ||
| ```python | ||
| tools = [{ | ||
| "type": "function", | ||
| "function": { | ||
| "name": "get_weather", | ||
| "parameters": {...} | ||
| } | ||
| }] | ||
| ``` | ||
|
|
||
| ## Model Requirements | ||
|
|
||
| | Feature | Requirement | | ||
| |---------|-------------| | ||
| | Basic chat | Any model | | ||
| | Streaming | Any model | | ||
| | Multi-turn | Any model | | ||
| | JSON mode | xgrammar support | | ||
| | Tool calling | Compatible model (Qwen3 and gpt_oss.) | |
58 changes: 58 additions & 0 deletions
58
examples/serve/compatibility/chat_completions/example_01_basic_chat.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,58 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| #!/usr/bin/env python3 | ||
| """Example 1: Basic Non-Streaming Chat Completion. | ||
|
|
||
| Demonstrates a simple chat completion request with the OpenAI-compatible API. | ||
| """ | ||
JunyiXu-nv marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| from openai import OpenAI | ||
|
|
||
| # Initialize the client | ||
| client = OpenAI( | ||
| base_url="http://localhost:8000/v1", | ||
| api_key="tensorrt_llm", | ||
| ) | ||
|
|
||
| # Get the model name from the server | ||
| models = client.models.list() | ||
| model = models.data[0].id | ||
|
|
||
| print("=" * 80) | ||
| print("Example 1: Basic Non-Streaming Chat Completion") | ||
| print("=" * 80) | ||
| print() | ||
|
|
||
| # Create a simple chat completion | ||
| response = client.chat.completions.create( | ||
| model=model, | ||
| messages=[ | ||
| {"role": "system", "content": "You are a helpful assistant."}, | ||
| {"role": "user", "content": "What is the capital of France?"}, | ||
| ], | ||
| max_tokens=4096, | ||
| temperature=0.7, | ||
| ) | ||
|
|
||
| # Print the response | ||
| print("Response:") | ||
| print(f"Content: {response.choices[0].message.content}") | ||
| print(f"Finish reason: {response.choices[0].finish_reason}") | ||
| print( | ||
| f"Tokens used: {response.usage.total_tokens} " | ||
| f"(prompt: {response.usage.prompt_tokens}, " | ||
| f"completion: {response.usage.completion_tokens})" | ||
| ) | ||
76 changes: 76 additions & 0 deletions
76
examples/serve/compatibility/chat_completions/example_02_streaming_chat.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,76 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| #!/usr/bin/env python3 | ||
| """Example 2: Streaming Chat Completion. | ||
|
|
||
| Demonstrates streaming responses with real-time token delivery. | ||
| """ | ||
JunyiXu-nv marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| from openai import OpenAI | ||
|
|
||
| # Initialize the client | ||
| client = OpenAI( | ||
| base_url="http://localhost:8000/v1", | ||
| api_key="tensorrt_llm", | ||
| ) | ||
|
|
||
| # Get the model name from the server | ||
| models = client.models.list() | ||
| model = models.data[0].id | ||
|
|
||
| print("=" * 80) | ||
| print("Example 2: Streaming Chat Completion") | ||
| print("=" * 80) | ||
| print() | ||
|
|
||
| print("Prompt: Write a haiku about artificial intelligence\n") | ||
|
|
||
| # Create a streaming chat completion | ||
| stream = client.chat.completions.create( | ||
| model=model, | ||
| messages=[{"role": "user", "content": "Write a haiku about artificial intelligence"}], | ||
| max_tokens=4096, | ||
| temperature=0.8, | ||
| stream=True, | ||
| ) | ||
|
|
||
| # Print tokens as they arrive | ||
| print("Response (streaming):") | ||
| print("Assistant: ", end="", flush=True) | ||
|
|
||
| current_state = "none" | ||
| for chunk in stream: | ||
| has_content = hasattr(chunk.choices[0].delta, "content") and chunk.choices[0].delta.content | ||
| has_reasoning_content = ( | ||
| hasattr(chunk.choices[0].delta, "reasoning_content") | ||
| and chunk.choices[0].delta.reasoning_content | ||
| ) | ||
| if has_content: | ||
| if current_state != "content": | ||
| print("Content: ", end="", flush=True) | ||
| current_state = "content" | ||
|
|
||
| print(chunk.choices[0].delta.content, end="", flush=True) | ||
|
|
||
| if has_reasoning_content: | ||
| if current_state != "reasoning_content": | ||
| print("Reasoning: ", end="", flush=True) | ||
| current_state = "reasoning_content" | ||
|
|
||
| print(chunk.choices[0].delta.reasoning_content, end="", flush=True) | ||
| print("\n") | ||
|
|
||
| print("Stop reason: ", chunk.choices[0].finish_reason) | ||
73 changes: 73 additions & 0 deletions
73
examples/serve/compatibility/chat_completions/example_03_multi_turn_conversation.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,73 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| #!/usr/bin/env python3 | ||
| """Example 3: Multi-turn Conversation. | ||
|
|
||
| Demonstrates maintaining conversation context across multiple turns. | ||
| """ | ||
JunyiXu-nv marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| from openai import OpenAI | ||
|
|
||
| # Initialize the client | ||
| client = OpenAI( | ||
| base_url="http://localhost:8000/v1", | ||
| api_key="tensorrt_llm", | ||
| ) | ||
|
|
||
| # Get the model name from the server | ||
| models = client.models.list() | ||
| model = models.data[0].id | ||
|
|
||
| print("=" * 80) | ||
| print("Example 3: Multi-turn Conversation") | ||
| print("=" * 80) | ||
| print() | ||
|
|
||
| # Start a conversation with system message | ||
| messages = [ | ||
| {"role": "system", "content": "You are an expert mathematician."}, | ||
| ] | ||
|
|
||
| # First turn: User asks a question | ||
| messages.append({"role": "user", "content": "What is 15 multiplied by 23?"}) | ||
| print("USER: What is 15 multiplied by 23?") | ||
|
|
||
| response1 = client.chat.completions.create( | ||
| model=model, | ||
| messages=messages, | ||
| max_tokens=4096, | ||
| temperature=0, | ||
| ) | ||
|
|
||
| assistant_reply_1 = response1.choices[0].message.content | ||
| print(f"ASSISTANT: {assistant_reply_1}\n") | ||
|
|
||
| # Add assistant's response to conversation history | ||
| messages.append({"role": "assistant", "content": assistant_reply_1}) | ||
|
|
||
| # Second turn: User asks a follow-up question | ||
| messages.append({"role": "user", "content": "Now divide that result by 5"}) | ||
| print("USER: Now divide that result by 5") | ||
|
|
||
| response2 = client.chat.completions.create( | ||
| model=model, | ||
| messages=messages, | ||
| max_tokens=4096, | ||
| temperature=0, | ||
| ) | ||
|
|
||
| assistant_reply_2 = response2.choices[0].message.content | ||
| print(f"ASSISTANT: {assistant_reply_2}") | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be Qwen3?