数学能力

通义千问数学模型是专门用于数学解题的语言模型,致力于解决复杂、具有挑战性的数学问题。

此模型目前主要支持英语,后续将推出中英双语版本。

模型概览

商业版

模型名称

上下文长度

最大输入

最大输出

输入成本

输出成本

免费额度

(Token数)

(每千Token)

qwen-math-plus

4,096

3,072

3,072

0.004

0.012

100Token

有效期:百炼开通后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

100Token

有效期:百炼开通后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 SDKDashScope SDK进行调用,还需要安装SDK

通义千问数学模型擅长解决英文数学类问题,在您的输入问题中请使用LaTex表示数学符号或公式,如:

$x$ 表示数学符号
$4x+5 = 6x+7$ 表示数学公式

此处以一元一次方程为例,向您展示通过OpenAI或者DashScope的方式体验通义千问数学模型在数学问题上的能力。

OpenAI兼容

您可以通过OpenAI SDKOpenAI兼容的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 SDKHTTP方式调用通义千问模型,体验单轮对话的功能。

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-0919qwen2.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"
}

常见问题

如何利用通义千问数学模型解答图片中的数学问题?

通义千问数学模型暂不支持图片识别功能。

  • 如果图片中是简单的数学问题,可以使用通义千问VL模型进行解答。

  • 如果图片中包含复杂的数学问题,可以先使用通义千问VL模型提取图片中的文字,再使用通义千问数学模型解答问题。

API参考

关于通义千问数学模型的输入与输出参数,请参考通义千问