DEV Community

ChinaWHAPI Team
ChinaWHAPI Team

Posted on

How to Use DeepSeek and Qwen with One OpenAI-Compatible API

How to Use DeepSeek and Qwen with One OpenAI-Compatible API

If you're building AI applications with Chinese LLMs like DeepSeek, Qwen, Kimi, or GLM, you've probably noticed that each provider has its own API format, authentication method, and SDK. This makes it painful to switch between models or build multi-model applications.

In this tutorial, I'll show you how to use one OpenAI-compatible API to access DeepSeek, Qwen, and 200+ other Chinese LLMs with minimal code changes.

Why Use an OpenAI-Compatible Gateway?

Most developers are already familiar with the OpenAI SDK. Instead of learning new APIs for each Chinese LLM provider, you can use an OpenAI-compatible gateway that:

  • ✅ Uses the same SDK you already know
  • ✅ Requires only changing base_url and model name
  • ✅ Provides one API key for all models
  • ✅ Supports streaming, function calling, and all OpenAI features
  • ✅ Handles rate limiting, fallback, and cost optimization

Prerequisites

Before we start, you'll need:

  1. Python 3.8+ or Node.js 16+
  2. An API key from ChinaWHAPI (they offer 200K free credits for testing)
  3. Basic knowledge of the OpenAI SDK

Step 1: Install the OpenAI SDK

Python

pip install openai
Enter fullscreen mode Exit fullscreen mode

Node.js

npm install openai
Enter fullscreen mode Exit fullscreen mode

Step 2: Configure Your Client

The key insight is that you only need to change two things from your standard OpenAI setup:

  1. The base_url (pointing to the gateway instead of OpenAI)
  2. The model name (using Chinese LLM identifiers)

Python Example

from openai import OpenAI

client = OpenAI(
    api_key="your_chinawhapi_api_key",
    base_url="https://api.chinawhapi.com/v1"
)

# Use DeepSeek
response = client.chat.completions.create(
    model="deepseek-chat",
    messages=[
        {"role": "user", "content": "Hello! What can you do?"}
    ]
)

print(response.choices[0].message.content)
Enter fullscreen mode Exit fullscreen mode

Node.js Example

import OpenAI from 'openai';

const client = new OpenAI({
  apiKey: process.env.CHINAWHAPI_API_KEY,
  baseURL: 'https://api.chinawhapi.com/v1'
});

// Use Qwen
const response = await client.chat.completions.create({
  model: 'qwen-plus',
  messages: [{ role: 'user', content: 'Hello! What can you do?' }]
});

console.log(response.choices[0].message.content);
Enter fullscreen mode Exit fullscreen mode

That's it! You're now using Chinese LLMs with the same code structure you'd use for GPT-4.

Supported Models

ChinaWHAPI provides access to 200+ models including:

DeepSeek Series

  • deepseek-chat - DeepSeek-V2 Chat
  • deepseek-coder - DeepSeek-Coder for programming tasks

Qwen Series (Alibaba)

  • qwen-max - Most capable Qwen model
  • qwen-plus - Balanced performance and cost
  • qwen-turbo - Fast and affordable
  • qwen2.5-72b - Latest Qwen2.5 72B

Kimi (Moonshot AI)

  • kimi-chat - Kimi chat model with long context

GLM Series (Zhipu AI)

  • glm-edge - GLM-Edge for general tasks
  • glm-flash - Fast and lightweight

Other Models

  • Doubao (ByteDance)
  • MiniMax (Abab series)
  • ERNIE Bot (Baidu)

Streaming Support

Streaming works exactly like OpenAI:

Python Streaming

from openai import OpenAI

client = OpenAI(
    api_key="your_api_key",
    base_url="https://api.chinawhapi.com/v1"
)

stream = client.chat.completions.create(
    model="deepseek-chat",
    messages=[{"role": "user", "content": "Tell me a story"}],
    stream=True
)

for chunk in stream:
    if chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="")
Enter fullscreen mode Exit fullscreen mode

Node.js Streaming

const stream = await client.chat.completions.create({
  model: 'qwen-plus',
  messages: [{ role: 'user', content: 'Tell me a story' }],
  stream: true
});

for await (const chunk of stream) {
  process.stdout.write(chunk.choices[0]?.delta?.content || '');
}
Enter fullscreen mode Exit fullscreen mode

Cost Comparison

One of the main advantages of Chinese LLMs is cost efficiency. Here's a rough comparison (per 1M tokens):

Model Input Cost Output Cost
GPT-4 Turbo $10.00 $30.00
DeepSeek-V2 $0.14 $0.28
Qwen-Plus $0.40 $1.20
GLM-Edge $0.20 $0.80

With ChinaWHAPI's 4.5% service fee, you're still saving 90%+ compared to GPT-4.

Production Tips

1. Environment Variables

Never hardcode your API key:

# .env file
CHINAWHAPI_API_KEY=your_api_key_here
CHINAWHAPI_BASE_URL=https://api.chinawhapi.com/v1
Enter fullscreen mode Exit fullscreen mode
import os
from openai import OpenAI

client = OpenAI(
    api_key=os.getenv("CHINAWHAPI_API_KEY"),
    base_url=os.getenv("CHINAWHAPI_BASE_URL")
)
Enter fullscreen mode Exit fullscreen mode

2. Error Handling

from openai import OpenAI, APIError

client = OpenAI(
    api_key="your_api_key",
    base_url="https://api.chinawhapi.com/v1"
)

try:
    response = client.chat.completions.create(
        model="deepseek-chat",
        messages=[{"role": "user", "content": "Hello"}]
    )
    print(response.choices[0].message.content)
except APIError as e:
    print(f"API Error: {e.status_code} - {e.message}")
Enter fullscreen mode Exit fullscreen mode

3. Model Fallback

For production apps, implement fallback logic:

def chat_with_fallback(messages):
    models = ["deepseek-chat", "qwen-plus", "glm-edge"]

    for model in models:
        try:
            response = client.chat.completions.create(
                model=model,
                messages=messages
            )
            return response.choices[0].message.content
        except APIError:
            continue

    raise Exception("All models failed")
Enter fullscreen mode Exit fullscreen mode

Complete Working Example

Here's a complete Python script you can run immediately:

from openai import OpenAI
import os

# Initialize client
client = OpenAI(
    api_key=os.getenv("CHINAWHAPI_API_KEY"),
    base_url="https://api.chinawhapi.com/v1"
)

# Simple chat function
def chat(message):
    response = client.chat.completions.create(
        model="deepseek-chat",
        messages=[{"role": "user", "content": message}]
    )
    return response.choices[0].message.content

# Test it
if __name__ == "__main__":
    print("Testing DeepSeek...")
    result = chat("What is the capital of France?")
    print(f"Answer: {result}")
Enter fullscreen mode Exit fullscreen mode

Getting Started

  1. Sign up: Visit ChinaWHAPI and create an account
  2. Get free credits: New users receive 200K free credits
  3. Create API key: Go to dashboard → API Keys → Create New Key
  4. Start coding: Use the examples above with your API key

Useful Resources

Conclusion

Using Chinese LLMs doesn't require rewriting your entire codebase. With an OpenAI-compatible gateway like ChinaWHAPI, you can:

  • Access DeepSeek, Qwen, Kimi, GLM, and 200+ models
  • Use the same OpenAI SDK you already know
  • Save 90%+ on API costs
  • Deploy to production in minutes

The migration is as simple as changing two lines of code. Give it a try with the free credits and see how much you can save!


Have questions? Drop them in the comments below or check out the ChinaWHAPI documentation.

Top comments (0)