Qwen 3 系列
Qwen3 系列是阿里推出的新一代开源大模型,能力大幅跃升:在代码理解、数学推理、多语言表达、复杂推断任务上,比肩甚至超越了目前市面上的顶级模型(如 o1、DeepSeek-R1)。它的核心突破在于引入了「思考模式」与「非思考模式」切换机制,让模型在面对不同难度任务时,自主调节推理深度,实现了速度与精度的双优平衡。 旗舰版 Qwen3-235B 采用稀疏激活,仅用 22B 参数推理,兼顾成本和卓越能力。全系模型全面开源,涵盖从轻量到超大规模需求。
1. 基础用法: 用 OpenAI 兼容格式转发。
2. 工具调用: 常规 Tools 调用支持 OpenAI 兼容格式(适用于 V2.5、V3),而 MCP Tools 依赖 qwen-agent
,需要先运行指令安装依赖:pip install -U qwen-agent mcp
。
更多细节可以参考阿里官方文档
from openai import OpenAI
client = OpenAI(
api_key="sk-***",
base_url="https://aihubmix.com/v1",
)
completion = client.chat.completions.create(
model="Qwen/Qwen3-30B-A3B",
messages=[
{
"role": "user",
"content": "Explain the Occam's Razor concept and provide everyday examples of it"
}
],
stream=True
)
for chunk in completion:
if hasattr(chunk.choices, '__len__') and len(chunk.choices) > 0:
if hasattr(chunk.choices[0].delta, 'content') and chunk.choices[0].delta.content is not None:
print(chunk.choices[0].delta.content, end="")
QvQ、Qwen 2.5 和 QwQ 系列
用 OpenAI 的兼容格式转发即可,区别在于流式调用的提取,需要剔除为空的 chunk.choices[0].delta.content
,参考如下。
1. QvQ、Qwen 2.5 VL: 图片识别
2. QwQ: 文本任务
Qwen/QVQ-72B-Preview
是基于 Qwen2-VL-72B
构建的开源多模态推理模型,专注于视觉推理和跨模态任务。
from openai import OpenAI
import base64
import os
client = OpenAI(
api_key="sk-***",
base_url="https://aihubmix.com/v1",
)
image_path = "yourpath/file.png"
def encode_image(image_path):
if not os.path.exists(image_path):
raise FileNotFoundError(f"图片文件不存在:{image_path}")
with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode('utf-8')
base64_image = encode_image(image_path)
completion = client.chat.completions.create(
model="qwen2.5-vl-72b-instruct",
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": "请详细描述这张图片,包括图片中的内容、风格和可能的含义。"},
{
"type": "image_url",
"image_url": {
"url": f"data:image/png;base64,{base64_image}"
}
}
]
}
],
stream=True
)
for chunk in completion:
if hasattr(chunk.choices, '__len__') and len(chunk.choices) > 0:
if hasattr(chunk.choices[0].delta, 'content') and chunk.choices[0].delta.content is not None:
print(chunk.choices[0].delta.content, end="")