Série Qwen 3
Qwen3 redéfinit les LLM ouverts grâce à des modes de thinking dynamiques, excellant dans le code, les mathématiques et le raisonnement multilingue. Propulsé par des paramètres actifs sparse de 22 milliards, il combine une vitesse fulgurante et une intelligence profonde — entièrement open-source, du modèle léger aux géants 235B. 1. Utilisation de base : redirection au format compatible OpenAI.2. Appel d’outils : les outils classiques prennent en charge le format compatible OpenAI, tandis que les MCP Tools s’appuient sur qwen-agent et nécessitent d’installer d’abord les dépendances avec la commande :
pip install -U qwen-agent mcp.
Pour plus de détails, consultez la documentation officielle Ali
from openai import OpenAI
client = OpenAI(
api_key="sk-***", # 🔑 Replace it by your AiHubMix Key
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="")
from openai import OpenAI
client = OpenAI(
api_key="sk-***", # 🔑 Replace it by your AiHubMix Key
base_url="https://aihubmix.com/v1",
)
# Define Tools
tools = [
{
"type": "function",
"function": {
"name": "get_current_weather",
"description": "Get the current weather of a specified location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "City name, e.g., Beijing, Shanghai, etc."
},
"unit": {
"type": "string",
"enum": ["celsius", "fahrenheit"],
"description": "Temperature unit"
}
},
"required": ["location"]
}
}
}
]
# Create chat completion request with tool definitions
completion = client.chat.completions.create(
model="Qwen/Qwen3-30B-A3B", # Supported by both 2.5 and 3, not supported by QwQ
messages=[
{
"role": "user",
"content": "What's the weather like in Beijing today?"
}
],
tools=tools,
tool_choice="auto", # Let the model decide whether to use a tool
stream=True
)
# Dictionary for collecting tool call info
tool_calls = {}
# Handle streaming responses
for chunk in completion:
if not hasattr(chunk.choices, '__len__') or len(chunk.choices) == 0:
continue
delta = chunk.choices[0].delta
# Handle textual content
if hasattr(delta, 'content') and delta.content:
print(delta.content, end="")
# Handle tool calls
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
# After completion, print collected tool call info
for idx, info in tool_calls.items():
if info["name"]:
print(f"\nTool call: {info['name']}")
if info["arguments"]:
print(f"Arguments: {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)
Série Qwen 2.5 et QwQ/QvQ
Utilisez le format compatible OpenAI pour la redirection ; la différence est que l’appel en streaming doit extrairechunk.choices[0].delta.content, voir ci-dessous.
1. QvQ, Qwen 2.5 VL : reconnaissance d’images.2. QwQ : tâche textuelle.
from openai import OpenAI
import base64
import os
client = OpenAI(
api_key="sk-***", # 🔑 Replace it by your AiHubMix Key
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 file does not exist: {image_path}")
with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode('utf-8')
# Get the base64 encoding of the image
base64_image = encode_image(image_path)
completion = client.chat.completions.create(
model="qwen2.5-vl-72b-instruct", #qwen2.5-vl-72b-instruct OR Qwen/QVQ-72B-Preview
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": "Please describe this image in detail"},
{
"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-***", # 🔑 Replace it by your AiHubMix Key
base_url="https://aihubmix.com/v1",
)
completion = client.chat.completions.create(
model="Qwen/QwQ-32B",
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": "What is the meta rule that dominates the universe?"}
]
}
],
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="")
Dernière mise à jour : 2026-06-01