百煉為通義千問視覺模型提供了與OpenAI兼容的使用方式。如果您之前使用OpenAI SDK或者其他OpenAI兼容接口(例如langchain_openai SDK),或者HTTP方式調用OpenAI的視覺模型服務,只需在原有框架下調整API-KEY、BASE_URL、model等參數,就可以直接使用通義千問視覺模型。
兼容OpenAI需要信息
BASE_URL
BASE_URL表示模型服務的網絡訪問點或地址。通過該地址,您可以訪問服務提供的功能或數據。在Web服務或API的使用中,BASE_URL通常對應于服務的具體操作或資源的URL。當您使用OpenAI兼容接口來使用通義千問視覺模型服務時,需要配置BASE_URL。
當您通過OpenAI SDK或其他OpenAI兼容的SDK調用時,需要配置的BASE_URL如下:
https://dashscope.aliyuncs.com/compatible-mode/v1
當您通過HTTP請求調用時,需要配置的完整訪問endpoint如下:
POST https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions
獲取API-KEY
您需要開通百煉模型服務并獲得API-KEY,詳情請參考:獲取API Key。
支持的模型列表
當前OpenAI兼容接口支持的通義千問系列模型如下表所示。
通義千問VL模型按輸入和輸出的總Token數進行計費。
圖像轉換為Token的規則:512x512像素的圖像約等于334個Token,其他分辨率圖像按比例換算;最小單位是28x28像素,即每28x28像素對應一個Token,如果圖像的長或寬不是28的整數倍,則向上取整至28的整數倍;一張圖最少4個Token。
模型名稱 | 版本 | 上下文長度 | 最大輸入 | 最大輸出 | 輸入輸出單價 | 免費額度 |
(Token數) | (每千Token) | |||||
qwen-vl-max 相比qwen-vl-plus再次提升視覺推理和指令遵循能力,在更多復雜任務中提供最佳性能。 當前等同qwen-vl-max-2024-08-09 | 穩定版 | 32,000 | 30,000 單圖最大16384 | 2,000 | 0.02元 Batch調用:0.01元 | 100萬Token 有效期:百煉開通后180天內 |
qwen-vl-max-latest 始終等同最新快照版 | 最新版 | 0.02元 | ||||
qwen-vl-max-2024-11-19 又稱qwen-vl-max-1119 | 快照版 | |||||
qwen-vl-max-2024-10-30 又稱qwen-vl-max-1030 | ||||||
qwen-vl-max-2024-08-09 又稱qwen-vl-max-0809 此版本擴展上下文至32k,增強圖像理解能力,能更好地識別圖片中的多語種和手寫體。 | ||||||
qwen-vl-max-2024-02-01 又稱qwen-vl-max-0201 | 8,000 | 6,000 單圖最大1280 | ||||
qwen-vl-plus 大幅提升細節識別和文字識別能力,支持超百萬像素分辨率和任意寬高比的圖像。在廣泛的視覺任務中提供卓越性能。 | 穩定版 | 8,000 | 6,000 單圖最大1280 | 0.008元 Batch調用:0.004元 | ||
qwen-vl-plus-latest 始終等同最新快照版 | 最新版 | 32,000 | 30,000 單圖最大16384 | 0.008元 | ||
qwen-vl-plus-2024-08-09 又稱qwen-vl-plus-0809 | 快照版 | |||||
qwen-vl-plus-2023-12-01 | 8,000 | 6,000 | 2,000 |
通過OpenAI SDK調用
前提條件
請確保您的計算機上安裝了Python環境。
請安裝最新版OpenAI SDK。
# 如果下述命令報錯,請將pip替換為pip3 pip install -U openai
您需要開通百煉模型服務并獲得API-KEY,詳情請參考:獲取API Key。
我們推薦您將API-KEY配置到環境變量中以降低API-KEY的泄露風險,配置方法可參考配置API Key到環境變量。您也可以在代碼中配置API-KEY,但是泄露風險會提高。
使用方式
您可以參考以下示例來使用OpenAI SDK訪問通義千問視覺模型。
非流式輸出
您可以輸入單張或多張圖片。
from openai import OpenAI
import os
def get_response():
client = OpenAI(
api_key=os.getenv("DASHSCOPE_API_KEY"),
base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
)
completion = client.chat.completions.create(
model="qwen-vl-plus",
messages=[
{
"role": "user",
"content": [
{
"type": "text",
"text": "這些是什么"
},
{
"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": "圖1中是一名女子和她的寵物狗在沙灘上互動,狗狗抬起前爪似乎想要握手。\n圖2是CG渲染的一張老虎的圖片。",
"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.aliyuncs.com/compatible-mode/v1",
)
completion = client.chat.completions.create(
model="qwen-vl-plus",
messages=[
{
"role": "user",
"content": [
{
"type": "text",
"text": "這是什么"
},
{
"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': '圖', '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': '中', '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': '是一名', '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': '女子和她的狗在', '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': '沙灘上互動。狗狗坐在地上,', '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': '伸出爪子像是要握手或者擊', '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': '掌的樣子。這名女士穿著格子', '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': '襯衫,似乎正在與狗狗進行親密', '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': '的接觸,并且面帶微笑。', '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': '背景是海洋和日出或日', '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': '落時分的天空。這是一', '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': '幅描繪人與寵物之間溫馨時刻', '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': '的畫面。', '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請求負載限制在6M以下。所以VL模型通過base64格式輸入的字符串也不能超過此限制。對應的輸入圖片原始大小需小于4.5Mb。
qwen-vl模型支持通過base64編碼的圖片輸入,您可以將本地圖片轉換為base64字符串后進行調用。示例圖片:test.png
from openai import OpenAI
import os
import base64
# base 64 編碼格式
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.aliyuncs.com/compatible-mode/v1",
)
completion = client.chat.completions.create(
model="qwen-vl-plus",
messages=[
{
"role": "user",
"content": [
{
"type": "text",
"text": "這是什么"
},
{
"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")
如果需要非流式輸出,將stream相關配置參數去除,并直接打印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': '這', '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': '是一', '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': '只', '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': '在天空中飛翔的鷹。它有著廣闊的翅膀,正在', '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': '翱翔于云層之間。這種', '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': '鳥類通常被認為是力量、自由和雄', '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': '心壯志的象征,在許多文化', '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': '中都具有重要的意義。', '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 | string | - | 用戶使用的模型名稱。 |
messages | array | - | 用戶與模型的對話歷史。array中的每個元素形式為 |
| float | - | 生成過程中的核采樣方法概率閾值,例如,取值為0.8時,僅保留概率加起來大于等于0.8的最可能token的最小集合作為候選集。取值范圍為(0,1.0],取值越大,生成的隨機性越高;取值越小,生成的確定性越高。 |
max_tokens(可選) | integer | - | 指定模型可生成的最大token個數。 |
temperature(可選) | float | - | 用于控制模型回復的隨機性和多樣性。具體來說,temperature值控制了生成文本時對每個候選詞的概率分布進行平滑的程度。較高的temperature值會降低概率分布的峰值,使得更多的低概率詞被選擇,生成結果更加多樣化;而較低的temperature值則會增強概率分布的峰值,使得高概率詞更容易被選擇,生成結果更加確定。 取值范圍: [0, 2),不建議取值為0,無意義。 |
presence_penalty (可選) | float | - | 用戶控制模型生成時整個序列中的重復度。提高presence_penalty時可以降低模型生成的重復度,取值范圍[-2.0, 2.0]。 |
seed(可選) | integer | - | 生成時使用的隨機數種子,用于控制模型生成內容的隨機性。seed支持無符號64位整數。 |
stream(可選) | boolean | False | 用于控制是否使用流式輸出。當以stream模式輸出結果時,接口返回結果為generator,需要通過迭代獲取結果,每次輸出為當前生成的增量序列。 |
stop(可選) | string or array | None | stop參數用于實現內容生成過程的精確控制,在模型生成的內容即將包含指定的字符串或token_id時自動停止。stop可以為string類型或array類型。
|
stream_options(可選) | object | None | 該參數用于配置在流式輸出時是否展示使用的token數目。只有當stream為True的時候該參數才會激活生效。若您需要統計流式輸出模式下的token數目,可將該參數配置為 |
通過langchain_openai SDK調用
前提條件
請確保您的計算機上安裝了Python環境。
通過運行以下命令安裝langchain_openai SDK。
# 如果下述命令報錯,請將pip替換為pip3 pip install -U langchain_openai
您需要開通百煉模型服務并獲得API-KEY,詳情請參考:獲取API Key。
我們推薦您將API-KEY配置到環境變量中以降低API-KEY的泄露風險,詳情可參考配置API Key到環境變量。您也可以在代碼中配置API-KEY,但是泄露風險會提高。
使用方式
您可以參考以下示例來通過langchain_openai SDK使用通義千問視覺模型。
非流式輸出
非流式輸出使用invoke方法實現,請參考以下示例代碼:
from langchain_openai import ChatOpenAI
import os
def get_response():
llm = ChatOpenAI(
# 如果您沒有配置環境變量,請在此處用您的API Key進行替換
api_key=os.getenv("DASHSCOPE_API_KEY"),
# 填寫DashScope base_url
base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
model="qwen-vl-plus",
)
messages= [
{
"role": "user",
"content": [
{
"type": "text",
"text": "這是什么"
},
{
"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())
if __name__ == "__main__":
get_response()
運行代碼,可以得到以下結果:
{
"content": "圖中是一名女子和她的狗在沙灘上互動。狗狗坐在地上,伸出爪子像是要握手或者擊掌的樣子。這名女士穿著格子襯衫,似乎正在與狗狗進行親密的接觸,并且面帶微笑。背景是海洋和日出或日落時分的天空。這是一張充滿溫馨感的照片,展現了人與寵物之間的友誼時刻。",
"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(
# 如果您沒有配置環境變量,請在此處用您的API Key進行替換
api_key=os.getenv("DASHSCOPE_API_KEY"),
# 填寫DashScope base_url
base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
model="qwen-plus",
# 通過以下設置,在流式輸出的最后一行展示token使用信息
stream_options={"include_usage": True}
)
messages= [
{
"role": "user",
"content": [
{
"type": "text",
"text": "這是什么"
},
{
"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())
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": "這張", "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": {}, "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": {}, "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": {}, "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": {}, "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": {}, "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": {}, "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": []}
關于輸入參數的配置,可以參考輸入參數配置,相關參數在ChatOpenAI對象中定義。
通過HTTP接口調用
您可以通過HTTP接口來調用通義千問視覺模型,獲得與通過HTTP接口調用OpenAI服務相同結構的返回結果。
前提條件
您需要開通百煉模型服務并獲得API-KEY,詳情請參考:獲取API Key。
我們推薦您將API-KEY配置到環境變量中以降低API-KEY的泄露風險,配置方法可參考配置API Key到環境變量。您也可以在代碼中配置API-KEY,但是泄露風險會提高。
提交接口調用
POST https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions
請求示例
以下示例展示通過CURL
命令來調用API的腳本。
如果您沒有配置API-KEY為環境變量,需將$DASHSCOPE_API_KEY更換為您的API-KEY。
非流式輸出
curl --location 'https://dashscope.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": "這些是什么"
},
{
"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": "圖1中是一名女子和她的寵物狗在沙灘上互動,狗狗抬起前爪似乎想要握手。\n圖2是CG渲染的一張老虎的圖片。",
"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.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": "這是什么"
},
{
"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":"圖"},"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":"中"},"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":"是一名"},"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":"女子和她的狗在"},"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":"沙灘上互動。狗狗坐在地上,"},"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":"伸出爪子像是要握手或者擊"},"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":"掌的樣子。這名女士穿著格子"},"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":"襯衫,似乎正在與狗狗進行親密"},"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":"的接觸,并且面帶微笑。"},"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":"背景是海洋和日出或日"},"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":"落時分的天空。這是一"},"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":"張充滿溫馨感的照片,展現了人"},"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":"與寵物之間的友誼時刻。"},"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":79,"total_tokens":1355},"created":1724729595,"system_fingerprint":null,"model":"qwen-vl-plus","id":"chatcmpl-4c83f437-303f-907b-9de5-79cac83d6b18"}
data: [DONE]
輸入參數的詳情請參考輸入參數配置。
異常響應示例
在訪問請求出錯的情況下,輸出的結果中會通過 code 和 message 指明出錯原因。
{
"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 key不正確。 |
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 | 服務端負載過高,可重試。 |