Model Studio は、Qwen モデル向けの OpenAI 互換インターフェイスを提供します。OpenAI から移行するには、API キー、BASE_URL、およびモデル名を更新してください。
OpenAI 互換性
BASE_URL
BASE_URL を設定して、OpenAI 互換インターフェイス経由で Model Studio に接続します。BASE_URL は、モデルサービスのネットワークエンドポイントです。
OpenAI SDK またはその他の OpenAI 互換 SDK を使用する場合、
BASE_URLを次のように設定します。シンガポール: https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1 米国 (バージニア): https://dashscope-us.aliyuncs.com/compatible-mode/v1 中国 (北京): https://dashscope.aliyuncs.com/compatible-mode/v1 中国 (香港): https://{WorkspaceId}.cn-hongkong.maas.aliyuncs.com/compatible-mode/v1HTTP リクエストを行う場合、フルエンドポイントを次のように設定します。
シンガポール: POST https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1/chat/completions 米国 (バージニア): POST https://dashscope-us.aliyuncs.com/compatible-mode/v1/chat/completions 中国 (北京): POST https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions 中国 (香港): POST https://{WorkspaceId}.cn-hongkong.maas.aliyuncs.com/compatible-mode/v1/chat/completions
レガシのシンガポールドメイン https://dashscope-intl.aliyuncs.com および中国 (香港) ドメイン https://cn-hongkong.dashscope.aliyuncs.com は廃止予定です。できるだけ早く https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com (シンガポール) および https://{WorkspaceId}.cn-hongkong.maas.aliyuncs.com (中国 (香港)) に移行してください。
サポートされるモデル
サポートされるモデルには、Qwen 大規模言語モデル (商用およびオープンソース)、Qwen-VL、Qwen-Coder、Qwen-Omni、Qwen-Math、DeepSeek、Kimi、GLM、MiniMax が含まれます。
OpenAI SDK 経由での Qwen モデル呼び出し
前提条件
Python をインストールします。
最新の OpenAI SDK をインストールします。
# 次のコマンドが失敗する場合は、pip を pip3 に置き換えてください。 pip install -U openaiModel Studio を有効化し、API キーを取得します。「API キーの取得」をご参照ください。
API キーを環境変数として設定し、漏洩リスクを低減します。「API キーを環境変数として設定する」をご参照ください。コード内に直接設定することも可能ですが、これにより漏洩リスクが高まります。
サポートされるモデルの一覧からモデルを選択します。
使用方法
非ストリーミング呼び出しの例
from openai import OpenAI
import os
def get_response():
client = OpenAI(
# API キーはリージョンごとに異なります。API キーの取得方法については、https://www.alibabacloud.com/help/ja/model-studio/get-api-key をご参照ください。
api_key=os.getenv("DASHSCOPE_API_KEY"), # 環境変数を設定していない場合は、この行を Model Studio API キーに置き換えてください: api_key="sk-xxx"
# 以下はシンガポールリージョンの base_url です。
base_url="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1",
)
completion = client.chat.completions.create(
model="qwen-plus", # この例では qwen-plus を使用しています。必要に応じてモデル名を変更できます。モデルの一覧については、https://www.alibabacloud.com/help/ja/model-studio/getting-started/models をご参照ください。
messages=[{'role': 'system', 'content': 'You are a helpful assistant.'},
{'role': 'user', 'content': 'Who are you?'}]
)
print(completion.model_dump_json())
if __name__ == '__main__':
get_response()出力:
{
"id": "chatcmpl-xxx",
"choices": [
{
"finish_reason": "stop",
"index": 0,
"logprobs": null,
"message": {
"content": "I am a large-scale pre-trained model from Alibaba Cloud. My name is Qwen.",
"role": "assistant",
"function_call": null,
"tool_calls": null
}
}
],
"created": 1716430652,
"model": "qwen-plus",
"object": "chat.completion",
"system_fingerprint": null,
"usage": {
"completion_tokens": 18,
"prompt_tokens": 22,
"total_tokens": 40
}
}ストリーミング呼び出しの例
from openai import OpenAI
import os
def get_response():
client = OpenAI(
# API キーはリージョンごとに異なります。API キーの取得方法については、https://www.alibabacloud.com/help/ja/model-studio/get-api-key をご参照ください。
# 環境変数を設定していない場合は、次の行を Model Studio API キーに置き換えてください: api_key="sk-xxx"
api_key=os.getenv("DASHSCOPE_API_KEY"),
# 以下はシンガポールリージョンの base_url です。
base_url="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1",
)
completion = client.chat.completions.create(
model="qwen-plus", # この例では qwen-plus を使用しています。必要に応じてモデル名を変更できます。モデルの一覧については、https://www.alibabacloud.com/help/ja/model-studio/getting-started/models をご参照ください。
messages=[{'role': 'system', 'content': 'You are a helpful assistant.'},
{'role': 'user', 'content': 'Who are you?'}],
stream=True,
# 次の設定により、ストリーミング出力の最終行にトークン使用量情報が表示されます。
stream_options={"include_usage": True}
)
for chunk in completion:
print(chunk.model_dump_json())
if __name__ == '__main__':
get_response()
出力:
{"id":"chatcmpl-xxx","choices":[{"delta":{"content":"","function_call":null,"role":"assistant","tool_calls":null},"finish_reason":null,"index":0,"logprobs":null}],"created":1719286190,"model":"qwen-plus","object":"chat.completion.chunk","system_fingerprint":null,"usage":null}
{"id":"chatcmpl-xxx","choices":[{"delta":{"content":"I am","function_call":null,"role":null,"tool_calls":null},"finish_reason":null,"index":0,"logprobs":null}],"created":1719286190,"model":"qwen-plus","object":"chat.completion.chunk","system_fingerprint":null,"usage":null}
{"id":"chatcmpl-xxx","choices":[{"delta":{"content":" a large","function_call":null,"role":null,"tool_calls":null},"finish_reason":null,"index":0,"logprobs":null}],"created":1719286190,"model":"qwen-plus","object":"chat.completion.chunk","system_fingerprint":null,"usage":null}
{"id":"chatcmpl-xxx","choices":[{"delta":{"content":" language model","function_call":null,"role":null,"tool_calls":null},"finish_reason":null,"index":0,"logprobs":null}],"created":1719286190,"model":"qwen-plus","object":"chat.completion.chunk","system_fingerprint":null,"usage":null}
{"id":"chatcmpl-xxx","choices":[{"delta":{"content":" from Alibaba","function_call":null,"role":null,"tool_calls":null},"finish_reason":null,"index":0,"logprobs":null}],"created":1719286190,"model":"qwen-plus","object":"chat.completion.chunk","system_fingerprint":null,"usage":null}
{"id":"chatcmpl-xxx","choices":[{"delta":{"content":" Cloud, and my","function_call":null,"role":null,"tool_calls":null},"finish_reason":null,"index":0,"logprobs":null}],"created":1719286190,"model":"qwen-plus","object":"chat.completion.chunk","system_fingerprint":null,"usage":null}
{"id":"chatcmpl-xxx","choices":[{"delta":{"content":" name is Qwen.","function_call":null,"role":null,"tool_calls":null},"finish_reason":null,"index":0,"logprobs":null}],"created":1719286190,"model":"qwen-plus","object":"chat.completion.chunk","system_fingerprint":null,"usage":null}
{"id":"chatcmpl-xxx","choices":[{"delta":{"content":"","function_call":null,"role":null,"tool_calls":null},"finish_reason":"stop","index":0,"logprobs":null}],"created":1719286190,"model":"qwen-plus","object":"chat.completion.chunk","system_fingerprint":null,"usage":null}
{"id":"chatcmpl-xxx","choices":[],"created":1719286190,"model":"qwen-plus","object":"chat.completion.chunk","system_fingerprint":null,"usage":{"completion_tokens":16,"prompt_tokens":22,"total_tokens":38}}関数呼び出しの例
次のコードは、天気と時刻照会ツールを使用したマルチターンの関数呼び出しを示しています。
from openai import OpenAI
from datetime import datetime
import json
import os
client = OpenAI(
# API キーはリージョンごとに異なります。API キーの取得方法については、https://www.alibabacloud.com/help/ja/model-studio/get-api-key をご参照ください。
# 環境変数を設定していない場合は、次の行を Model Studio API キーに置き換えてください: api_key="sk-xxx",
api_key=os.getenv("DASHSCOPE_API_KEY"),
# 以下はシンガポールリージョンの base_url です。
base_url="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1",
)
# ツールのリストを定義します。モデルは、どのツールを使用するか選択する際に、ツールの名前と説明を参照します。
tools = [
# ツール 1: 現在時刻を取得します。
{
"type": "function",
"function": {
"name": "get_current_time",
"description": "現在時刻を知りたい場合に役立ちます。",
# 現在時刻を取得するために入力パラメーターは不要なため、「parameters」オブジェクトは空です。
"parameters": {}
}
},
# ツール 2: 指定された都市の天気を取得します。
{
"type": "function",
"function": {
"name": "get_current_weather",
"description": "指定された都市の天気を照会したい場合に役立ちます。",
"parameters": {
"type": "object",
"properties": {
# 天気を照会するには場所が必要なため、「location」パラメーターを定義します。
"location": {
"type": "string",
"description": "都市または地区。例: 北京、杭州、余杭。"
}
},
"required": [
"location"
]
}
}
}
]
# 天気照会ツールをシミュレートします。例: 「今日の北京は雨です。」
def get_current_weather(location):
return f"今日の{location}は雨です。 "
# 現在時刻を照会するツール。例: 「現在時刻: 2024-04-15 17:15:18。」
def get_current_time():
# 現在の日時を取得します。
current_datetime = datetime.now()
# 現在の日時をフォーマットします。
formatted_time = current_datetime.strftime('%Y-%m-%d %H:%M:%S')
# フォーマット済みの現在時刻を返します。
return f"現在時刻: {formatted_time}。"
# モデル応答関数を定義します。
def get_response(messages):
completion = client.chat.completions.create(
model="qwen-plus", # この例では qwen-plus を使用しています。必要に応じてモデル名を変更できます。モデルの一覧については、https://www.alibabacloud.com/help/ja/model-studio/getting-started/models をご参照ください。
messages=messages,
tools=tools
)
return completion.model_dump()
def call_with_messages():
print('\n')
messages = [
{
"content": input('入力してください: '), # 例: 「現在時刻は何時ですか?」「1 時間後の時刻は何時ですか?」「北京の天気はどうですか?」
"role": "user"
}
]
print("-"*60)
# モデル呼び出しの最初のターン。
i = 1
first_response = get_response(messages)
assistant_output = first_response['choices'][0]['message']
print(f"\nターン {i} の LLM 出力: {first_response}\n")
if assistant_output['content'] is None:
assistant_output['content'] = ""
messages.append(assistant_output)
# モデルがツール呼び出しが不要と判断した場合、アシスタントの返信を直接出力します。
if assistant_output['tool_calls'] == None:
print(f"ツール呼び出しは不要です。直接返信できます: {assistant_output['content']}")
return
# ツール呼び出しが必要な場合、最終的な回答が生成されるまでモデル呼び出しを続けます。
while assistant_output['tool_calls'] != None:
# モデルが天気照会ツールを呼び出す必要があると判断した場合、天気照会ツールを実行します。
if assistant_output['tool_calls'][0]['function']['name'] == 'get_current_weather':
tool_info = {"name": "get_current_weather", "role":"tool"}
# location パラメーターを抽出します。
location = json.loads(assistant_output['tool_calls'][0]['function']['arguments'])['location']
tool_info['content'] = get_current_weather(location)
# モデルが時刻照会ツールを呼び出す必要があると判断した場合、時刻照会ツールを実行します。
elif assistant_output['tool_calls'][0]['function']['name'] == 'get_current_time':
tool_info = {"name": "get_current_time", "role":"tool"}
tool_info['content'] = get_current_time()
print(f"ツール出力: {tool_info['content']}\n")
print("-"*60)
messages.append(tool_info)
assistant_output = get_response(messages)['choices'][0]['message']
if assistant_output['content'] is None:
assistant_output['content'] = ""
messages.append(assistant_output)
i += 1
print(f"ターン {i} の LLM 出力: {assistant_output}\n")
print(f"最終回答: {assistant_output['content']}")
if __name__ == '__main__':
call_with_messages()杭州と北京の天気はどうですか? 現在時刻は何時ですか? と入力すると、プログラムは次の出力を返します。

パラメーター
OpenAI API と互換性のある入力パラメーター:
パラメーター | タイプ | デフォルト | 説明 |
model | 文字列 | - | 使用するモデル。「サポートされるモデルの一覧」をご参照ください。 |
messages | 配列 | - | ユーザーとモデル間の会話履歴。配列内の各要素は |
top_p (オプション) | 浮動 | - | ヌクレウスサンプリングのしきい値。0.8 の場合、累積確率が 0.8 を超える最小のトークンセットを保持します。範囲: (0, 1.0)。値が大きいほどランダム性が増し、小さいほど決定論的になります。 |
temperature (オプション) | 浮動小数点数 | - | 出力のランダム性を制御します。値が高いほど多様な出力が生成され、低いほど決定論的な出力が生成されます。 範囲: [0, 2)。0 に設定しないでください。 |
presence_penalty (オプション) | 浮動 | - | 生成されたシーケンス内のトークンの繰り返しを制御します。 説明 このパラメーターは、商用 Qwen モデルおよび qwen1.5 以降のオープンソースモデルでのみサポートされています。 |
n (オプション) | 整数 | 1 | 生成する応答の数。値は このパラメーターは現在、 |
max_tokens (オプション) | 整数 | - | モデルが生成できるトークンの最大数。 出力制限はモデルによって異なります。上記のサポートされるモデル一覧をご確認ください。 |
seed (オプション) | 整数 | - | 生成時の乱数シード。 |
stream (オプション) | ブール値 | False | ストリーミング出力を有効にします。有効にすると、API はイテレート可能なジェネレーターを返します。各チャンクは応答の増分部分です。 |
stop (オプション) | 文字列または配列 | None |
|
tools (オプション) | 配列 | None | モデルが呼び出せるツール。関数呼び出しフローでは、モデルがこのライブラリから 1 つのツールを選択します。
関数呼び出しフローでは、呼び出しを開始するときとツールの結果を送信するときに 説明
|
stream_options (オプション) | オブジェクト | None | ストリーミング中にトークン使用量を表示します。 |
応答パラメーター
パラメーター | タイプ | 説明 | 備考 |
id | 文字列 | リクエストに対してシステムが生成する一意の ID。 | - |
model | 文字列 | リクエストに使用されたモデル。 | - |
system_fingerprint | 文字列 | 現在未使用。空の文字列を返します。 | - |
choices | 配列 | 生成されたチャット補完のリスト。 | - |
choices[i].finish_reason | 文字列 | モデルがトークンの生成を停止した理由。考えられる値は次のとおりです。
| |
choices[i].message | オブジェクト | モデルによって生成されたメッセージオブジェクト。 | |
choices[i].message.role | 文字列 | メッセージ作成者のロール。この値は常に | |
choices[i].message.content | 文字列 | モデルが生成したメッセージ本文。 | |
choices[i].index | 整数 |
| |
created | 整数 | チャット補完の作成時刻 (UNIX タイムスタンプ、秒単位)。 | - |
usage | オブジェクト | リクエストのトークン使用量統計。 | - |
usage.prompt_tokens | 整数 | 入力プロンプト内のトークン数。 | - |
usage.completion_tokens | 整数 | 生成された補完内のトークン数。 | - |
usage.total_tokens | 整数 | リクエストで使用されたトークンの合計数 ( | - |
langchain_openai SDK 経由での呼び出し
前提条件
Python をインストールします。
langchain_openai SDK をインストールします。
# 次のコマンドが失敗する場合は、pip を pip3 に置き換えてください。 pip install -U langchain_openai
Model Studio を有効化し、API キーを取得します。「API キーの取得」をご参照ください。
API キーを環境変数として設定し、漏洩リスクを低減します。「API キーを環境変数として設定する」をご参照ください。コード内に直接設定することも可能ですが、これにより漏洩リスクが高まります。
サポートされるモデルの一覧からモデルを選択します。
使用方法
非ストリーミング出力
invoke メソッドを使用して非ストリーミング出力を取得します。
from langchain_openai import ChatOpenAI
import os
def get_response():
llm = ChatOpenAI(
# API キーはリージョンごとに異なります。API キーの取得方法については、https://www.alibabacloud.com/help/ja/model-studio/get-api-key をご参照ください。
api_key=os.getenv("DASHSCOPE_API_KEY"), # 環境変数を設定していない場合は、この行を Model Studio API キーに置き換えてください: api_key="sk-xxx"
base_url="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1", # これはシンガポールリージョンの base_url です。
model="qwen-plus" # この例では qwen-plus を使用しています。必要に応じてモデル名を変更できます。モデルの一覧については、https://www.alibabacloud.com/help/ja/model-studio/getting-started/models をご参照ください。
)
messages = [
{"role":"system","content":"You are a helpful assistant."},
{"role":"user","content":"Who are you?"}
]
response = llm.invoke(messages)
print(response.json())
if __name__ == "__main__":
get_response()出力:
{
"content": "I am a large language model from Alibaba Cloud. My name is Tongyi Qwen.",
"additional_kwargs": {},
"response_metadata": {
"token_usage": {
"completion_tokens": 16,
"prompt_tokens": 22,
"total_tokens": 38
},
"model_name": "qwen-plus",
"system_fingerprint": "",
"finish_reason": "stop",
"logprobs": null
},
"type": "ai",
"name": null,
"id": "run-xxx",
"example": false,
"tool_calls": [],
"invalid_tool_calls": []
}ストリーミング出力
stream メソッドを使用してストリーミング出力を取得します。追加の stream パラメーターは不要です。
from langchain_openai import ChatOpenAI
import os
def get_response():
llm = ChatOpenAI(
# API キーはリージョンごとに異なります。API キーの取得方法については、https://www.alibabacloud.com/help/ja/model-studio/get-api-key をご参照ください。
api_key=os.getenv("DASHSCOPE_API_KEY"), # 環境変数を設定していない場合は、この行を Model Studio API キーに置き換えてください: api_key="sk-xxx"
base_url="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1", # これはシンガポールリージョンの base_url です。
model="qwen-plus", # この例では qwen-plus を使用しています。必要に応じてモデル名を変更できます。モデルの一覧については、https://www.alibabacloud.com/help/ja/model-studio/getting-started/models をご参照ください。
stream_usage=True
)
messages = [
{"role":"system","content":"You are a helpful assistant."},
{"role":"user","content":"Who are you?"},
]
response = llm.stream(messages)
for chunk in response:
print(chunk.model_dump_json())
if __name__ == "__main__":
get_response()出力:
{"content": "", "additional_kwargs": {}, "response_metadata": {}, "type": "AIMessageChunk", "name": null, "id": "run-xxx", "example": false, "tool_calls": [], "invalid_tool_calls": [], "usage_metadata": null, "tool_call_chunks": []}
{"content": "I", "additional_kwargs": {}, "response_metadata": {}, "type": "AIMessageChunk", "name": null, "id": "run-xxx", "example": false, "tool_calls": [], "invalid_tool_calls": [], "usage_metadata": null, "tool_call_chunks": []}
{"content": " am", "additional_kwargs": {}, "response_metadata": {}, "type": "AIMessageChunk", "name": null, "id": "run-xxx", "example": false, "tool_calls": [], "invalid_tool_calls": [], "usage_metadata": null, "tool_call_chunks": []}
{"content": " a", "additional_kwargs": {}, "response_metadata": {}, "type": "AIMessageChunk", "name": null, "id": "run-xxx", "example": false, "tool_calls": [], "invalid_tool_calls": [], "usage_metadata": null, "tool_call_chunks": []}
{"content": " large", "additional_kwargs": {}, "response_metadata": {}, "type": "AIMessageChunk", "name": null, "id": "run-xxx", "example": false, "tool_calls": [], "invalid_tool_calls": [], "usage_metadata": null, "tool_call_chunks": []}
{"content": " language model", "additional_kwargs": {}, "response_metadata": {}, "type": "AIMessageChunk", "name": null, "id": "run-xxx", "example": false, "tool_calls": [], "invalid_tool_calls": [], "usage_metadata": null, "tool_call_chunks": []}
{"content": " from", "additional_kwargs": {}, "response_metadata": {}, "type": "AIMessageChunk", "name": null, "id": "run-xxx", "example": false, "tool_calls": [], "invalid_tool_calls": [], "usage_metadata": null, "tool_call_chunks": []}
{"content": " Alibaba Cloud. My name is Tongyi", "additional_kwargs": {}, "response_metadata": {}, "type": "AIMessageChunk", "name": null, "id": "run-xxx", "example": false, "tool_calls": [], "invalid_tool_calls": [], "usage_metadata": null, "tool_call_chunks": []}
{"content": " Qwen.", "additional_kwargs": {}, "response_metadata": {}, "type": "AIMessageChunk", "name": null, "id": "run-xxx", "example": false, "tool_calls": [], "invalid_tool_calls": [], "usage_metadata": null, "tool_call_chunks": []}
{"content": "", "additional_kwargs": {}, "response_metadata": {"finish_reason": "stop"}, "type": "AIMessageChunk", "name": null, "id": "run-xxx", "example": false, "tool_calls": [], "invalid_tool_calls": [], "usage_metadata": null, "tool_call_chunks": []}
{"content": "", "additional_kwargs": {}, "response_metadata": {}, "type": "AIMessageChunk", "name": null, "id": "run-xxx", "example": false, "tool_calls": [], "invalid_tool_calls": [], "usage_metadata": {"input_tokens": 22, "output_tokens": 16, "total_tokens": 38}, "tool_call_chunks": []}入力パラメーターについては、「入力パラメーター」をご参照ください。
HTTP API 呼び出し
HTTP 経由で Model Studio を呼び出します。応答は OpenAI API と同じ構造に従います。
前提条件
Model Studio を有効化し、API キーを取得します。「API キーの取得」をご参照ください。
API キーを環境変数として設定し、漏洩リスクを低減します。「API キーを環境変数として設定する」をご参照ください。コード内に直接設定することも可能ですが、これにより漏洩リスクが高まります。
API リクエスト
シンガポール: POST https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1/chat/completions
米国 (バージニア): POST https://dashscope-us.aliyuncs.com/compatible-mode/v1/chat/completions
中国 (北京): POST https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions
中国 (香港): POST https://{WorkspaceId}.cn-hongkong.maas.aliyuncs.com/compatible-mode/v1/chat/completionsリクエスト例
cURL を使用して API を呼び出します。
API キーを環境変数として設定していない場合は、$DASHSCOPE_API_KEY をご利用の API キーに置き換えてください。
非ストリーミング出力
# これはシンガポールリージョンの base_url です。
curl --location 'https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1/chat/completions' \
--header "Authorization: Bearer $DASHSCOPE_API_KEY" \
--header 'Content-Type: application/json' \
--data '{
"model": "qwen-plus",
"messages": [
{
"role": "system",
"content": "You are a helpful assistant."
},
{
"role": "user",
"content": "Who are you?"
}
]
}'
出力:
{
"choices": [
{
"message": {
"role": "assistant",
"content": "I am a large language model from Alibaba Cloud. My name is Qwen."
},
"finish_reason": "stop",
"index": 0,
"logprobs": null
}
],
"object": "chat.completion",
"usage": {
"prompt_tokens": 11,
"completion_tokens": 16,
"total_tokens": 27
},
"created": 1715252778,
"system_fingerprint": "",
"model": "qwen-plus",
"id": "chatcmpl-xxx"
}ストリーミング出力
ストリーミング出力を有効にするには、リクエストボディ内で stream パラメーターを true に設定します。
# これはシンガポールリージョンの base_url です。
curl --location 'https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1/chat/completions' \
--header "Authorization: Bearer $DASHSCOPE_API_KEY" \
--header 'Content-Type: application/json' \
--data '{
"model": "qwen-plus",
"messages": [
{
"role": "system",
"content": "You are a helpful assistant."
},
{
"role": "user",
"content": "Who are you?"
}
],
"stream":true
}'出力:
data: {"choices":[{"delta":{"content":"","role":"assistant"},"index":0,"logprobs":null,"finish_reason":null}],"object":"chat.completion.chunk","usage":null,"created":1715931028,"system_fingerprint":null,"model":"qwen-plus","id":"chatcmpl-3bb05cf5cd819fbca5f0b8d67a025022"}
data: {"choices":[{"finish_reason":null,"delta":{"content":"I am "},"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1715931028,"system_fingerprint":null,"model":"qwen-plus","id":"chatcmpl-3bb05cf5cd819fbca5f0b8d67a025022"}
data: {"choices":[{"delta":{"content":"a large "},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1715931028,"system_fingerprint":null,"model":"qwen-plus","id":"chatcmpl-3bb05cf5cd819fbca5f0b8d67a025022"}
data: {"choices":[{"delta":{"content":"language "},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1715931028,"system_fingerprint":null,"model":"qwen-plus","id":"chatcmpl-3bb05cf5cd819fbca5f0b8d67a025022"}
data: {"choices":[{"delta":{"content":"model from "},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1715931028,"system_fingerprint":null,"model":"qwen-plus","id":"chatcmpl-3bb05cf5cd819fbca5f0b8d67a025022"}
data: {"choices":[{"delta":{"content":"Alibaba Cloud. "},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1715931028,"system_fingerprint":null,"model":"qwen-plus","id":"chatcmpl-3bb05cf5cd819fbca5f0b8d67a025022"}
data: {"choices":[{"delta":{"content":"My name is Qwen."},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1715931028,"system_fingerprint":null,"model":"qwen-plus","id":"chatcmpl-3bb05cf5cd819fbca5f0b8d67a025022"}
data: {"choices":[{"delta":{"content":""},"finish_reason":"stop","index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1715931028,"system_fingerprint":null,"model":"qwen-plus","id":"chatcmpl-3bb05cf5cd819fbca5f0b8d67a025022"}
data: [DONE]
「入力パラメーターの構成」をご参照ください。
エラー応答の例
リクエストが失敗した場合、応答には失敗の原因を説明するエラーコードとエラーメッセージが含まれます。
{
"error": {
"message": "Incorrect API key provided. ",
"type": "invalid_request_error",
"param": null,
"code": "invalid_api_key"
}
}ステータスコード
エラーコード | 説明 |
400 - Invalid request error | 無効なリクエストです。詳細はエラーメッセージをご確認ください。 |
401 - Incorrect API key provided | 指定された API キーが無効です。 |
429 - Rate limit reached for requests | リクエストレートが制限を超えました (例: 秒間クエリ数 (QPS) や分間クエリ数 (QPM))。 |
429 - You exceeded your current quota, please check your plan and billing details | クォータを超えているか、アカウントに支払い遅延があります。 |
500 - The server had an error while processing your request | サーバーで内部エラーが発生しました。 |
503 - The engine is currently overloaded, please try again later | サービスが一時的に過負荷状態です。しばらくしてから再度お試しください。 |