Qwen VL モデルは OpenAI インターフェースと互換性があります。OpenAI から移行する場合は、base_url
、api_key
、model
の 3 つのパラメーターを変更するだけで、Qwen を使用できます。
パラメーターを準備する
base_url
base_url
は、Model Studio のモデルサービスのエンドポイントまたはアドレスを指定します。
OpenAI SDK またはその他の OpenAI 互換 SDK を使用する場合は、base_url を次のように設定します。
https://dashscope-intl.aliyuncs.com/compatible-mode/v1
HTTP リクエストを使用する場合は、エンドポイントを次のように設定します。
POST https://dashscope-intl.aliyuncs.com/compatible-mode/v1/chat/completions
api_key
Model Studio をアクティブ化して API キーを取得する。API キーの漏洩の脅威を軽減するために、API キーを環境変数として設定することをお勧めします。
model
model
は、使用するモデルの名前を指定します。サポートされている Qwen VL モデルは次のとおりです。
カテゴリ | 名前 |
Qwen | qvq-max qvq-max-latest qvq-max-2025-03-25 qwen-vl-max qwen-vl-plus |
Qwen-オープンソース | qwen2.5-vl-72b-instruct qwen2.5-vl-32b-instruct qwen2.5-vl-7b-instruct qwen2.5-vl-3b-instruct |
OpenAI SDK を使用する
前提条件
Python がインストールされていること。
最新バージョンの OpenAI SDK がインストールされていること。
# 次のコマンドでエラーが返された場合は、pip を pip3 に置き換えてください。pip3 は Python 3 以降のバージョンで使用されます pip install -U openai
使用方法
次の例は、OpenAI SDK を使用して Model Studio の Qwen VL モデルにアクセスする方法を示しています。
非ストリーミング出力
入力には 1 つまたは複数の画像を含めることができます。
from openai import OpenAI
import os
def get_response():
client = OpenAI(
api_key=os.getenv("DASHSCOPE_API_KEY"),
base_url="https://dashscope-intl.aliyuncs.com/compatible-mode/v1",
)
completion = client.chat.completions.create(
model="qwen-vl-plus",
messages=[
{
"role": "user",
"content": [
{
"type": "text",
"text": "What are these"
},
{
"type": "image_url",
"image_url": {
"url": "https://dashscope.oss-cn-beijing.aliyuncs.com/images/dog_and_girl.jpeg"
}
},
{
"type": "image_url",
"image_url": {
"url": "https://dashscope.oss-cn-beijing.aliyuncs.com/images/tiger.png"
}
}
]
}
]
)
print(completion.model_dump_json())
if __name__=='__main__':
get_response()
サンプルレスポンス:
{
"id": "chatcmpl-3edf4830-fb91-90d2-bec4-d3e97a5910ea",
"choices": [
{
"finish_reason": "stop",
"index": 0,
"logprobs": null,
"message": {
"content": "In image 1, a woman is interacting with her pet dog on the beach. The dog raises its front paw as if wanting to shake hands.\nImage 2 is a CG-rendered picture of a tiger.",
"role": "assistant",
"function_call": null,
"tool_calls": null
}
}
],
"created": 1724638019,
"model": "qwen-vl-plus",
"object": "chat.completion",
"service_tier": null,
"system_fingerprint": null,
"usage": {
"completion_tokens": 33,
"prompt_tokens": 2509,
"total_tokens": 2542
}
}
ストリーミング出力
from openai import OpenAI
import os
def get_response():
client = OpenAI(
api_key=os.getenv("DASHSCOPE_API_KEY"),
base_url="https://dashscope-intl.aliyuncs.com/compatible-mode/v1",
)
completion = client.chat.completions.create(
model="qwen-vl-plus",
messages=[
{
"role": "user",
"content": [
{
"type": "text",
"text": "What is this"
},
{
"type": "image_url",
"image_url": {
"url": "https://dashscope.oss-cn-beijing.aliyuncs.com/images/dog_and_girl.jpeg"
}
}
]
}
],
stream=True,
stream_options={"include_usage":True}
)
for chunk in completion:
print(chunk.model_dump())
if __name__=='__main__':
get_response()
サンプルレスポンス:
{'id': 'chatcmpl-6cf91cc7-1121-9977-b4bc-5e7d1fbfd693', 'choices': [{'delta': {'content': '', 'function_call': None, 'role': 'assistant', 'tool_calls': None}, 'finish_reason': None, 'index': 0, 'logprobs': None}], 'created': 1721823365, 'model': 'qwen-vl-plus', 'object': 'chat.completion.chunk', 'service_tier': None, 'system_fingerprint': None, 'usage': None}
{'id': 'chatcmpl-6cf91cc7-1121-9977-b4bc-5e7d1fbfd693', 'choices': [{'delta': {'content': 'In', 'function_call': None, 'role': None, 'tool_calls': None}, 'finish_reason': None, 'index': 0, 'logprobs': None}], 'created': 1721823365, 'model': 'qwen-vl-plus', 'object': 'chat.completion.chunk', 'service_tier': None, 'system_fingerprint': None, 'usage': None}
{'id': 'chatcmpl-6cf91cc7-1121-9977-b4bc-5e7d1fbfd693', 'choices': [{'delta': {'content': 'the', 'function_call': None, 'role': None, 'tool_calls': None}, 'finish_reason': None, 'index': 0, 'logprobs': None}], 'created': 1721823365, 'model': 'qwen-vl-plus', 'object': 'chat.completion.chunk', 'service_tier': None, 'system_fingerprint': None, 'usage': None}
{'id': 'chatcmpl-6cf91cc7-1121-9977-b4bc-5e7d1fbfd693', 'choices': [{'delta': {'content': 'picture', 'function_call': None, 'role': None, 'tool_calls': None}, 'finish_reason': None, 'index': 0, 'logprobs': None}], 'created': 1721823365, 'model': 'qwen-vl-plus', 'object': 'chat.completion.chunk', 'service_tier': None, 'system_fingerprint': None, 'usage': None}
{'id': 'chatcmpl-6cf91cc7-1121-9977-b4bc-5e7d1fbfd693', 'choices': [{'delta': {'content': 'is a woman', 'function_call': None, 'role': None, 'tool_calls': None}, 'finish_reason': None, 'index': 0, 'logprobs': None}], 'created': 1721823365, 'model': 'qwen-vl-plus', 'object': 'chat.completion.chunk', 'service_tier': None, 'system_fingerprint': None, 'usage': None}
{'id': 'chatcmpl-6cf91cc7-1121-9977-b4bc-5e7d1fbfd693', 'choices': [{'delta': {'content': 'and her dog interacting', 'function_call': None, 'role': None, 'tool_calls': None}, 'finish_reason': None, 'index': 0, 'logprobs': None}], 'created': 1721823365, 'model': 'qwen-vl-plus', 'object': 'chat.completion.chunk', 'service_tier': None, 'system_fingerprint': None, 'usage': None}
{'id': 'chatcmpl-6cf91cc7-1121-9977-b4bc-5e7d1fbfd693', 'choices': [{'delta': {'content': 'on the beach. The dog is sitting on the ground,', 'function_call': None, 'role': None, 'tool_calls': None}, 'finish_reason': None, 'index': 0, 'logprobs': None}], 'created': 1721823365, 'model': 'qwen-vl-plus', 'object': 'chat.completion.chunk', 'service_tier': None, 'system_fingerprint': None, 'usage': None}
{'id': 'chatcmpl-6cf91cc7-1121-9977-b4bc-5e7d1fbfd693', 'choices': [{'delta': {'content': 'extending its paw as if to shake hands or give', 'function_call': None, 'role': None, 'tool_calls': None}, 'finish_reason': None, 'index': 0, 'logprobs': None}], 'created': 1721823365, 'model': 'qwen-vl-plus', 'object': 'chat.completion.chunk', 'service_tier': None, 'system_fingerprint': None, 'usage': None}
{'id': 'chatcmpl-6cf91cc7-1121-9977-b4bc-5e7d1fbfd693', 'choices': [{'delta': {'content': 'a high five. The woman is wearing a plaid', 'function_call': None, 'role': None, 'tool_calls': None}, 'finish_reason': None, 'index': 0, 'logprobs': None}], 'created': 1721823365, 'model': 'qwen-vl-plus', 'object': 'chat.completion.chunk', 'service_tier': None, 'system_fingerprint': None, 'usage': None}
{'id': 'chatcmpl-6cf91cc7-1121-9977-b4bc-5e7d1fbfd693', 'choices': [{'delta': {'content': 'shirt and seems to be having a close interaction', 'function_call': None, 'role': None, 'tool_calls': None}, 'finish_reason': None, 'index': 0, 'logprobs': None}], 'created': 1721823365, 'model': 'qwen-vl-plus', 'object': 'chat.completion.chunk', 'service_tier': None, 'system_fingerprint': None, 'usage': None}
{'id': 'chatcmpl-6cf91cc7-1121-9977-b4bc-5e7d1fbfd693', 'choices': [{'delta': {'content': 'with the dog, smiling.', 'function_call': None, 'role': None, 'tool_calls': None}, 'finish_reason': None, 'index': 0, 'logprobs': None}], 'created': 1721823365, 'model': 'qwen-vl-plus', 'object': 'chat.completion.chunk', 'service_tier': None, 'system_fingerprint': None, 'usage': None}
{'id': 'chatcmpl-6cf91cc7-1121-9977-b4bc-5e7d1fbfd693', 'choices': [{'delta': {'content': 'The background is the ocean and the sky at sunrise or sunset.', 'function_call': None, 'role': None, 'tool_calls': None}, 'finish_reason': None, 'index': 0, 'logprobs': None}], 'created': 1721823365, 'model': 'qwen-vl-plus', 'object': 'chat.completion.chunk', 'service_tier': None, 'system_fingerprint': None, 'usage': None}
{'id': 'chatcmpl-6cf91cc7-1121-9977-b4bc-5e7d1fbfd693', 'choices': [{'delta': {'content': 'This is a', 'function_call': None, 'role': None, 'tool_calls': None}, 'finish_reason': None, 'index': 0, 'logprobs': None}], 'created': 1721823365, 'model': 'qwen-vl-plus', 'object': 'chat.completion.chunk', 'service_tier': None, 'system_fingerprint': None, 'usage': None}
{'id': 'chatcmpl-6cf91cc7-1121-9977-b4bc-5e7d1fbfd693', 'choices': [{'delta': {'content': 'picture depicting a warm moment between humans and pets.', 'function_call': None, 'role': None, 'tool_calls': None}, 'finish_reason': 'stop', 'index': 0, 'logprobs': None}], 'created': 1721823365, 'model': 'qwen-vl-plus', 'object': 'chat.completion.chunk', 'service_tier': None, 'system_fingerprint': None, 'usage': None}
{'id': 'chatcmpl-6cf91cc7-1121-9977-b4bc-5e7d1fbfd693', 'choices': [], 'created': 1721823365, 'model': 'qwen-vl-plus', 'object': 'chat.completion.chunk', 'service_tier': None, 'system_fingerprint': None, 'usage': {'completion_tokens': 75, 'prompt_tokens': 1276, 'total_tokens': 1351}}
ローカルファイル
API リクエストのペイロードは 6 MB に制限されているため、Qwen VL モデルはこの制限を超える base64 エンコード文字列を受け入れません。入力画像の元のサイズが 4.5 MB 未満であることを確認してください。
Qwen VL モデルは、呼び出しのために base64 エンコード文字列として画像入力を受け入れます。サンプル画像: test.png。
from openai import OpenAI
import os
import base64
# Base64 エンコード形式
def encode_image(image_path):
with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode('utf-8')
def get_response(image_path):
base64_image = encode_image(image_path)
client = OpenAI(
api_key=os.getenv("DASHSCOPE_API_KEY"),
base_url="https://dashscope-intl.aliyuncs.com/compatible-mode/v1",
)
completion = client.chat.completions.create(
model="qwen-vl-plus",
messages=[
{
"role": "user",
"content": [
{
"type": "text",
"text": "What is this"
},
{
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{base64_image}"
}
}
]
}
],
stream=True,
stream_options={"include_usage":True}
)
for chunk in completion:
print(chunk.model_dump())
if __name__=='__main__':
get_response("test.png")
非ストリーミング出力を有効にするには、ストリーミング関連のパラメーターを省略し、completion
を出力します。
サンプルレスポンス
{'id': 'chatcmpl-42012997-2e91-9579-9da1-8806aa79db13', 'choices': [{'delta': {'content': '', 'function_call': None, 'role': 'assistant', 'tool_calls': None}, 'finish_reason': None, 'index': 0, 'logprobs': None}], 'created': 1724728778, 'model': 'qwen-vl-plus', 'object': 'chat.completion.chunk', 'service_tier': None, 'system_fingerprint': None, 'usage': None}
{'id': 'chatcmpl-42012997-2e91-9579-9da1-8806aa79db13', 'choices': [{'delta': {'content': 'This', 'function_call': None, 'role': None, 'tool_calls': None}, 'finish_reason': None, 'index': 0, 'logprobs': None}], 'created': 1724728778, 'model': 'qwen-vl-plus', 'object': 'chat.completion.chunk', 'service_tier': None, 'system_fingerprint': None, 'usage': None}
{'id': 'chatcmpl-42012997-2e91-9579-9da1-8806aa79db13', 'choices': [{'delta': {'content': 'is an', 'function_call': None, 'role': None, 'tool_calls': None}, 'finish_reason': None, 'index': 0, 'logprobs': None}], 'created': 1724728778, 'model': 'qwen-vl-plus', 'object': 'chat.completion.chunk', 'service_tier': None, 'system_fingerprint': None, 'usage': None}
{'id': 'chatcmpl-42012997-2e91-9579-9da1-8806aa79db13', 'choices': [{'delta': {'content': 'eagle flying in the sky. It has broad wings and is', 'function_call': None, 'role': None, 'tool_calls': None}, 'finish_reason': None, 'index': 0, 'logprobs': None}], 'created': 1724728778, 'model': 'qwen-vl-plus', 'object': 'chat.completion.chunk', 'service_tier': None, 'system_fingerprint': None, 'usage': None}
{'id': 'chatcmpl-42012997-2e91-9579-9da1-8806aa79db13', 'choices': [{'delta': {'content': 'soaring among the clouds. This', 'function_call': None, 'role': None, 'tool_calls': None}, 'finish_reason': None, 'index': 0, 'logprobs': None}], 'created': 1724728778, 'model': 'qwen-vl-plus', 'object': 'chat.completion.chunk', 'service_tier': None, 'system_fingerprint': None, 'usage': None}
{'id': 'chatcmpl-42012997-2e91-9579-9da1-8806aa79db13', 'choices': [{'delta': {'content': 'bird is often considered a symbol of strength, freedom, and ambition, and holds significant meaning in many cultures.', 'function_call': None, 'role': None, 'tool_calls': None}, 'finish_reason': 'stop', 'index': 0, 'logprobs': None}], 'created': 1724728778, 'model': 'qwen-vl-plus', 'object': 'chat.completion.chunk', 'service_tier': None, 'system_fingerprint': None, 'usage': None}
{'id': 'chatcmpl-42012997-2e91-9579-9da1-8806aa79db13', 'choices': [], 'created': 1724728778, 'model': 'qwen-vl-plus', 'object': 'chat.completion.chunk', 'service_tier': None, 'system_fingerprint': None, 'usage': {'completion_tokens': 47, 'prompt_tokens': 1254, 'total_tokens': 1301}}
リクエストパラメーター
リクエストパラメーターは、OpenAI インターフェースのパラメーターと一致しています。次の表にパラメーターを示します。
パラメーター | タイプ | 説明 |
model | 文字列 | モデル名を指定します。 |
messages | 配列 | ユーザーとモデル間の会話履歴。配列の各要素は ロールの有効な値: system、user、assistant。system ロールは配列の最初の要素 ( |
top_p | 浮動小数点数 | オプション。nucleus サンプリングの確率しきい値。たとえば、このパラメーターを 0.8 に設定すると、モデルは累積確率が 0.8 以上である最小のトークンセットを選択します。値が大きいほど、生成されるコンテンツにランダム性が加わります。 有効な値: (0,1.0]。 |
max_tokens | 整数 | オプション。モデルが生成できるトークンの最大数。 |
temperature | 浮動小数点数 | オプション。生成されるコンテンツのランダム性と多様性。具体的には、このパラメーターの値は、モデルが各単語をサンプリングする確率分布を制御します。
有効な値: [0, 2)。0 は意味がないため、このパラメーターを 0 に設定しないことをお勧めします。 |
presence_penalty | 浮動小数点数 | オプション。生成されるコンテンツでの単語の繰り返し。値が大きいほど、生成されるコンテンツでの単語の繰り返しが少なくなります。 有効な値: [-2.0, 2.0]。 |
seed | 整数 | オプション。コンテンツ生成中に使用されるランダムシード。このパラメーターは、生成されるコンテンツのランダム性を制御します。 有効な値: 64 ビット符号なし整数。 |
stream | ブール値 | オプション。ストリーミング出力モードを有効にするかどうかを指定します。ストリーミング出力モードでは、モデルはジェネレーターを返します。ジェネレーターから結果を取得し、テキストを段階的に表示するには、反復ループを使用する必要があります。 デフォルト値: False。 |
stop | 文字列または配列 | オプション。このパラメーターに文字列またはトークン ID を指定すると、モデルは文字列またはトークンが生成されようとしているときにコンテンツの生成を停止します。stop パラメーターの値は、文字列または配列です。
|
stream_options | オブジェクト | オプション。ストリーミング出力モードで使用されるトークン数を表示するかどうかを指定します。このパラメーターは、stream パラメーターが True に設定されている場合にのみ有効になります。ストリーミング出力モードで使用されるトークン数をカウントする場合は、このパラメーターを |
langchain_openai SDK を使用する
前提条件
Python がインストールされていること。
langchain_openai SDK がインストールされていること。langchain_openai SDK をインストールするには、次のコマンドを実行します。
# 次のコマンドでエラーが返された場合は、pip を pip3 に置き換えてください。pip3 は Python 3 以降のバージョンで使用されます pip install -U langchain_openai
使用方法
次の例は、langchain_openai SDK を使用して Model Studio の Qwen VL モデルにアクセスする方法を示しています。
非ストリーミング出力
次の例では、invoke メソッドを使用して非ストリーミング出力を実装しています。
from langchain_openai import ChatOpenAI
import os
def get_response():
llm = ChatOpenAI(
# 環境変数を設定していない場合は、DASHSCOPE_API_KEY を API キーに置き換えてください。
api_key=os.getenv("DASHSCOPE_API_KEY"),
base_url="https://dashscope-intl.aliyuncs.com/compatible-mode/v1",
model="qwen-vl-plus",
)
messages= [
{
"role": "user",
"content": [
{
"type": "text",
"text": "What is this"
},
{
"type": "image_url",
"image_url": {
"url": "https://dashscope.oss-cn-beijing.aliyuncs.com/images/dog_and_girl.jpeg"
}
}
]
}
]
response = llm.invoke(messages)
print(response.json(ensure_ascii=False))
if __name__ == "__main__":
get_response()
サンプルレスポンス:
{
"content": "The picture shows a woman interacting with her dog on the beach. The dog is sitting on the ground, extending its paw as if to shake hands or give a high five. The woman is wearing a plaid shirt and seems to be having a close interaction with the dog, smiling. The background is the ocean and the sky at sunrise or sunset. This is a warm photo depicting a moment of friendship between humans and pets.",
"additional_kwargs": {},
"response_metadata": {
"token_usage": {
"completion_tokens": 79,
"prompt_tokens": 1276,
"total_tokens": 1355
},
"model_name": "qwen-vl-plus",
"system_fingerprint": null,
"finish_reason": "stop",
"logprobs": null
},
"type": "ai",
"name": null,
"id": "run-c72701d2-e2c6-40a8-9e8b-37b58d53160f-0",
"example": false,
"tool_calls": [],
"invalid_tool_calls": [],
"usage_metadata": {
"input_tokens": 1276,
"output_tokens": 79,
"total_tokens": 1355
}
}
ストリーミング出力
from langchain_openai import ChatOpenAI
import os
def get_response():
llm = ChatOpenAI(
# 環境変数を設定していない場合は、DASHSCOPE_API_KEY を API キーに置き換えてください。
api_key=os.getenv("DASHSCOPE_API_KEY"),
base_url="https://dashscope-intl.aliyuncs.com/compatible-mode/v1",
model="qwen-plus",
# ストリーミング出力の最後の行にトークンの使用状況を表示するには、次の設定を追加します。
stream_options={"include_usage": True}
)
messages= [
{
"role": "user",
"content": [
{
"type": "text",
"text": "What is this"
},
{
"type": "image_url",
"image_url": {
"url": "https://dashscope.oss-cn-beijing.aliyuncs.com/images/dog_and_girl.jpeg"
}
}
]
}
]
response = llm.stream(messages)
for chunk in response:
print(chunk.json(ensure_ascii=False))
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": "This", "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": "picture", "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": "shows", "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 dog and a young", "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": "girl. The dog looks", "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": "friendly and could be", "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 pet. The young girl", "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": "seems to be interacting with", "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": "or playing with the dog,", "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": "depicting a warm", "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": "relationship between", "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": "humans and animals.", "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": {}, "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": 23, "output_tokens": 40, "total_tokens": 63}, "tool_call_chunks": []}
リクエストパラメーターの詳細については、「リクエストパラメーター」をご参照ください。
HTTP を使用する
OpenAI と同じ構造の HTTP インターフェースを使用して、Model Studio からレスポンスを取得できます。
エンドポイントを指定する
POST https://dashscope-intl.aliyuncs.com/compatible-mode/v1/chat/completions
サンプルリクエスト
次の例では、cURL
コマンドを使用して API を呼び出します。
環境変数を設定していない場合は、$DASHSCOPE_API_KEY を API キーに置き換えてください。
非ストリーミング出力
curl --location 'https://dashscope-intl.aliyuncs.com/compatible-mode/v1/chat/completions' \
--header "Authorization: Bearer $DASHSCOPE_API_KEY" \
--header 'Content-Type: application/json' \
--data '{
"model": "qwen-vl-plus",
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": "What are these"
},
{
"type": "image_url",
"image_url": {
"url": "https://dashscope.oss-cn-beijing.aliyuncs.com/images/dog_and_girl.jpeg"
}
},
{
"type": "image_url",
"image_url": {
"url": "https://dashscope.oss-cn-beijing.aliyuncs.com/images/tiger.png"
}
}
]
}
]
}'
サンプルレスポンス:
{
"choices": [
{
"message": {
"content": "In image 1, a woman is interacting with her pet dog on the beach. The dog raises its front paw as if wanting to shake hands.\nImage 2 is a CG-rendered picture of a tiger.",
"role": "assistant"
},
"finish_reason": "stop",
"index": 0,
"logprobs": null
}
],
"object": "chat.completion",
"usage": {
"prompt_tokens": 2509,
"completion_tokens": 34,
"total_tokens": 2543
},
"created": 1724729556,
"system_fingerprint": null,
"model": "qwen-vl-plus",
"id": "chatcmpl-1abb4eb9-f508-9637-a8ba-ac7fc6f73e53"
}
ストリーミング出力
ストリーミング出力モードを使用する場合は、リクエスト本文で stream パラメーターを true に設定します。
curl --location 'https://dashscope-intl.aliyuncs.com/compatible-mode/v1/chat/completions' \
--header "Authorization: Bearer $DASHSCOPE_API_KEY" \
--header 'Content-Type: application/json' \
--data '{
"model": "qwen-vl-plus",
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": "What is this"
},
{
"type": "image_url",
"image_url": {
"url": "https://dashscope.oss-cn-beijing.aliyuncs.com/images/dog_and_girl.jpeg"
}
}
]
}
],
"stream":true,
"stream_options":{"include_usage":true}
}'
サンプルレスポンス:
data: {"choices":[{"delta":{"content":"","role":"assistant"},"index":0,"logprobs":null,"finish_reason":null}],"object":"chat.completion.chunk","usage":null,"created":1724729595,"system_fingerprint":null,"model":"qwen-vl-plus","id":"chatcmpl-4c83f437-303f-907b-9de5-79cac83d6b18"}
data: {"choices":[{"finish_reason":null,"delta":{"content":"In"},"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1724729595,"system_fingerprint":null,"model":"qwen-vl-plus","id":"chatcmpl-4c83f437-303f-907b-9de5-79cac83d6b18"}
data: {"choices":[{"delta":{"content":"the"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1724729595,"system_fingerprint":null,"model":"qwen-vl-plus","id":"chatcmpl-4c83f437-303f-907b-9de5-79cac83d6b18"}
data: {"choices":[{"delta":{"content":"picture"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1724729595,"system_fingerprint":null,"model":"qwen-vl-plus","id":"chatcmpl-4c83f437-303f-907b-9de5-79cac83d6b18"}
data: {"choices":[{"delta":{"content":"is a woman"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1724729595,"system_fingerprint":null,"model":"qwen-vl-plus","id":"chatcmpl-4c83f437-303f-907b-9de5-79cac83d6b18"}
data: {"choices":[{"delta":{"content":"and her dog interacting"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1724729595,"system_fingerprint":null,"model":"qwen-vl-plus","id":"chatcmpl-4c83f437-303f-907b-9de5-79cac83d6b18"}
data: {"choices":[{"delta":{"content":"on the beach. The dog is sitting on the ground,"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1724729595,"system_fingerprint":null,"model":"qwen-vl-plus","id":"chatcmpl-4c83f437-303f-907b-9de5-79cac83d6b18"}
data: {"choices":[{"delta":{"content":"extending its paw as if to shake hands or give"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1724729595,"system_fingerprint":null,"model":"qwen-vl-plus","id":"chatcmpl-4c83f437-303f-907b-9de5-79cac83d6b18"}
data: {"choices":[{"delta":{"content":"a high five. The woman is wearing a plaid"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1724729595,"system_fingerprint":null,"model":"qwen-vl-plus","id":"chatcmpl-4c83f437-303f-907b-9de5-79cac83d6b18"}
data: {"choices":[{"delta":{"content":"shirt and seems to be having a close interaction"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1724729595,"system_fingerprint":null,"model":"qwen-vl-plus","id":"chatcmpl-4c83f437-303f-907b-9de5-79cac83d6b18"}
data: {"choices":[{"delta":{"content":"with the dog, smiling."},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1724729595,"system_fingerprint":null,"model":"qwen-vl-plus","id":"chatcmpl-4c83f437-303f-907b-9de5-79cac83d6b18"}
data: {"choices":[{"delta":{"content":"The background is the ocean and the sky at sunrise or sunset."},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1724729595,"system_fingerprint":null,"model":"qwen-vl-plus","id":"chatcmpl-4c83f437-303f-907b-9de5-79cac83d6b18"}
data: {"choices":[{"delta":{"content":"This is a"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1724729595,"system_fingerprint":null,"model":"qwen-vl-plus","id":"chatcmpl-4c83f437-303f-907b-9de5-79cac83d6b18"}
data: {"choices":[{"finish_reason":"stop","delta":{"content":"picture depicting a warm moment between humans and pets."},"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1724729595,"system_fingerprint":null,"model":"qwen-vl-plus","id":"chatcmpl-4c83f437-303f-907b-9de5-79cac83d6b18"}
data: {"choices":[],"object":"chat.completion.chunk","usage":{"prompt_tokens":1276,"completion_tokens":75,"total_tokens":1351},"created":1724729595,"system_fingerprint":null,"model":"qwen-vl-plus","id":"chatcmpl-4c83f437-303f-907b-9de5-79cac83d6b18"}
data: [DONE]
リクエストパラメーターの詳細については、「リクエストパラメーター」をご参照ください。
サンプルエラーレスポンス
リクエストが失敗した場合、次のエラーコードとエラーメッセージが返されます。
{
"error": {
"message": "Incorrect API key provided. ",
"type": "invalid_request_error",
"param": null,
"code": "invalid_api_key"
}
}
状態コード
状態コードとメッセージについては、「状態コード」をご参照ください。