Skip to main content
Deploy Qwen models with a production-ready API server that’s compatible with OpenAI’s API format. This allows you to use existing OpenAI client libraries and tools with Qwen.

Quick Start

1

Install Dependencies

Install the required packages:
2

Download the API Script

The openai_api.py script is included in the Qwen repository:
3

Launch the Server

Start the API server with default settings:
The server will start on http://127.0.0.1:8000 by default. Visit http://localhost:8000/docs for interactive API documentation.

Configuration Options

Command Line Arguments

string
default:"Qwen/Qwen-7B-Chat"
Model checkpoint name or path. Can be:
  • HuggingFace model name: Qwen/Qwen-7B-Chat
  • Local path: /path/to/model
int
default:"8000"
Port to run the API server on
string
default:"127.0.0.1"
Server bind address:
  • 127.0.0.1: Local access only
  • 0.0.0.0: Accept connections from any network interface
boolean
default:"false"
Run the model on CPU only (not recommended for production)
boolean
default:"false"
Disable garbage collection after each response (improves latency but increases memory usage)
string
Enable basic HTTP authentication in format username:password

API Usage

Using OpenAI Python Client

Using cURL

Request Parameters

string
required
Model identifier (ignored by server, uses the loaded model)
array
required
Array of message objects with role and content fields
float
default:"1.0"
Sampling temperature (0.0 to 2.0). Lower values make output more focused and deterministic
float
default:"1.0"
Nucleus sampling parameter. Alternative to temperature
int
Top-k sampling parameter. Limits token selection to top k options
int
Maximum total sequence length (prompt + completion)
boolean
default:"false"
Enable streaming responses
array
Array of stop sequences to halt generation
array
Array of function definitions for function calling

Production Deployment

Using Gunicorn

For production deployments with multiple workers:
Multiple workers require multiple GPUs or CPU-only deployment. Each worker loads a full model instance.

Using Systemd Service

Create a systemd service file /etc/systemd/system/qwen-api.service:
Enable and start the service:

Behind Nginx Reverse Proxy

Nginx configuration for SSL termination and load balancing:

Authentication

Basic HTTP Authentication

Enable authentication when starting the server:
Client usage:

Custom Authentication

For OAuth2, JWT, or custom authentication, modify the openai_api.py script to add middleware:

Monitoring

Health Check Endpoint

Add a health check endpoint to your deployment:

Logging

Enable detailed logging:

Performance Tips

  • Use --disable-gc for lower latency at the cost of higher memory usage
  • Enable KV cache quantization in the model config
  • Use quantized models (Int4/Int8) for reduced VRAM requirements
  • Use vLLM-based deployment for high-concurrency scenarios
  • Enable Flash Attention 2 in the model
  • Consider multi-GPU deployment with tensor parallelism
  • Use smaller models when possible
  • Implement request batching
  • Use bfloat16 precision instead of float32
  • Pre-load model at startup

Troubleshooting

Error: RuntimeError: CUDA out of memorySolution: Use a smaller model or quantized version:
Issue: API responses are slowSolutions:
  • Install Flash Attention 2
  • Use GPU instead of CPU
  • Reduce max_length parameter
  • Consider vLLM deployment for better performance
Error: Cannot connect to API serverSolution: Ensure server is bound to correct interface:
Check firewall rules and network configuration.

Next Steps

Docker Deployment

Deploy with Docker for easier management

vLLM Deployment

Use vLLM for high-performance production inference

Production Guide

Best practices for production deployments

API Reference

Complete API documentation