Skip to main content
The CLI demo provides a powerful command-line interface for interacting with Qwen-Chat models. This demo supports multi-turn conversations with history management, streaming responses, and configurable generation parameters.

Overview

The CLI demo (cli_demo.py) offers an interactive chat experience directly in your terminal with features including:
  • Real-time streaming responses
  • Conversation history management
  • Dynamic generation configuration
  • Random seed control for reproducibility
  • CPU-only mode support

Installation

1

Install Dependencies

Make sure you have the required packages installed:
2

Optional: Install Flash Attention

For improved performance (GPU only):

Basic Usage

Quick Start

Run the demo with default settings (Qwen-7B-Chat):

Command-Line Options

The CLI demo supports the following arguments:
string
default:"Qwen/Qwen-7B-Chat"
Model checkpoint name or path from HuggingFace/ModelScope
integer
default:"1234"
Random seed for reproducible generation
flag
Run the demo with CPU only (no GPU required)

Usage Examples

Interactive Commands

Once the demo is running, you can use these special commands:

Help and Information

Session Management

Configuration

Configuration changes persist for the current session only.
View Configuration:
Modify Configuration:
Reset to Default:

Common Configuration Parameters

Random Seed Control

Check Current Seed:
Set New Seed:

Example Session

Here’s what a typical interaction looks like:
cli_demo.py:19

Features

Streaming Responses

The CLI demo uses model.chat_stream() method to provide real-time streaming responses:
cli_demo.py:198
This creates a smooth, interactive experience where you see the response being generated token by token.

History Management

Conversations are automatically tracked:
cli_demo.py:206
You can:
  • View all previous exchanges with :history
  • Clear history with :clear-his to start fresh
  • History is preserved across multiple turns

Keyboard Interrupt Handling

Press Ctrl+C during generation to interrupt:
cli_demo.py:202

Performance Tips

GPU Memory Management: The demo includes automatic garbage collection and CUDA cache clearing when you clear history or screen.

Memory Optimization

The demo automatically manages memory:
cli_demo.py:68
Memory is cleared when:
  • Clearing the screen (:clear)
  • Clearing history (:clear-his)

Device Selection

Troubleshooting

First-time model loading downloads the model from HuggingFace/ModelScope. This can take time depending on your connection. Subsequent runs will use the cached model.Consider downloading the model manually:
Try these solutions:
  1. Use a smaller model (e.g., Qwen-1.8B-Chat instead of Qwen-7B-Chat)
  2. Enable CPU-only mode with --cpu-only
  3. Use quantized models (Int4 or Int8 versions)
  4. Clear history frequently with :clear-his
The demo handles encoding errors automatically:
cli_demo.py:95
If issues persist, check your terminal’s encoding settings.
Try:
  1. Adjusting temperature: :conf temperature=0.7 (lower = more focused)
  2. Changing the random seed: :seed 42
  3. Resetting config: :reset-conf
  4. Clearing history if context is confusing: :clear-his

Source Code Reference

The CLI demo implementation can be found at cli_demo.py:1 in the Qwen repository. Key components:
  • Model loading: cli_demo.py:44
  • Main loop: cli_demo.py:105
  • Command processing: cli_demo.py:128
  • Chat streaming: cli_demo.py:198

Next Steps

Web Demo

Try the Gradio-based web interface

Model API

Integrate Qwen into your applications