πŸ€– AI/ML

Qwen

Tongyi Qianwen

Alibaba's open-source large language model. Excellent multilingual support and coding ability.

πŸ“– Detailed Explanation

Qwen (Tongyi Qianwen) is a series of large language models developed by Alibaba Cloud. Since its first release in 2023, it has advanced rapidly, and in 2024 the Qwen2.5 series achieved top-tier performance among open-source LLMs. In particular, it supports 29 languages, including Chinese and English, and its strong understanding of Asian languages leads to excellent performance on Korean tasks as well.

The Qwen2.5 series is offered in various sizes (0.5B, 1.5B, 3B, 7B, 14B, 32B, 72B), so it can be deployed in diverse environments, from edge devices to data centers. In particular, Qwen2.5-Coder supports 92 programming languages and shows GPT-4-level performance in code generation and debugging. Qwen2.5-Math is specialized in solving math problems, and Qwen-VL is a multimodal model that processes images and text together.

Technically, Qwen2.5 uses Grouped Query Attention (GQA), the SwiGLU activation function, and RoPE positional encoding. It supports a default context length of 128K tokens, which can be extended up to 1M tokens using the YaRN technique. It is licensed under Apache 2.0, so commercial use is free, and the weights can be downloaded to run locally or fine-tune. It is easily accessible on Hugging Face, ModelScope, Ollama, and more.

Alibaba Cloud also offers Qwen models as a cloud service through the DashScope API. Its pricing is lower than OpenAI's (about 1/3 to 1/10), making it a good option for companies that need to comply with data regulations in China. It also provides the Qwen-Agent framework, which makes it easy to build AI agents capable of tool use, RAG, and code execution. In the global market, together with Llama and Mistral, it forms a three-way leading group of open-source LLMs.

πŸ’» Code Example

Here are various ways to run Qwen models locally or call them via API:

# 1. Transformers둜 둜컬 μ‹€ν–‰ (GPU ν•„μš”)
from transformers import AutoModelForCausalLM, AutoTokenizer

model_name = "Qwen/Qwen2.5-7B-Instruct"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(
    model_name,
    torch_dtype="auto",
    device_map="auto"
)

messages = [
    {"role": "system", "content": "당신은 μΉœμ ˆν•œ AI λΉ„μ„œμž…λ‹ˆλ‹€."},
    {"role": "user", "content": "파이썬으둜 ν”Όλ³΄λ‚˜μΉ˜ μˆ˜μ—΄μ„ κ΅¬ν˜„ν•΄μ£Όμ„Έμš”."}
]

text = tokenizer.apply_chat_template(
    messages,
    tokenize=False,
    add_generation_prompt=True
)
inputs = tokenizer(text, return_tensors="pt").to(model.device)

outputs = model.generate(
    **inputs,
    max_new_tokens=512,
    temperature=0.7,
    do_sample=True
)
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
print(response)

# 2. Ollama둜 κ°„νŽΈ μ‹€ν–‰ (μ„€μΉ˜: ollama pull qwen2.5:7b)
import ollama

response = ollama.chat(
    model='qwen2.5:7b',
    messages=[
        {'role': 'user', 'content': 'ν•œκ΅­μ˜ μˆ˜λ„λŠ” μ–΄λ””μΈκ°€μš”?'}
    ]
)
print(response['message']['content'])

# 3. DashScope API 호좜 (Alibaba Cloud)
import dashscope
from dashscope import Generation

dashscope.api_key = "your-api-key"

response = Generation.call(
    model="qwen-max",
    messages=[
        {"role": "system", "content": "당신은 μ „λ¬Έ λ²ˆμ—­κ°€μž…λ‹ˆλ‹€."},
        {"role": "user", "content": "Translate to Korean: Hello, how are you?"}
    ],
    result_format='message'
)
print(response.output.choices[0].message.content)

# 4. OpenAI ν˜Έν™˜ API (vLLM μ„œλ²„)
from openai import OpenAI

client = OpenAI(
    base_url="http://localhost:8000/v1",  # vLLM μ„œλ²„ μ£Όμ†Œ
    api_key="not-needed"
)

response = client.chat.completions.create(
    model="Qwen/Qwen2.5-7B-Instruct",
    messages=[
        {"role": "user", "content": "JSON ν˜•μ‹μœΌλ‘œ ν•œκ΅­ λŒ€λ„μ‹œ 5개λ₯Ό μ•Œλ €μ£Όμ„Έμš”."}
    ],
    response_format={"type": "json_object"}
)
print(response.choices[0].message.content)

# 5. Qwen-VL λ©€ν‹°λͺ¨λ‹¬ (이미지 + ν…μŠ€νŠΈ)
from transformers import Qwen2VLForConditionalGeneration, AutoProcessor

model = Qwen2VLForConditionalGeneration.from_pretrained(
    "Qwen/Qwen2-VL-7B-Instruct",
    torch_dtype="auto",
    device_map="auto"
)
processor = AutoProcessor.from_pretrained("Qwen/Qwen2-VL-7B-Instruct")

messages = [{
    "role": "user",
    "content": [
        {"type": "image", "image": "https://example.com/image.jpg"},
        {"type": "text", "text": "이 이미지λ₯Ό μ„€λͺ…ν•΄μ£Όμ„Έμš”."}
    ]
}]
# ... 처리 및 생성

πŸ“Š Performance & Cost

DashScope API Pricing (as of January 2025):

Model Input (1M tokens) Output (1M tokens) Context
Qwen-Max $1.68 $6.72 32K
Qwen-Plus $0.42 $1.26 128K
Qwen-Turbo $0.0525 $0.21 128K
Qwen-Long $0.07 $0.28 1M

Self-Hosting GPU Requirements:

Model Size FP16 INT8 INT4
Qwen2.5-7B 16GB 10GB 6GB
Qwen2.5-14B 32GB 18GB 10GB
Qwen2.5-72B 160GB 80GB 48GB

Tip: Compared with GPT-4o, Qwen-Plus offers similar performance at about 1/7 the price. In particular, it offers excellent cost-performance on Asian language tasks.

πŸ—£οΈ Say It Like This in Practice

πŸ’Ό In Meetings

"Qwen2.5 is a good choice for a Korean chatbot. It has abundant Asian-language training data, so its understanding of Korean is higher than Llama's, and since it is Apache 2.0, there are no restrictions on commercial use. With a 7B model, you can serve it on a single RTX 4090."

πŸ‘” In Interviews

"The strengths of Qwen2.5-Coder are its support for 92 programming languages and its fill-in-the-middle capability. On the HumanEval benchmark, the 7B model outperforms GPT-3.5-turbo, and the 32B comes close to GPT-4. It is well suited to code autocompletion and refactoring tools."

πŸ”§ In Technical Discussions

"Qwen's long-context handling is impressive. With YaRN it can be extended from 128K to 1M, and in needle-in-haystack tests it maintains high recall up to 700K. It is well suited to analyzing legal documents or entire codebases."

Take a mock interview on this topic β†’ WIA SOOM AI Interview Β· Start for free

⚠️ Common Mistakes & Cautions

❌
Model version confusion: Qwen, Qwen1.5, Qwen2, and Qwen2.5 have different architectures. Since the latest version (Qwen2.5) has the best performance, use the latest version unless you have a specific reason not to. Check the checkpoint name carefully.
❌
Dependence on Chinese servers: When downloading from ModelScope, you may run into speed issues with Chinese servers. Downloading through Hugging Face Hub or Ollama may be faster. Also, the DashScope API may be affected by Chinese regulations.
βœ…
Best Practice: Using the Instruct version (-Instruct suffix) gives better results on conversational tasks. Use Qwen-Coder for code generation and Qwen-Math for math. You must apply chat_template correctly for performance to come out properly.

πŸ”— Related Terms

πŸ“š Learn More