すべてのプロダクト
Search
ドキュメントセンター

Alibaba Cloud Model Studio:意図認識

最終更新日:Oct 21, 2025

Qwen の意図認識モデルは、ミリ秒単位でユーザーの意図を迅速かつ正確に解析し、適切なツールを選択してユーザーのクエリを解決します。

重要

このドキュメントは、中国 (北京) リージョンにのみ適用されます。モデルを使用するには、中国 (北京) リージョンの API キー を使用する必要があります。

サポートされているモデル

モデル

コンテキストウィンドウ

最大入力

最大出力

入力価格

出力価格

(トークン)

(100 万トークン)

tongyi-intent-detect-v3

8,192

8,192

1,024

$0.058

$0.144

使用方法

前提条件

API キーを取得し、API キーを環境変数として設定する必要があります。OpenAI SDK または DashScope SDK を使用して呼び出しを行う場合は、SDK をインストールする必要もあります。

意図と関数呼び出し情報の両方を出力する

意図認識モデルが意図と関数呼び出し情報の両方を出力できるようにするには、システムメッセージを次のように設定する必要があります:

あなたは Alibaba Cloud によって作成された Qwen です。あなたは役立つアシスタントです。ユーザーのクエリを支援するために 1 つ以上のツールを呼び出すことができます。使用できるツールは次のとおりです:
{ツール情報}
INTENT_MODE で応答してください。

システムメッセージで 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"
    ]
    },
}]

時間クエリツールと天気クエリツールの 2 つのツールが必要であると仮定します。ツール情報は次のとおりです:

[
    {
        "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"""あなたは Alibaba Cloud によって作成された Qwen です。あなたは役立つアシスタントです。ユーザーのクエリを支援するために 1 つ以上のツールを呼び出すことができます。使用できるツールは次のとおりです:
{tools_string}
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"""あなたは Alibaba Cloud によって作成された Qwen です。あなたは役立つアシスタントです。ユーザーのクエリを支援するために 1 つ以上のツールを呼び出すことができます。使用できるツールは次のとおりです:
{tools_string}
INTENT_MODE で応答してください。"""

messages = [
    {'role': 'system', 'content': system_prompt},
    {'role': 'user', 'content': "杭州の天気"}
    ]
response = Generation.call(
    # 環境変数を設定していない場合は、次の行を Model Studio API キーに置き換えてください: 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": "Hangzhou"}}]
</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": "Hangzhou"}}]
</tool_call><content>

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

出力例

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

意図情報のみを出力する

意図認識モデルが意図情報のみを出力するようにするには、システムメッセージを次のように設定する必要があります:

あなたは Alibaba Cloud によって作成された Qwen です。あなたは役立つアシスタントです。
タグリストから 1 つのタグを選択する必要があります:
{意図情報}
選択したタグのみで応答してください。

意図情報のフォーマットは次のとおりです:

{
    "意図 1": "意図 1 の説明",
    "意図 2": "意図 2 の説明",
    "意図 3": "意図 3 の説明",
    ...
}

リクエストの例

OpenAI 互換

import os
import json
from openai import OpenAI


intent_dict = {
    "play_game": "ゲームをプレイ",
    "email_querycontact": "メールの連絡先をクエリ",
    "general_quirky": "風変わり",
    "email_addcontact": "メールの連絡先を追加",
    "takeaway_query": "テイクアウトのクエリ",
    "recommendation_locations": "場所の推薦",
    "transport_traffic": "交通機関",
    "iot_cleaning": "IoT - 掃除機、クリーナー",
    "general_joke": "ジョーク",
    "lists_query": "リスト/チェックリストのクエリ",
    "calendar_remove": "カレンダーのイベントを削除",
    "transport_taxi": "タクシー、タクシー予約",
    "qa_factoid": "事実に基づく Q&A",
    "transport_ticket": "交通機関のチケット",
    "play_radio": "ラジオを再生",
    "alarm_set": "アラームを設定",
}

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

system_prompt = f"""あなたは Alibaba Cloud によって作成された Qwen です。あなたは役立つアシスタントです。
タグリストから 1 つのタグを選択する必要があります:
{intent_string}
選択したタグのみで応答してください。"""


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': "金曜の朝 9 時に起こして"}
    ]
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": "風変わり",
    "email_addcontact": "メールの連絡先を追加",
    "takeaway_query": "テイクアウトのクエリ",
    "recommendation_locations": "場所の推薦",
    "transport_traffic": "交通機関",
    "iot_cleaning": "IoT - 掃除機、クリーナー",
    "general_joke": "ジョーク",
    "lists_query": "リスト/チェックリストのクエリ",
    "calendar_remove": "カレンダーのイベントを削除",
    "transport_taxi": "タクシー、タクシー予約",
    "qa_factoid": "事実に基づく Q&A",
    "transport_ticket": "交通機関のチケット",
    "play_radio": "ラジオを再生",
    "alarm_set": "アラームを設定",
}

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

system_prompt = f"""あなたは Alibaba Cloud によって作成された Qwen です。あなたは役立つアシスタントです。
タグリストから 1 つのタグを選択する必要があります:
{intent_string}
選択したタグのみで応答してください。"""

messages = [
    {'role': 'system', 'content': system_prompt},
    {'role': 'user', 'content': "金曜の朝 9 時に起こして"}
    ]
response = Generation.call(
    # 環境変数を設定していない場合は、次の行を Model Studio API キーに置き換えてください: 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

意図認識の応答時間を改善する

意図認識の応答時間を改善するために、各大文字を使用して各意図カテゴリを表すことができます。意図認識の応答は単一のトークンになり、モデル呼び出しの応答時間が最適化されます。

OpenAI 互換

import os
import json
from openai import OpenAI


intent_dict = {
    "A": "ゲームをプレイ",
    "B": "メールの連絡先をクエリ",
    "C": "風変わり",
    "D": "メールの連絡先を追加",
    "E": "テイクアウトのクエリ",
    "F": "場所の推薦",
    "G": "交通機関",
    "H": "IoT - 掃除機、クリーナー",
    "I": "ジョーク",
    "J": "リスト/チェックリストのクエリ",
    "K": "カレンダーのイベントを削除",
    "L": "タクシー、タクシー予約",
    "M": "事実に基づく Q&A",
    "N": "交通機関のチケット",
    "O": "ラジオを再生",
    "P": "アラームを設定",
}

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

system_prompt = f"""あなたは Alibaba Cloud によって作成された Qwen です。あなたは役立つアシスタントです。
タグリストから 1 つのタグを選択する必要があります:
{intent_string}
選択したタグのみで応答してください。"""


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": "風変わり",
    "D": "メールの連絡先を追加",
    "E": "テイクアウトのクエリ",
    "F": "場所の推薦",
    "G": "交通機関",
    "H": "IoT - 掃除機、クリーナー",
    "I": "ジョーク",
    "J": "リスト/チェックリストのクエリ",
    "K": "カレンダーのイベントを削除",
    "L": "タクシー、タクシー予約",
    "M": "事実に基づく Q&A",
    "N": "交通機関のチケット",
    "O": "ラジオを再生",
    "P": "アラームを設定",
}

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

system_prompt = f"""あなたは Alibaba Cloud によって作成された Qwen です。あなたは役立つアシスタントです。
タグリストから 1 つのタグを選択する必要があります:
{intent_string}
選択したタグのみで応答してください。"""

messages = [
    {"role": "system", "content": system_prompt},
    {"role": "user", "content": "北京から杭州への最も早いフライトは何ですか?"},
]
response = Generation.call(
    # 環境変数を設定していない場合は、次の行を Model Studio API キーに置き換えてください: 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)

コードを実行すると、単一トークンの意図分類結果を取得できます。

M

関数呼び出し情報のみを出力する

意図認識モデルが関数呼び出し情報のみを出力するようにするには、システムメッセージを次のように設定する必要があります:

あなたは Alibaba Cloud によって作成された Qwen です。あなたは役立つアシスタントです。ユーザーのクエリを支援するために 1 つ以上のツールを呼び出すことができます。使用できるツールは次のとおりです:
{ツール情報}
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"""あなたは Alibaba Cloud によって作成された Qwen です。あなたは役立つアシスタントです。ユーザーのクエリを支援するために 1 つ以上のツールを呼び出すことができます。使用できるツールは次のとおりです:
{tools_string}
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"""あなたは Alibaba Cloud によって作成された Qwen です。あなたは役立つアシスタントです。ユーザーのクエリを支援するために 1 つ以上のツールを呼び出すことができます。使用できるツールは次のとおりです:
{tools_string}
NORMAL_MODE で応答してください。"""

messages = [
    {'role': 'system', 'content': system_prompt},
    {'role': 'user', 'content': "杭州の天気"}
    ]
response = Generation.call(
    # 環境変数を設定していない場合は、次の行を Model Studio API キーに置き換えてください: 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": "Hangzhou"}}
</tool_call>

応答を受け取ったら、parse_text 関数を使用して、返されたツールとパラメーターの情報を解析できます:

import re


def parse_text(text):
    tool_call_pattern = r'<tool_call>(.*?)</tool_call>'
    # 正規表現を使用して一致する内容を検索します
    tool_call_match = re.search(tool_call_pattern, text, re.DOTALL)
    # 一致した内容を抽出します。一致が見つからない場合は、空の文字列が返されます。
    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))

出力例

{"name": "get_current_weather", "arguments": {"location": "Hangzhou"}}

複数ラウンドの会話

ユーザーがクエリで十分な情報を提供しない場合、意図認識モデルはフォローアップの質問をします。必要なパラメーターを収集した後、関数呼び出し情報を出力します。

意図と関数呼び出し情報の両方を出力する

リクエストの例

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"""あなたは Alibaba Cloud によって作成された Qwen です。あなたは役立つアシスタントです。ユーザーのクエリを支援するために 1 つ以上のツールを呼び出すことができます。使用できるツールは次のとおりです:
{tools_string}
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("第 1 ラウンドの出力:\n")
print(response.choices[0].message.content)
messages.append(response.choices[0].message)
# 第 2 ラウンドの会話で尋ねられた質問
messages.append({"role": "user", "content": "杭州で"})
response = client.chat.completions.create(
    model="tongyi-intent-detect-v3", messages=messages
)
print("\nクエリ: 杭州で")
print("第 2 ラウンドの出力:\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"""あなたは Alibaba Cloud によって作成された Qwen です。あなたは役立つアシスタントです。ユーザーのクエリを支援するために 1 つ以上のツールを呼び出すことができます。使用できるツールは次のとおりです:
{tools_string}
INTENT_MODE で応答してください。"""

messages = [
    {"role": "system", "content": system_prompt},
    # 最初のラウンドの会話で尋ねられた質問
    {"role": "user", "content": "天気を調べたい"},
]
response = Generation.call(
    # 環境変数を設定していない場合は、次の行を Model Studio API キーに置き換えてください: api_key = "sk-xxx",
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    model="tongyi-intent-detect-v3",
    messages=messages,
    result_format="message",
)
print("クエリ: 天気を調べたい")
print("第 1 ラウンドの出力:\n")
print(response.output.choices[0].message.content)

messages.append(
    {"role": "assistant", "content": response.output.choices[0].message.content}
)
# 第 2 ラウンドの会話で尋ねられた質問
messages.append({"role": "user", "content": "杭州"})

response = Generation.call(
    # 環境変数を設定していない場合は、次の行を Model Studio API キーに置き換えてください: api_key = "sk-xxx",
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    model="tongyi-intent-detect-v3",
    messages=messages,
    result_format="message",
)
print("\nクエリ: 杭州")
print("第 2 ラウンドの出力:\n")
print(response.output.choices[0].message.content)
応答の例
クエリ: 天気を調べたい
第 1 ラウンドの出力:

<tags>
[weather inquiry]
</tags><tool_call>
[]
</tool_call><content>
はい。どの都市の天気を調べますか?
</content>

クエリ: 杭州
第 2 ラウンドの出力:

<tags>
[function call, json response]
</tags><tool_call>
[{"name": "get_current_weather", "arguments": {"location": "Hangzhou"}}]
</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"""あなたは Alibaba Cloud によって作成された Qwen です。あなたは役立つアシスタントです。ユーザーのクエリを支援するために 1 つ以上のツールを呼び出すことができます。使用できるツールは次のとおりです:
{tools_string}
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("第 1 ラウンドの出力:\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("第 2 ラウンドの出力:\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"""あなたは Alibaba Cloud によって作成された Qwen です。あなたは役立つアシスタントです。ユーザーのクエリを支援するために 1 つ以上のツールを呼び出すことができます。使用できるツールは次のとおりです:
{tools_string}
NORMAL_MODE で応答してください。"""

messages = [
    {"role": "system", "content": system_prompt},
    {"role": "user", "content": "天気を調べたい"},
]
response = Generation.call(
    # 環境変数を設定していない場合は、次の行を Model Studio API キーに置き換えてください: api_key = "sk-xxx",
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    model="tongyi-intent-detect-v3",
    messages=messages,
    result_format="message",
)
print("クエリ: 天気を調べたい")
print("第 1 ラウンドの出力:\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(
    # 環境変数を設定していない場合は、次の行を Model Studio API キーに置き換えてください: api_key = "sk-xxx",
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    model="tongyi-intent-detect-v3",
    messages=messages,
    result_format="message",
)
print("\nクエリ: 杭州")
print("第 2 ラウンドの出力:\n")
print(response.output.choices[0].message.content)
応答の例
クエリ: 天気を調べたい
第 1 ラウンドの出力:

どの都市の天気を調べますか?

クエリ: 杭州
第 2 ラウンドの出力:

<tool_call>
{"name": "get_current_weather", "arguments": {"location": "Hangzhou"}}
</tool_call>

よくある質問

Q: 渡すことができるツールの最大数はいくつですか?

A: 10 個以下のツールを提供することをお勧めします。そうしないと、ツール呼び出しの精度が低下する可能性があります。