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-***", # 🔑 换成你在 AiHubMix 生成的密钥
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
)
# 某些 chunk 对象可能没有 choices 属性或 choices 是一个空列表,处理方法:
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="")
from openai import OpenAI
client = OpenAI(
api_key="sk-***", # 🔑 换成你在 AiHubMix 生成的密钥
base_url="https://aihubmix.com/v1",
)
# 定义工具
tools = [
{
"type": "function",
"function": {
"name": "get_current_weather",
"description": "获取指定位置的当前天气",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "城市名称,如北京、上海等"
},
"unit": {
"type": "string",
"enum": ["celsius", "fahrenheit"],
"description": "温度单位"
}
},
"required": ["location"]
}
}
}
]
# 创建聊天完成请求,包含工具定义
completion = client.chat.completions.create(
model="Qwen/Qwen3-30B-A3B", #2.5 和 3 都支持,QwQ 不支持
messages=[
{
"role": "user",
"content": "北京今天的天气怎么样?"
}
],
tools=tools,
tool_choice="auto", # 让模型自行决定是否使用工具
stream=True
)
# 用于收集工具调用信息的字典
tool_calls = {}
# 处理流式响应
for chunk in completion:
if not hasattr(chunk.choices, '__len__') or len(chunk.choices) == 0:
continue
delta = chunk.choices[0].delta
# 处理文本内容
if hasattr(delta, 'content') and delta.content:
print(delta.content, end="")
# 处理工具调用
if hasattr(delta, 'tool_calls') and delta.tool_calls:
for tool_call in delta.tool_calls:
if not hasattr(tool_call, 'index'):
continue
idx = tool_call.index
if idx not in tool_calls:
tool_calls[idx] = {"name": "", "arguments": ""}
if hasattr(tool_call, 'function'):
if hasattr(tool_call.function, 'name') and tool_call.function.name:
tool_calls[idx]["name"] = tool_call.function.name
if hasattr(tool_call.function, 'arguments') and tool_call.function.arguments:
tool_calls[idx]["arguments"] += tool_call.function.arguments
# 完成后,打印收集到的工具调用信息
for idx, info in tool_calls.items():
if info["name"]:
print(f"\n工具调用:{info['name']}")
if info["arguments"]:
print(f"参数:{info['arguments']}")
from qwen_agent.agents import Assistant
import os
# Define LLM
llm_cfg = {
'model': 'Qwen/Qwen3-30B-A3B',
# Use a custom endpoint compatible with OpenAI API:
'model_server': 'https://aihubmix.com/v1',
'api_key': os.getenv('AIHUBMIX_API_KEY'),
# Other parameters:
# 'generate_cfg': {
# # Add: When the response content is `<think>this is the thought</think>this is the answer;
# # Do not add: When the response has been separated by reasoning_content and content.
# 'thought_in_content': True,
# },
}
# Define Tools
tools = [
{'mcpServers': { # You can specify the MCP configuration file
'time': {
'command': 'uvx',
'args': ['mcp-server-time', '--local-timezone=Asia/Shanghai']
},
"fetch": {
"command": "uvx",
"args": ["mcp-server-fetch"]
}
}
},
'code_interpreter', # Built-in tools
]
# Define Agent
bot = Assistant(llm=llm_cfg, function_list=tools)
# Streaming generation
messages = [{'role': 'user', 'content': 'https://qwenlm.github.io/blog/ Introduce the latest developments of Qwen'}]
for responses in bot.run(messages=messages):
pass
print(responses)
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-***", # 🔑 换成你在 AiHubMix 生成的密钥
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 编码
base64_image = encode_image(image_path)
# 创建包含文本和图像的消息
completion = client.chat.completions.create(
model="qwen2.5-vl-72b-instruct", #qwen2.5-vl-72b-instruct 或 Qwen/QVQ-72B-Preview
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="")
from openai import OpenAI
client = OpenAI(
api_key="sk-***", # 🔑 换成你在 AiHubMix 生成的密钥
base_url="https://aihubmix.com/v1",
)
completion = client.chat.completions.create(
model="Qwen/QwQ-32B",
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": "支配宇宙的元规则是什么?"}
]
}
],
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="")
更新时间:2026-06-01