通義千問數學模型是專門用于數學解題的語言模型,致力于解決復雜、具有挑戰性的數學問題。
此模型目前主要支持英語,后續將推出中英雙語版本。
模型概覽
商業版
模型名稱 | 上下文長度 | 最大輸入 | 最大輸出 | 輸入成本 | 輸出成本 | 免費額度 |
(Token數) | (每千Token) | |||||
qwen-math-plus | 4,096 | 3,072 | 3,072 | 0.004元 | 0.012元 | 100萬Token 有效期:百煉開通后180天內 |
qwen-math-plus-latest | 0.004元 | 0.012元 | ||||
qwen-math-plus-0919 | 0.004元 | 0.012元 | ||||
qwen-math-plus-0816 | 0.004元 | 0.012元 | ||||
qwen-math-turbo | 0.002元 | 0.006元 | ||||
qwen-math-turbo-latest | 0.002元 | 0.006元 | ||||
qwen-math-turbo-0919 | 0.002元 | 0.006元 |
開源版
模型名稱 | 上下文長度 | 最大輸入 | 最大輸出 | 輸入成本 | 輸出成本 | 免費額度 |
(Token數) | (每千Token) | |||||
qwen2.5-math-72b-instruct | 4,096 | 3,072 | 3,072 | 0.004元 | 0.012元 | 100萬Token 有效期:百煉開通后180天內 |
qwen2.5-math-7b-instruct | 0.001元 | 0.002元 | ||||
qwen2.5-math-1.5b-instruct | 限時免費 | |||||
qwen2-math-72b-instruct | 0.004元 | 0.012元 | ||||
qwen2-math-7b-instruct | 0.001元 | 0.002元 | ||||
qwen2-math-1.5b-instruct | 目前僅供免費體驗。 免費額度用完后不可調用,敬請關注后續動態。 |
qwen-math-plus-latest、qwen-math-plus-0919、qwen-math-turbo、qwen-math-turbo-latest、qwen-math-turbo-0919、qwen2.5-math模型的默認System Message為"Please reason step by step, and put your final answer within \\\\boxed{}."。
關于模型的限流條件,請參見限流。
示例代碼
您需要已獲取API Key并配置API Key到環境變量。如果通過OpenAI SDK或DashScope SDK進行調用,還需要安裝SDK。
通義千問數學模型擅長解決英文數學類問題,在您的輸入問題中請使用LaTex表示數學符號或公式,如:
$x$ 表示數學符號
$4x+5 = 6x+7$ 表示數學公式
此處以一元一次方程為例,向您展示通過OpenAI或者DashScope的方式體驗通義千問數學模型在數學問題上的能力。
OpenAI兼容
您可以通過OpenAI SDK或OpenAI兼容的HTTP方式調用通義千問數學模型。
Python
示例代碼
from openai import OpenAI
import os
def get_response():
client = OpenAI(
api_key=os.getenv("DASHSCOPE_API_KEY"), # 如果您沒有配置環境變量,請在此處用您的API Key進行替換
base_url="https://dashscope.aliyuncs.com/compatible-mode/v1", # 填寫DashScope服務的base_url
)
completion = client.chat.completions.create(
model="qwen2-math-72b-instruct",
messages=[
{'role': 'system', 'content': 'You are a helpful assistant.'},
{'role': 'user', 'content': 'Find the value of $x$ that satisfies the equation $4x+5 = 6x+7$.'}]
)
print(completion.model_dump_json())
if __name__ == '__main__':
get_response()
返回結果
{
"id": "chatcmpl-3ddf85f7-71c1-9fef-9c95-22671565895a",
"choices": [
{
"finish_reason": "stop",
"index": 0,
"logprobs": null,
"message": {
"content": "To solve the equation \\(4x + 5 = 6x + 7\\), we will follow a step-by-step approach to isolate the variable \\(x\\).\n\n1. **Start with the given equation:**\n \\[\n 4x + 5 = 6x + 7\n \\]\n\n2. **Subtract \\(4x\\) from both sides to eliminate \\(x\\) from the left side:**\n \\[\n 4x + 5 - 4x = 6x + 7 - 4x\n \\]\n Simplifying both sides, we get:\n \\[\n 5 = 2x + 7\n \\]\n\n3. **Subtract 7 from both sides to isolate the term with \\(x\\) on the right side:**\n \\[\n 5 - 7 = 2x + 7 - 7\n \\]\n Simplifying both sides, we get:\n \\[\n -2 = 2x\n \\]\n\n4. **Divide both sides by 2 to solve for \\(x\\):**\n \\[\n \\frac{-2}{2} = \\frac{2x}{2}\n \\]\n Simplifying both sides, we get:\n \\[\n -1 = x\n \\]\n\n5. **Write the final answer:**\n \\[\n \\boxed{-1}\n \\]\n\nThus, the value of \\(x\\) that satisfies the equation \\(4x + 5 = 6x + 7\\) is \\(\\boxed{-1}\\).",
"refusal": null,
"role": "assistant",
"function_call": null,
"tool_calls": null
}
}
],
"created": 1724068397,
"model": "qwen2-math-72b-instruct",
"object": "chat.completion",
"service_tier": null,
"system_fingerprint": null,
"usage": {
"completion_tokens": 331,
"prompt_tokens": 42,
"total_tokens": 373
}
}
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": "qwen2-math-72b-instruct",
"messages": [
{
"role": "system",
"content": "You are a helpful assistant."
},
{
"role": "user",
"content": "Find the value of $x$ that satisfies the equation $4x+5 = 6x+7$."
}
]
}'
返回結果
{
"choices": [
{
"message": {
"role": "assistant",
"content": "To solve the equation \\(4x + 5 = 6x + 7\\), we will follow a step-by-step approach to isolate the variable \\(x\\).\n\n1. **Start with the given equation:**\n \\[\n 4x + 5 = 6x + 7\n \\]\n\n2. **Subtract \\(4x\\) from both sides to eliminate \\(x\\) from the left side:**\n \\[\n 4x + 5 - 4x = 6x + 7 - 4x\n \\]\n Simplifying both sides, we get:\n \\[\n 5 = 2x + 7\n \\]\n\n3. **Subtract 7 from both sides to isolate the term with \\(x\\) on the right side:**\n \\[\n 5 - 7 = 2x + 7 - 7\n \\]\n Simplifying both sides, we get:\n \\[\n -2 = 2x\n \\]\n\n4. **Divide both sides by 2 to solve for \\(x\\):**\n \\[\n \\frac{-2}{2} = \\frac{2x}{2}\n \\]\n Simplifying both sides, we get:\n \\[\n -1 = x\n \\]\n\n5. **Write the final answer:**\n \\[\n \\boxed{-1}\n \\]\n\nThus, the value of \\(x\\) that satisfies the equation \\(4x + 5 = 6x + 7\\) is \\(\\boxed{-1}\\)."
},
"finish_reason": "stop",
"index": 0,
"logprobs": null
}
],
"object": "chat.completion",
"usage": {
"prompt_tokens": 42,
"completion_tokens": 331,
"total_tokens": 373
},
"created": 1724068476,
"system_fingerprint": null,
"model": "qwen2-math-72b-instruct",
"id": "chatcmpl-dc1cca46-dbe1-9543-a3fe-f0db8d05141e"
}
DashScope
您可以通過DashScope SDK或HTTP方式調用通義千問模型,體驗單輪對話的功能。
Python
示例代碼
import random
from http import HTTPStatus
import dashscope
def call_with_messages():
messages = [
{'role': 'user', 'content': 'Find the value of $x$ that satisfies the equation $4x+5 = 6x+7$.'}]
response = dashscope.Generation.call(
model='qwen2-math-72b-instruct',
messages=messages,
# 設置result_format為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()
返回結果
{
"status_code": 200,
"request_id": "021f9c99-fcc6-9a48-847f-45d9db78ea65",
"code": "",
"message": "",
"output": {
"text": null,
"finish_reason": null,
"choices": [
{
"finish_reason": "stop",
"message": {
"role": "assistant",
"content": "To solve the equation \\(4x + 5 = 6x + 7\\), we will follow a step-by-step approach to isolate the variable \\(x\\).\n\n1. **Start with the given equation:**\n \\[\n 4x + 5 = 6x + 7\n \\]\n\n2. **Subtract \\(4x\\) from both sides to eliminate \\(x\\) from the left side:**\n \\[\n 4x + 5 - 4x = 6x + 7 - 4x\n \\]\n Simplifying both sides, we get:\n \\[\n 5 = 2x + 7\n \\]\n\n3. **Subtract 7 from both sides to isolate the term with \\(x\\) on the right side:**\n \\[\n 5 - 7 = 2x + 7 - 7\n \\]\n Simplifying both sides, we get:\n \\[\n -2 = 2x\n \\]\n\n4. **Divide both sides by 2 to solve for \\(x\\):**\n \\[\n \\frac{-2}{2} = \\frac{2x}{2}\n \\]\n Simplifying both sides, we get:\n \\[\n -1 = x\n \\]\n\n5. **Write the final answer:**\n \\[\n \\boxed{-1}\n \\]\n\nThus, the value of \\(x\\) that satisfies the equation \\(4x + 5 = 6x + 7\\) is \\(\\boxed{-1}\\)."
}
}
]
},
"usage": {
"input_tokens": 31,
"output_tokens": 331,
"total_tokens": 362
}
}
Java
示例代碼
// Copyright (c) Alibaba, Inc. and its affiliates.
import java.util.Arrays;
import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.aigc.generation.models.QwenParam;
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("Find the value of $x$ that satisfies the equation $4x+5 = 6x+7$.").build();
QwenParam param =
QwenParam.builder().model("qwen2-math-72b-instruct").messages(Arrays.asList(userMsg))
.resultFormat(QwenParam.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());
}
System.exit(0);
}
}
返回結果
{
"requestId": "a9f3d0b9-41b0-9d49-8cb0-158974a1bdae",
"usage": {
"input_tokens": 31,
"output_tokens": 331,
"total_tokens": 362
},
"output": {
"choices": [
{
"finish_reason": "stop",
"message": {
"role": "assistant",
"content": "To solve the equation \\(4x + 5 = 6x + 7\\), we will follow a step-by-step approach to isolate the variable \\(x\\).\n\n1. **Start with the given equation:**\n \\[\n 4x + 5 = 6x + 7\n \\]\n\n2. **Subtract \\(4x\\) from both sides to eliminate \\(x\\) from the left side:**\n \\[\n 4x + 5 - 4x = 6x + 7 - 4x\n \\]\n Simplifying both sides, we get:\n \\[\n 5 = 2x + 7\n \\]\n\n3. **Subtract 7 from both sides to isolate the term with \\(x\\) on the right side:**\n \\[\n 5 - 7 = 2x + 7 - 7\n \\]\n Simplifying both sides, we get:\n \\[\n -2 = 2x\n \\]\n\n4. **Divide both sides by 2 to solve for \\(x\\):**\n \\[\n \\frac{-2}{2} = \\frac{2x}{2}\n \\]\n Simplifying both sides, we get:\n \\[\n -1 = x\n \\]\n\n5. **Write the final answer:**\n \\[\n \\boxed{-1}\n \\]\n\nThus, the value of \\(x\\) that satisfies the equation \\(4x + 5 = 6x + 7\\) is \\(\\boxed{-1}\\)."
}
}
]
}
}
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": "qwen2-math-72b-instruct",
"input":{
"messages":[
{
"role": "system",
"content": "You are a helpful assistant."
},
{
"role": "user",
"content": "Find the value of $x$ that satisfies the equation $4x+5 = 6x+7$."
}
]
},
"parameters": {
"result_format": "message"
}
}'
返回結果
{
"output": {
"choices": [
{
"finish_reason": "stop",
"message": {
"role": "assistant",
"content": "To solve the equation \\(4x + 5 = 6x + 7\\), we will follow a step-by-step approach to isolate the variable \\(x\\).\n\n1. **Start with the given equation:**\n \\[\n 4x + 5 = 6x + 7\n \\]\n\n2. **Subtract \\(4x\\) from both sides to eliminate \\(x\\) from the left side:**\n \\[\n 4x + 5 - 4x = 6x + 7 - 4x\n \\]\n Simplifying both sides, we get:\n \\[\n 5 = 2x + 7\n \\]\n\n3. **Subtract 7 from both sides to isolate the term with \\(x\\) on the right side:**\n \\[\n 5 - 7 = 2x + 7 - 7\n \\]\n Simplifying both sides, we get:\n \\[\n -2 = 2x\n \\]\n\n4. **Divide both sides by 2 to solve for \\(x\\):**\n \\[\n \\frac{-2}{2} = \\frac{2x}{2}\n \\]\n Simplifying both sides, we get:\n \\[\n -1 = x\n \\]\n\n5. **Write the final answer:**\n \\[\n \\boxed{-1}\n \\]\n\nThus, the value of \\(x\\) that satisfies the equation \\(4x + 5 = 6x + 7\\) is \\(\\boxed{-1}\\)."
}
}
]
},
"usage": {
"total_tokens": 373,
"output_tokens": 331,
"input_tokens": 42
},
"request_id": "ec5d01f6-3b78-95f2-a300-d01a601bacdf"
}
Partial Mode
如果您希望通義千問數學模型能夠嚴格地以指定前綴為基礎來生成文本,可以使用Partial Mode。您輸入的messages數組中最后一個message需要為assistant message,并且符合以下格式:
{
"role": "assistant",
"content": "您指定的前綴",
"partial": true
}
當前支持的數學模型為qwen-math-plus、qwen-math-plus-latest、qwen-math-plus-0919、qwen-math-turbo、qwen-math-turbo-latest、qwen-math-turbo-0919與qwen2.5-math系列模型。
OpenAI兼容
Python
示例代碼
import os
from openai import OpenAI
client = OpenAI(
api_key=os.getenv("DASHSCOPE_API_KEY"), # 如果您沒有配置環境變量,請在此處用您的API Key進行替換
base_url="https://dashscope.aliyuncs.com/compatible-mode/v1", # 填寫DashScope服務的base_url
)
completion = client.chat.completions.create(
model="qwen-math-plus",
messages=[{
"role": "user",
"content": "請按照應用題的格式解答問題。“斐波那契數列前10位是多少”"
},
{
"role": "assistant",
"content": "解:",
"partial": True
}]
)
print(completion.choices[0].message.content)
返回結果
斐波那契數列的定義是:數列的前兩個數是0和1,之后的每個數是前兩個數的和。根據這個定義,我們可以寫出數列的前10位。
1. 第1位:0
2. 第2位:1
3. 第3位:0 + 1 = 1
4. 第4位:1 + 1 = 2
5. 第5位:1 + 2 = 3
6. 第6位:2 + 3 = 5
7. 第7位:3 + 5 = 8
8. 第8位:5 + 8 = 13
9. 第9位:8 + 13 = 21
10. 第10位:13 + 21 = 34
因此,斐波那契數列的前10位是:0, 1, 1, 2, 3, 5, 8, 13, 21, 34。
\boxed{0, 1, 1, 2, 3, 5, 8, 13, 21, 34}
curl
示例代碼
curl -X POST https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "qwen-math-plus",
"messages": [{
"role": "user",
"content": "請按照應用題的格式解答問題。“斐波那契數列前10位是多少”"
},
{
"role": "assistant",
"content": "解:",
"partial": true
}]
}'
返回結果
{
"choices": [
{
"message": {
"role": "assistant",
"content": "斐波那契數列的定義是:數列的前兩個數是0和1,之后的每個數是前兩個數的和。根據這個定義,我們可以寫出數列的前10位。\n\n1. 第1位:0\n2. 第2位:1\n3. 第3位:0 + 1 = 1\n4. 第4位:1 + 1 = 2\n5. 第5位:1 + 2 = 3\n6. 第6位:2 + 3 = 5\n7. 第7位:3 + 5 = 8\n8. 第8位:5 + 8 = 13\n9. 第9位:8 + 13 = 21\n10. 第10位:13 + 21 = 34\n\n因此,斐波那契數列的前10位是:0, 1, 1, 2, 3, 5, 8, 13, 21, 34。\n\n\\boxed{0, 1, 1, 2, 3, 5, 8, 13, 21, 34}"
},
"finish_reason": "stop",
"index": 0,
"logprobs": null
}
],
"object": "chat.completion",
"usage": {
"prompt_tokens": 31,
"completion_tokens": 264,
"total_tokens": 295
},
"created": 1731900686,
"system_fingerprint": null,
"model": "qwen-math-plus",
"id": "chatcmpl-6ffcc74a-7a29-9b4b-8da1-88559f800507"
}
DashScope
Python
示例代碼
import os
import dashscope
messages = [{
"role": "user",
"content": "請按照應用題的格式解答問題。“斐波那契數列前10位是多少”"
},
{
"role": "assistant",
"content": "解:",
"partial": True
}]
response = dashscope.Generation.call(
# 若沒有配置環境變量,請用百煉API Key將下行替換為:api_key="sk-xxx",
api_key=os.getenv("DASHSCOPE_API_KEY"),
model='qwen-math-plus',
messages=messages,
result_format='message',
)
print(response.output.choices[0].message.content)
返回結果
斐波那契數列的定義是:數列的前兩個數是0和1,之后的每個數是前兩個數的和。根據這個定義,我們可以寫出數列的前10位。
1. 第1位:0
2. 第2位:1
3. 第3位:0 + 1 = 1
4. 第4位:1 + 1 = 2
5. 第5位:1 + 2 = 3
6. 第6位:2 + 3 = 5
7. 第7位:3 + 5 = 8
8. 第8位:5 + 8 = 13
9. 第9位:8 + 13 = 21
10. 第10位:13 + 21 = 34
因此,斐波那契數列的前10位是:0, 1, 1, 2, 3, 5, 8, 13, 21, 34。
\boxed{0, 1, 1, 2, 3, 5, 8, 13, 21, 34}
curl
示例代碼
curl -X POST "https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation" \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "qwen-math-plus",
"input":{
"messages":[{
"role": "user",
"content": "請按照應用題的格式解答問題。“斐波那契數列前10位是多少”"
},
{
"role": "assistant",
"content": "解:",
"partial": true
}]
},
"parameters": {
"result_format": "message"
}
}'
返回結果
{
"output": {
"choices": [
{
"finish_reason": "stop",
"message": {
"role": "assistant",
"content": "斐波那契數列的定義是:數列的前兩個數是0和1,之后的每個數是前兩個數的和。根據這個定義,我們可以寫出數列的前10位。\n\n1. 第1位:0\n2. 第2位:1\n3. 第3位:0 + 1 = 1\n4. 第4位:1 + 1 = 2\n5. 第5位:1 + 2 = 3\n6. 第6位:2 + 3 = 5\n7. 第7位:3 + 5 = 8\n8. 第8位:5 + 8 = 13\n9. 第9位:8 + 13 = 21\n10. 第10位:13 + 21 = 34\n\n因此,斐波那契數列的前10位是:0, 1, 1, 2, 3, 5, 8, 13, 21, 34。\n\n\\boxed{0, 1, 1, 2, 3, 5, 8, 13, 21, 34}"
}
}
]
},
"usage": {
"total_tokens": 295,
"output_tokens": 264,
"input_tokens": 31
},
"request_id": "4c7833e2-c17e-993f-a8ee-97103816a4c6"
}
常見問題
如何利用通義千問數學模型解答圖片中的數學問題?
通義千問數學模型暫不支持圖片識別功能。
API參考
關于通義千問數學模型的輸入與輸出參數,請參考通義千問。
錯誤碼
如果模型調用失敗并返回報錯信息,請參見錯誤碼進行解決。