Skip to main content
More Models

Intent recognition

Identifies user intents in milliseconds and selects appropriate tools to address queries.

China (Beijing) region only. Use an API key from China (Beijing).
Alibaba Cloud Model Studio has released workspace-specific domains for the China (Beijing), Singapore, and China (Hong Kong) regions. The new dedicated domains deliver superior performance and higher stability for inference requests. We recommend migrating to the new domains:
  • China (Beijing): from https://dashscope.aliyuncs.com to https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com
  • Singapore: from https://dashscope-intl.aliyuncs.com to https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com
  • China (Hong Kong): from https://cn-hongkong.dashscope.aliyuncs.com to https://{WorkspaceId}.cn-hongkong.maas.aliyuncs.com
{WorkspaceId} is your workspace ID, which can be found on the Workspace Details page in the Alibaba Cloud Model Studio console. The existing domain remains fully functional.

Supported models

Model

Context window

Max input

Max output

Input price

Output price

(tokens)

(per 1M tokens)

tongyi-intent-detect-v3

8,192

8,192

1,024

$0.058

$0.144

Usage

Prerequisites

Obtain 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.

Output both intent and function call information

To return both intent and function call information, set the system message as follows:
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:
{Tool information}
Response in INTENT_MODE.
Include Response in INTENT_MODE. in the system message and specify available tools. The tool information format is as follows:
[{
    "name": "Name of tool 1",
    "description": "Description of tool 1",
    "parameters": {
        "type": "The type of the parameter, typically object",
        "properties": {
            "parameter_1": {
                "description": "Description of parameter_1",
                "type": "Type of parameter_1",
                "default": "Default value of parameter_1"
            },
            ...
            "parameter_n": {
                "description": "Description of parameter_n",
                "type": "Type of parameter_n",
                "default": "Default value of parameter_n"
            }
        },
        "required": [
        "parameter_1",
        ...
        "parameter_n"
    ]
    },
},
...
{
    "name": "Name of tool n",
    "description": "Description of tool n",
    "parameters": {
        "type": "The type of the parameter, typically object",
        "properties": {
            "parameter_1": {
                "description": "Description of parameter_1",
                "type": "Type of parameter_1",
                "default": "Default value of parameter_1"
            },
            ...
            "parameter_n": {
                "description": "Description of parameter_n",
                "type": "Type of parameter_n",
                "default": "Default value of parameter_n"
            }
        },
        "required": [
        "parameter_1",
        ...
        "parameter_n"
    ]
    },
}]
Example with two tools (time query and weather query):
[
    {
        "name": "get_current_time",
        "description": "This is useful when you want to know the current time.",
        "parameters": {}
    },
    {
        "name": "get_current_weather",
        "description": "This is useful when you want to query the weather of a specified city.",
        "parameters": {
            "type": "object",
            "properties": {
                "location": {
                    "type": "string",
                    "description": "A city or district, such as Beijing, Hangzhou, or Yuhang District."
                }
            },
            "required": ["location"]
        }
    }
]

Sample request

import os
import json
from openai import OpenAI

# Define tools
tools = [
    {
        "name": "get_current_time",
        "description": "This is useful when you want to know the current time.",
        "parameters": {}
    },
    {
        "name": "get_current_weather",
        "description": "This is useful when you want to query the weather of a specified city.",
        "parameters": {
            "type": "object",
            "properties": {
                "location": {
                    "type": "string",
                    "description": "A city or district, such as Beijing, Hangzhou, or Yuhang District.",
                }
            },
            "required": ["location"]
        }
    }
]

tools_string = json.dumps(tools,ensure_ascii=False)

system_prompt = f"""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:
{tools_string}
Response in INTENT_MODE."""
client = OpenAI(
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    base_url="https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1",
)
messages = [
    {'role': 'system', 'content': system_prompt},
    {'role': 'user', 'content': "Weather in Hangzhou"}
    ]
response = client.chat.completions.create(
    model="tongyi-intent-detect-v3",
    messages=messages
)

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

Sample response

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

</content>
Parse the response using the parse_text function:
import re

def parse_text(text):
    # Define regular expression patterns to match <tags>, <tool_call>, <content>, and their content
    tags_pattern = r'<tags>(.*?)</tags>'
    tool_call_pattern = r'<tool_call>(.*?)</tool_call>'
    content_pattern = r'<content>(.*?)</content>'
    # Use regular expressions to find matching 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)
    # Extract matched content (returns empty string if no match)
    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 ""
    # Store the extracted content in a dictionary
    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": "Hangzhou"}}]
</tool_call><content>

</content>"""
print(parse_text(response))
The output is as follows:
{
    "tags": "[function call, json response]",
    "tool_call": [
        {
            "name": "get_current_weather",
            "arguments": {
                "location": "Hangzhou"
            }
        }
    ],
    "content": ""
}

Output only intent information

To return only intent information, set the system message as follows:
You are Qwen, created by Alibaba Cloud. You are a helpful assistant. \nYou should choose one tag from the tag list:\n{intent information}\njust reply with the chosen tag.
The intent information format is as follows:
{
    "Intent 1": "Description of Intent 1",
    "Intent 2": "Description of Intent 2",
    "Intent 3": "Description of Intent 3",
    ...
}

Sample request

import os
import json
from openai import OpenAI

intent_dict = {
    "play_game": "Play game",
    "email_querycontact": "Email query contact",
    "general_quirky": "quirky",
    "email_addcontact": "Email add contact",
    "takeaway_query": "Takeaway query",
    "recommendation_locations": "Location recommendation",
    "transport_traffic": "Transportation",
    "iot_cleaning": "IoT - vacuum cleaner, cleaner",
    "general_joke": "Joke",
    "lists_query": "Query list/checklist",
    "calendar_remove": "Calendar delete event",
    "transport_taxi": "Taxi, taxi booking",
    "qa_factoid": "Factual Q&A",
    "transport_ticket": "Transportation ticket",
    "play_radio": "Play radio",
    "alarm_set": "Set alarm",
}

intent_string = json.dumps(intent_dict,ensure_ascii=False)

system_prompt = f"""You are Qwen, created by Alibaba Cloud. You are a helpful assistant.
You should choose one tag from the tag list:
{intent_string}
Just reply with the chosen tag."""

client = OpenAI(
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    base_url="https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1",
)
messages = [
    {'role': 'system', 'content': system_prompt},
    {'role': 'user', 'content': "Wake me up at nine on Friday morning"}
    ]
response = client.chat.completions.create(
    model="tongyi-intent-detect-v3",
    messages=messages
)

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

Sample response

alarm_set

Improve intent recognition response time

To improve response time, use single uppercase letters for intent categories. This produces single-token responses, optimizing model call latency.
import os
import json
from openai import OpenAI

intent_dict = {
    "A": "Play game",
    "B": "Email query contact",
    "C": "quirky",
    "D": "Email add contact",
    "E": "Takeaway query",
    "F": "Location recommendation",
    "G": "Transportation",
    "H": "IoT - vacuum cleaner, cleaner",
    "I": "Joke",
    "J": "Query list/checklist",
    "K": "Calendar delete event",
    "L": "Taxi, taxi booking",
    "M": "Factual Q&A",
    "N": "Transportation ticket",
    "O": "Play radio",
    "P": "Set alarm",
}

intent_string = json.dumps(intent_dict, ensure_ascii=False)

system_prompt = f"""You are Qwen, created by Alibaba Cloud. You are a helpful assistant.
You should choose one tag from the tag list:
{intent_string}
Just reply with the chosen tag."""

client = OpenAI(
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    base_url="https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1",
)
messages = [
    {"role": "system", "content": system_prompt},
    {"role": "user", "content": "What is the earliest flight from Beijing to Hangzhou?"},
]
response = client.chat.completions.create(
    model="tongyi-intent-detect-v3", messages=messages
)

print(response.choices[0].message.content)
Output: a single-token intent result.
M

Output only function call information

To return only function call information, set the system message as follows:
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:\n{Tool information}\nResponse in NORMAL_MODE.
The tool information format is the same as in the Output both intent and function call information section.

Sample request

import os
import json
from openai import OpenAI

# Define tools
tools = [
    {
        "name": "get_current_time",
        "description": "This is useful when you want to know the current time.",
        "parameters": {}
    },
    {
        "name": "get_current_weather",
        "description": "This is useful when you want to query the weather of a specified city.",
        "parameters": {
            "type": "object",
            "properties": {
                "location": {
                    "type": "string",
                    "description": "A city or district, such as Beijing, Hangzhou, or Yuhang District.",
                }
            },
            "required": ["location"]
        }
    }
]

tools_string = json.dumps(tools,ensure_ascii=False)

system_prompt = f"""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:
{tools_string}
Response in NORMAL_MODE."""
client = OpenAI(
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    base_url="https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1",
)
messages = [
    {'role': 'system', 'content': system_prompt},
    {'role': 'user', 'content': "Weather in Hangzhou"}
    ]
response = client.chat.completions.create(
    model="tongyi-intent-detect-v3",
    messages=messages
)

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

Sample response

<tool_call>
{"name": "get_current_weather", "arguments": {"location": "Hangzhou"}}
</tool_call>
After receiving the response, use the parse_text function to parse the returned tool and parameter information:
import re

def parse_text(text):
    tool_call_pattern = r'<tool_call>(.*?)</tool_call>'
    # Use regular expressions to find matching content
    tool_call_match = re.search(tool_call_pattern, text, re.DOTALL)
    # Extract matched content (returns empty string if no match)
    tool_call = tool_call_match.group(1).strip() if tool_call_match else ""
    return tool_call

response = """<tool_call>
{"name": "get_current_weather", "arguments": {"location": "Hangzhou"}}
</tool_call>"""
print(parse_text(response))
The output is as follows:
{"name": "get_current_weather", "arguments": {"location": "Hangzhou"}}

Multi-round conversations

If a query lacks sufficient information, the model asks follow-up questions. After collecting necessary parameters, it returns the function call information.
  • Output both intent and function call information
  • Output only function call information
Sample request
import os
import json
from openai import OpenAI

# Define tools
tools = [
    {
        "name": "get_current_time",
        "description": "This is useful when you want to know the current time.",
        "parameters": {},
    },
    {
        "name": "get_current_weather",
        "description": "This is useful when you want to query the weather of a specified city.",
        "parameters": {
            "type": "object",
            "properties": {
                "location": {
                    "type": "string",
                    "description": "A city or district, such as Beijing, Hangzhou, or Yuhang District.",
                }
            },
            "required": ["location"],
        },
    },
]

tools_string = json.dumps(tools, ensure_ascii=False)

system_prompt = f"""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:
{tools_string}
Response in INTENT_MODE."""
client = OpenAI(
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    base_url="https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1",
)
messages = [
    {"role": "system", "content": system_prompt},
    # First round question
    {"role": "user", "content": "I want to check the weather"},
]
response = client.chat.completions.create(
    model="tongyi-intent-detect-v3", messages=messages
)

print("Query: I want to check the weather")
print("First-round output:\n")
print(response.choices[0].message.content)
messages.append(response.choices[0].message)
# Second round question
messages.append({"role": "user", "content": "In Hangzhou"})
response = client.chat.completions.create(
    model="tongyi-intent-detect-v3", messages=messages
)
print("\nQuery: In Hangzhou")
print("Second-round output:\n")
print(response.choices[0].message.content)
Sample response
Query: I want to check the weather
First-round output:

<tags>
[weather inquiry]
</tags><tool_call>
[]
</tool_call><content>
OK. Which city's weather would you like to check?
</content>

Query: Hangzhou
Second-round output:

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

</content>

FAQ

What is the maximum number of tools that can be passed in?

Provide no more than 10 tools. Exceeding this limit may reduce tool call accuracy.