意图理解能力

通义千问的意图理解模型能够在毫秒级时间内快速、准确地解析用户意图,并选择合适的工具来解决用户的问题。

支持的模型

模型名称

上下文长度

最大输入

最大输出

输入成本

输出成本

免费额度

(注)

(Token数)

(每千Token)

tongyi-intent-detect-v3

8,192

8,192

1,024

0.0008

0.002

100Token

有效期:百炼开通后180天内

使用方法

前提条件

您需要已获取API Key配置API Key到环境变量。如果通过OpenAI SDKDashScope SDK进行调用,还需要安装SDK

为了使用通义千问意图理解模型的意图理解能力,您需要按照以下方式设置System Message:

You are Qwen, created by Alibaba Cloud. You are a helpful assistant. You may call one or more tools to assist with the user query. The tools you can use are as follows:
{工具信息}
Response in INTENT_MODE.

您需要在System Message中说明Response in INTENT_MODE.并且放入可能使用到的工具信息。工具信息的格式为:

[{
    "name": "工具1的名称",
    "description": "工具1的描述",
    "parameters": {
        "type": "参数的类型,一般为object",
        "properties": {
            "parameter_1": {
                "description": "parameter_1的描述",
                "type": "parameter_1的类型",
                "default": "parameter_1的默认值"
            },
            ...
            "parameter_n": {
                "description": "parameter_n的描述",
                "type": "parameter_n的类型",
                "default": "parameter_n的默认值"
            }
        }
    },
    "required": [
        "parameter_1",
        ...
        "parameter_n"
    ]
},
...
{
    "name": "工具n的名称",
    "description": "工具n的描述",
    "parameters": {
        "type": "参数的类型,一般为object",
        "properties": {
            "parameter_1": {
                "description": "parameter_1的描述",
                "type": "parameter_1的类型",
                "default": "parameter_1的默认值"
            },
            ...
            "parameter_n": {
                "description": "parameter_n的描述",
                "type": "parameter_n的类型",
                "default": "parameter_n的默认值"
            }
        }
    },
    "required": [
        "parameter_1",
        ...
        "parameter_n"
    ]
}]

开始使用

假设您的业务场景需要使用时间查询与天气查询两个工具,工具信息为:

[{
    "type": "function",
    "function": {
        "name": "get_current_time",
        "description": "当你想知道现在的时间时非常有用。",
        "parameters": {}
    }
},
{
    "type": "function",
    "function": {
        "name": "get_current_weather",
        "description": "当你想查询指定城市的天气时非常有用。",
        "parameters": {  
            "type": "object",
            "properties": {
                "location": {
                    "type": "string",
                    "description": "城市或县区,比如北京市、杭州市、余杭区等。"
                }
            }
        },
        "required": [
            "location"
        ]
    }
}]

请求示例

OpenAI兼容

import os
from openai import OpenAI

system_prompt = """You are Qwen, created by Alibaba Cloud. You are a helpful assistant. You may call one or more tools to assist with the user query. The tools you can use are as follows:
[{
    "type": "function",
    "function": {
        "name": "get_current_time",
        "description": "当你想知道现在的时间时非常有用。",
        "parameters": {}
    }
},
{
    "type": "function",
    "function": {
        "name": "get_current_weather",
        "description": "当你想查询指定城市的天气时非常有用。",
        "parameters": { 
            "type": "object",
            "properties": {
                "location": {
                    "type": "string",
                    "description": "城市或县区,比如北京市、杭州市、余杭区等。"
                }
            }
        },
        "required": [
            "location"
        ]
    }
}]
Response in INTENT_MODE.
"""
client = OpenAI(
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
)
messages = [
    {'role': 'system', 'content': system_prompt},
    {'role': 'user', 'content': "杭州天气"}
    ]
response = client.chat.completions.create(
    model="tongyi-intent-detect-v3",
    messages=messages
)

print(response.choices[0].message.content)

DashScope

import os
from dashscope import Generation

system_prompt = """You are Qwen, created by Alibaba Cloud. You are a helpful assistant. You may call one or more tools to assist with the user query. The tools you can use are as follows:
[{
    "type": "function",
    "function": {
        "name": "get_current_time",
        "description": "当你想知道现在的时间时非常有用。",
        "parameters": {}
    }
},
{
    "type": "function",
    "function": {
        "name": "get_current_weather",
        "description": "当你想查询指定城市的天气时非常有用。",
        "parameters": { 
            "type": "object",
            "properties": {
                "location": {
                    "type": "string",
                    "description": "城市或县区,比如北京市、杭州市、余杭区等。"
                }
            }
        },
        "required": [
            "location"
        ]
    }
}]
Response in INTENT_MODE.
"""
messages = [
    {'role': 'system', 'content': system_prompt},
    {'role': 'user', 'content': "杭州天气"}
    ]
response = Generation.call(
    # 若没有配置环境变量,请用百炼API Key将下行替换为:api_key = "sk-xxx",
    api_key=os.getenv("DASHSCOPE_API_KEY"), 
    model="tongyi-intent-detect-v3",
    messages=messages,
    result_format="message"
)

print(response.output.choices[0].message.content)

响应示例

<tags>
[function call, json response]
</tags><tool_call>
[{"name": "get_current_weather", "arguments": {"location": "杭州市"}}]
</tool_call><content>

</content>

在得到响应后,您需要使用parse_text函数解析出返回的工具与参数信息:

import re


def parse_text(text):
    # 定义正则表达式模式来匹配 <tags>, <tool_call>, <content> 及其内容
    tags_pattern = r'<tags>(.*?)</tags>'
    tool_call_pattern = r'<tool_call>(.*?)</tool_call>'
    content_pattern = r'<content>(.*?)</content>'
    # 使用正则表达式查找匹配的内容
    tags_match = re.search(tags_pattern, text, re.DOTALL)
    tool_call_match = re.search(tool_call_pattern, text, re.DOTALL)
    content_match = re.search(content_pattern, text, re.DOTALL)
    # 提取匹配的内容,如果没有匹配到则返回空字符串
    tags = tags_match.group(1).strip() if tags_match else ""
    tool_call = tool_call_match.group(1).strip() if tool_call_match else ""
    content = content_match.group(1).strip() if content_match else ""
    # 将提取的内容存储在字典中
    result = {
      "tags": tags,
      "tool_call": tool_call,
      "content": content
    }
    return result

response = """<tags>
[function call, json response]
</tags><tool_call>
[{"name": "get_current_weather", "arguments": {"location": "杭州市"}}]
</tool_call><content>

</content>"""
print(parse_text(response))

得到输出为:

{
    "tags": "[function call, json response]",
    "tool_call": [
        {
            "name": "get_current_weather",
            "arguments": {
                "location": "杭州市"
            }
        }
    ],
    "content": ""
}

其中tags为模型生成的意图判断,tool_call为模型生成的函数调用响应结果,content为模型生成的自然回复,当进行函数调用时无内容生成。

如果您需要根据tool_calls响应结果进行函数调用并完成后续的Function Call流程,请参考Function Call(工具调用)