通義千問的意圖理解模型能夠在百毫秒級時間內快速、準確地解析使用者意圖,並選擇合適的工具來解決使用者的問題。
本文檔僅適用於“中國大陸(北京)”地區。如需使用模型,需使用“中國大陸(北京)”地區的API Key。
支援的模型
模型名稱 | 上下文長度 | 最大輸入 | 最大輸出 | 輸入成本 | 輸出成本 |
(Token數) | (每百萬Token) | ||||
tongyi-intent-detect-v3 | 8,192 | 8,192 | 1,024 | $0.058 | $0.144 |
使用方法
前提條件
您需要已準備工作:擷取與配置 API Key並配置API Key到環境變數(準備下線,併入配置 API Key)。如果通過OpenAI SDK或DashScope 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"
]
},
}]假設您的業務情境需要使用時間查詢與天氣查詢兩個工具,工具資訊為:
[
{
"name": "get_current_time",
"description": "當你想知道現在的時間時非常有用。",
"parameters": {}
},
{
"name": "get_current_weather",
"description": "當你想查詢指定城市的天氣時非常有用。",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "城市或縣區,比如北京市、杭州市、餘杭區等。",
}
},
"required": ["location"]
}
}
]請求樣本
OpenAI相容
import os
import json
from openai import OpenAI
# 定義工具
tools = [
{
"name": "get_current_time",
"description": "當你想知道現在的時間時非常有用。",
"parameters": {}
},
{
"name": "get_current_weather",
"description": "當你想查詢指定城市的天氣時非常有用。",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "城市或縣區,比如北京市、杭州市、餘杭區等。",
}
},
"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://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
import json
from dashscope import Generation
# 定義工具
tools = [
{
"name": "get_current_time",
"description": "當你想知道現在的時間時非常有用。",
"parameters": {}
},
{
"name": "get_current_weather",
"description": "當你想查詢指定城市的天氣時非常有用。",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "城市或縣區,比如北京市、杭州市、餘杭區等。",
}
},
"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."""
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):
# 定義Regex模式來匹配 <tags>, <tool_call>, <content> 及其內容
tags_pattern = r'<tags>(.*?)</tags>'
tool_call_pattern = r'<tool_call>(.*?)</tool_call>'
content_pattern = r'<content>(.*?)</content>'
# 使用Regex尋找匹配的內容
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)
# 提取匹配的內容,如果沒有匹配到則返回Null 字元串
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": ""
}只輸出意圖資訊
為了使意圖理解模型只輸出意圖資訊,您需要按照以下方式設定System Message:
You are Qwen, created by Alibaba Cloud. You are a helpful assistant. \nYou should choose one tag from the tag list:\n{意圖資訊}\njust reply with the chosen tag.意圖資訊的格式為:
{
"意圖1": "意圖1的描述",
"意圖2": "意圖2的描述",
"意圖3": "意圖3的描述",
...
}請求樣本
OpenAI相容
import os
import json
from openai import OpenAI
intent_dict = {
"play_game": "玩遊戲",
"email_querycontact": "電子郵件查詢連絡人",
"general_quirky": "quirky",
"email_addcontact": "電子郵件新增連絡人...",
"takeaway_query": "外賣查詢",
"recommendation_locations": "地點推薦",
"transport_traffic": "交通運輸",
"iot_cleaning": "物聯網-吸塵器, 清潔器",
"general_joke": "笑話",
"lists_query": "查詢列表/清單",
"calendar_remove": "日曆刪除事件",
"transport_taxi": "打車, 出租車預約",
"qa_factoid": "事實性問答",
"transport_ticket": "交通票據",
"play_radio": "播放廣播",
"alarm_set": "設定鬧鐘",
}
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://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
import json
from dashscope import Generation
intent_dict = {
"play_game": "玩遊戲",
"email_querycontact": "電子郵件查詢連絡人",
"general_quirky": "quirky",
"email_addcontact": "電子郵件新增連絡人...",
"takeaway_query": "外賣查詢",
"recommendation_locations": "地點推薦",
"transport_traffic": "交通運輸",
"iot_cleaning": "物聯網-吸塵器, 清潔器",
"general_joke": "笑話",
"lists_query": "查詢列表/清單",
"calendar_remove": "日曆刪除事件",
"transport_taxi": "打車, 出租車預約",
"qa_factoid": "事實性問答",
"transport_ticket": "交通票據",
"play_radio": "播放廣播",
"alarm_set": "設定鬧鐘",
}
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."""
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)響應樣本
alarm_set提升意圖識別的響應速度
為了提升意圖識別的響應速度,您可以將意圖的分類種類用一個簡單的大寫字母進行指代,意圖識別響應結果將始終為一個 Token,這可以最佳化模型調用的回應時間。
OpenAI相容
import os
import json
from openai import OpenAI
intent_dict = {
"A": "玩遊戲",
"B": "電子郵件查詢連絡人",
"C": "quirky",
"D": "電子郵件新增連絡人...",
"E": "外賣查詢",
"F": "地點推薦",
"G": "交通運輸",
"H": "物聯網-吸塵器, 清潔器",
"I": "笑話",
"J": "查詢列表/清單",
"K": "日曆刪除事件",
"L": "打車, 出租車預約",
"M": "事實性問答",
"N": "交通票據",
"O": "播放廣播",
"P": "設定鬧鐘",
}
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://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
import json
from dashscope import Generation
intent_dict = {
"A": "玩遊戲",
"B": "電子郵件查詢連絡人",
"C": "quirky",
"D": "電子郵件新增連絡人...",
"E": "外賣查詢",
"F": "地點推薦",
"G": "交通運輸",
"H": "物聯網-吸塵器, 清潔器",
"I": "笑話",
"J": "查詢列表/清單",
"K": "日曆刪除事件",
"L": "打車, 出租車預約",
"M": "事實性問答",
"N": "交通票據",
"O": "播放廣播",
"P": "設定鬧鐘",
}
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."""
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)運行代碼後可以得到一個 Token 的意圖分類結果。
M只輸出函數調用資訊
為了使意圖理解模型只輸出函數調用資訊,您需要按照以下方式設定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:\n{工具資訊}\nResponse in NORMAL_MODE.其中工具資訊與同時輸出意圖與函數調用資訊中的工具資訊格式相同。
請求樣本
OpenAI相容
import os
import json
from openai import OpenAI
# 定義工具
tools = [
{
"name": "get_current_time",
"description": "當你想知道現在的時間時非常有用。",
"parameters": {}
},
{
"name": "get_current_weather",
"description": "當你想查詢指定城市的天氣時非常有用。",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "城市或縣區,比如北京市、杭州市、餘杭區等。",
}
},
"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://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
import json
from dashscope import Generation
# 定義工具
tools = [
{
"name": "get_current_time",
"description": "當你想知道現在的時間時非常有用。",
"parameters": {}
},
{
"name": "get_current_weather",
"description": "當你想查詢指定城市的天氣時非常有用。",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "城市或縣區,比如北京市、杭州市、餘杭區等。",
}
},
"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."""
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)響應樣本
<tool_call>
{"name": "get_current_weather", "arguments": {"location": "杭州市"}}
</tool_call>在得到響應後,您需要使用parse_text函數解析出返回的工具與參數資訊:
import re
def parse_text(text):
tool_call_pattern = r'<tool_call>(.*?)</tool_call>'
# 使用Regex尋找匹配的內容
tool_call_match = re.search(tool_call_pattern, text, re.DOTALL)
# 提取匹配的內容,如果沒有匹配到則返回Null 字元串
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": "杭州市"}}
</tool_call>"""
print(parse_text(response))得到輸出為:
{"name": "get_current_weather", "arguments": {"location": "杭州市"}}多輪對話
如果使用者在提問時未提供充足的資訊,意圖理解模型會進行反問,通過多輪對話採集到必要的參數後,再輸出函數調用的資訊。
同時輸出意圖與函數調用資訊
請求樣本
OpenAI 相容
import os
import json
from openai import OpenAI
# 定義工具
tools = [
{
"name": "get_current_time",
"description": "當你想知道現在的時間時非常有用。",
"parameters": {},
},
{
"name": "get_current_weather",
"description": "當你想查詢指定城市的天氣時非常有用。",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "城市或縣區,比如北京市、杭州市、餘杭區等。",
}
},
"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://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("查詢問題:我想查天氣")
print("第一輪輸出:\n")
print(response.choices[0].message.content)
messages.append(response.choices[0].message)
# 第二輪對話提出的問題
messages.append({"role": "user", "content": "杭州的"})
response = client.chat.completions.create(
model="tongyi-intent-detect-v3", messages=messages
)
print("\n查詢問題:杭州的")
print("第二輪輸出:\n")
print(response.choices[0].message.content)DashScope
import os
import json
from dashscope import Generation
# 定義工具
tools = [
{
"name": "get_current_time",
"description": "當你想知道現在的時間時非常有用。",
"parameters": {},
},
{
"name": "get_current_weather",
"description": "當你想查詢指定城市的天氣時非常有用。",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "城市或縣區,比如北京市、杭州市、餘杭區等。",
}
},
"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."""
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("查詢問題:我想查天氣")
print("第一輪輸出:\n")
print(response.output.choices[0].message.content)
messages.append(
{"role": "assistant", "content": response.output.choices[0].message.content}
)
# 第二輪對話提出的問題
messages.append({"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("\n查詢問題:杭州")
print("第二輪輸出:\n")
print(response.output.choices[0].message.content)響應樣本
查詢問題:我想查天氣
第一輪輸出:
<tags>
[weather inquiry]
</tags><tool_call>
[]
</tool_call><content>
好的,請問您想查詢哪個城市的天氣呢?
</content>
查詢問題:杭州
第二輪輸出:
<tags>
[function call, json response]
</tags><tool_call>
[{"name": "get_current_weather", "arguments": {"location": "杭州"}}]
</tool_call><content>
</content>只輸出函數調用資訊
請求樣本
OpenAI相容
import os
import json
from openai import OpenAI
# 定義工具
tools = [
{
"name": "get_current_time",
"description": "當你想知道現在的時間時非常有用。",
"parameters": {},
},
{
"name": "get_current_weather",
"description": "當你想查詢指定城市的天氣時非常有用。",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "城市或縣區,比如北京市、杭州市、餘杭區等。",
}
},
"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://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
)
messages.append(response.choices[0].message)
print("查詢問題:我想查天氣")
print("第一輪輸出:\n")
print(response.choices[0].message.content)
messages.append({"role": "user", "content": "杭州"})
response = client.chat.completions.create(
model="tongyi-intent-detect-v3", messages=messages
)
print("\n查詢問題:杭州")
print("第二輪輸出:\n")
print(response.choices[0].message.content)DashScope
import os
import json
from dashscope import Generation
# 定義工具
tools = [
{
"name": "get_current_time",
"description": "當你想知道現在的時間時非常有用。",
"parameters": {},
},
{
"name": "get_current_weather",
"description": "當你想查詢指定城市的天氣時非常有用。",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "城市或縣區,比如北京市、杭州市、餘杭區等。",
}
},
"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."""
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("查詢問題:我想查天氣")
print("第一輪輸出:\n")
print(response.output.choices[0].message.content)
messages.append(
{"role": "assistant", "content": response.output.choices[0].message.content}
)
messages.append({"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("\n查詢問題:杭州")
print("第二輪輸出:\n")
print(response.output.choices[0].message.content)響應樣本
查詢問題:我想查天氣
第一輪輸出:
請問您想查詢哪個城市的天氣呢?
查詢問題:杭州
第二輪輸出:
<tool_call>
{"name": "get_current_weather", "arguments": {"location": "杭州"}}
</tool_call>常見問題
Q:最多傳入幾個工具?
A:我們建議您傳入不超過10個的工具,否則模型調用工具的準確率可能會降低。