The Qwen-Math models from Model Studio offer powerful mathematical reasoning and computational capabilities. They provide detailed, step-by-step solutions that are easy to understand and verify.
We recommend using the latest general-purpose models (see Select Models) instead of the Qwen-Math models, which are based on the older Qwen2.5.
This document applies only to the China (Beijing) region, and you must use the China (Beijing) region's API key.
Models and pricing
Commercial Models
Model name | Input price | Output price | Context window | Max input | Max output | Free quota |
(per million tokens) | (tokens) | |||||
qwen-math-plus | CNY 4.00 | CNY 12.00 | 4,096 | 3,072 | 3,072 | 1 million input tokens and 1 million output tokens Valid for 90 days after you activate Model Studio. |
qwen-math-turbo | CNY 2.00 | CNY 6.00 | ||||
The free quota for each model is not shared.
For information about model rate limits, see rate limiting.
Quick start
Create an API key and export the API key as an environment variable. If you use the OpenAI SDK or DashScope SDK to make calls, install the SDK.
OpenAI compatible
Python
from openai import OpenAI
import os
client = OpenAI(
api_key=os.getenv("DASHSCOPE_API_KEY"), # For security, do not hard-code your API key. Use an environment variable or a secrets management service.
base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
)
completion = client.chat.completions.create(
model="qwen-math-plus",
messages=[{'role': 'user', 'content': 'Derive a universal solution for the quadratic equation $ Ax^2+Bx+C=0 $'}])
print(completion.choices[0].message.content)curl
curl --location "https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions" \
--header "Authorization: Bearer $DASHSCOPE_API_KEY" \
--header "Content-Type: application/json" \
--data '{
"model": "qwen-math-plus",
"messages": [
{
"role": "user",
"content": "Derive a universal solution for the quadratic equation $ Ax^2+Bx+C=0 $"
}
]
}'DashScope
Python
from http import HTTPStatus
import dashscope
def call_with_messages():
messages = [
{'role': 'user', 'content': 'Derive a universal solution for the quadratic equation $ Ax^2+Bx+C=0 $'}]
response = dashscope.Generation.call(
model='qwen-math-plus',
messages=messages,
# Set result_format to 'message'.
result_format='message',
)
if response.status_code == HTTPStatus.OK:
print(response)
else:
print('Request id: %s, Status code: %s, error code: %s, error message: %s' % (
response.request_id, response.status_code,
response.code, response.message
))
if __name__ == '__main__':
call_with_messages()
Java
import java.util.Arrays;
import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.aigc.generation.GenerationParam;
import com.alibaba.dashscope.common.Message;
import com.alibaba.dashscope.common.Role;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
public class Main {
public static void callWithMessage()
throws NoApiKeyException, ApiException, InputRequiredException {
Generation gen = new Generation();
Message userMsg = Message.builder().role(Role.USER.getValue()).content("Derive a universal solution for the quadratic equation $ Ax^2+Bx+C=0 $").build();
GenerationParam param =GenerationParam.builder()
.apiKey(System.getenv("DASHSCOPE_API_KEY"))
.model("qwen-math-plus")
.messages(Arrays.asList(userMsg))
.resultFormat(GenerationParam.ResultFormat.MESSAGE)
.build();
GenerationResult result = gen.call(param);
System.out.println(result);
}
public static void main(String[] args){
try {
callWithMessage();
} catch (ApiException | NoApiKeyException | InputRequiredException e) {
System.out.println(e.getMessage());
}
}
}
curl
curl --location "https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation" \
--header "Authorization: Bearer $DASHSCOPE_API_KEY" \
--header "Content-Type: application/json" \
--data '{
"model": "qwen-math-plus",
"input":{
"messages":[
{
"role": "user",
"content": "Derive a universal solution for the quadratic equation $ Ax^2+Bx+C=0 $"
}
]
},
"parameters": {
"result_format": "message"
}
}'You can also go to the Model Studio console to try out the model.
Recommendations
Use English and LaTeX: The Qwen-Math model is designed for mathematical problems in English. Use LaTeX to represent mathematical symbols and formulas in your input.
Control content output: Partial mode provides precise control to ensure that the model's output closely follows the provided prefix. This improves the accuracy and control of the generated results.
The
temperatureparameter is set to0by default. Do not adjust this setting.Easily extract results: The Qwen-Math model outputs the final result in a
\boxed{}block by default.
Frequently asked questions
How to use the Qwen-Math model to solve math problems in images? |
The Qwen-Math model does not currently support image recognition.
For information about the input and output parameters of the Qwen-Math model, see the Qwen API Reference. |
Where can I find detailed information about error codes? |
If the model call fails and returns an error message, see Error codes for resolution. |