Skip to main content

Overview

The Qwen Chat API provides methods for conversational interactions with the model. It supports both synchronous and streaming responses, multi-turn conversations with history, and custom system prompts.

chat() Method

Generate a complete response for a user query:

Parameters

AutoTokenizer
required
Tokenizer instance for encoding/decoding text
str
required
User’s current message or question
list[tuple[str, str]]
default:"None"
Conversation history as list of (user_message, assistant_response) tuples:
str
default:"You are a helpful assistant."
System prompt defining the assistant’s behavior and role
list[list[int]]
default:"None"
Token ID sequences that trigger generation termination:
dict
Additional generation parameters (see GenerationConfig)

Returns

str
The model’s generated response text
list[tuple[str, str]]
Updated conversation history including the current exchange

chat_stream() Method

Generate a streaming response for real-time display:

Parameters

Same as chat() method.

Yields

str
Incrementally generated response text. Each yield contains the full response up to the current point (not just the delta).

Multi-turn Conversation Example

Streaming Response Example

Custom System Prompts

Using Stop Words

Generation with Parameters

Chat Message Format

Internally, chat messages use the ChatML format:
The chat() and chat_stream() methods handle this formatting automatically.