Skip to main content
This example demonstrates how to create an interactive chat application using ONNX Runtime GenAI. The application maintains conversation context and supports streaming responses.

Overview

The chat example shows how to:
  • Apply chat templates to format messages
  • Maintain conversation history
  • Stream token generation in real-time
  • Handle system and user prompts
  • Optionally rewind conversation history

Complete Example

Key Features

Chat Template Application

The example uses apply_chat_template() to properly format messages according to the model’s expected format:

Streaming Output

Tokens are streamed in real-time as they’re generated:

Conversation Rewinding

Optionally reset conversation history while keeping the system prompt:

Running the Example

Basic usage:
With verbose output and timings:
With custom system prompt:
With conversation rewinding (no history):

Command-Line Arguments

Next Steps