API詳情
qwen-long是通義千問模型家族中,提供具備強大長文本處理能力的模型,最大可支持千萬tokens的對話窗口,并通過與OpenAI兼容的模式提供API服務。參考Chat,只需配置DashScope的API key以及服務的base_url,即可訪問(注:Dashscope SDK調(diào)用的方式仍然兼容)。
模型概覽
模型名 | 模型簡介 |
qwen-long | 通義千問超大規(guī)模語言模型,支持長文本上下文,以及基于長文檔、多文檔等多個場景的對話功能。具體支持的文檔格式與限制,可參見File。 |
使用說明
模型能力限制
qwen-long可支持最大10,000,000 tokens(包含您的問答歷史及上傳文檔的總tokens)的上下文,請參照以下說明選擇適合您的使用方式。
場景示例
為了幫助您選擇合適的模型使用方式,我們提供以下幾種較為常見的場景及模型的使用方法供您參考。
單文檔對話
單文檔對話 | 方法介紹 | 方法說明 |
方式1 | 通過文件服務上傳文件獲取fileid 放在system message中進行對話,參考單文檔。 | 推薦使用該方法。 |
方式2 | 可直接將文檔內(nèi)容放在system message中,參考單文檔。 | 1M tokens以下的文檔可選用該方法。 |
多文檔對話
當您在本輪對話時明確有多個文檔需要對話時
多文檔對話
方法介紹
方法說明
方式1
通過文件服務將所有需要對話的文檔上傳,并將所有的文檔id輸入system message,參考多文檔。
推薦使用該方法。
方式2
直接每個文檔內(nèi)容放進一個system message中,一并傳給大模型,可參考多文檔。
不推薦使用該方法,直接輸入多文檔內(nèi)容進行對話。受API調(diào)用請求大小所限,大量輸入文本內(nèi)容(超過1M tokens)可能會受到限制。
當您需要在對話中追加文檔進行對話時
追加文檔對話
方法介紹
方法說明
方式1
持續(xù)通過文件服務上傳文檔,而后將待追加的文檔id填入system message中繼續(xù)對話,參考追加文檔。
推薦使用該方法。
方式2
持續(xù)將待追加的文件內(nèi)容直接放入system message中繼續(xù)對話,參考追加文檔。
不推薦使用該方法,直接輸入多文檔內(nèi)容進行對話。受API調(diào)用請求大小所限,大量輸入文本內(nèi)容(超過1M tokens)可能會受到限制。
通過OpenAI SDK調(diào)用
前提條件
已開通服務并獲得API-KEY:API-KEY的獲取與配置。
安裝OpenAI SDK,注意OpenAI SDK版本需不低于1.0.0
安裝方式:pip install --upgrade 'openai>=1.0'
檢查版本:python -c 'import openai; print("version =",openai.__version__)'
使用方式
qwen-long支持長文本(文檔)對話,文檔內(nèi)容需放在role為system的message中,有以下兩種方式可將文檔信息輸入給模型:
在提前上傳文件獲取文件id(fileid)后,可以直接提供fileid。其中上傳文件的接口和操作方法可參考File。支持在對話中,使用一個或多個fileid。
直接輸入需要處理的文本格式的文檔內(nèi)容(file content)。
請避免直接將文檔內(nèi)容放在role為user的message中,role為user的message及用于role play的system message限制輸入最長為9K tokens。
使用qwen-long時,通過system message提供文檔信息時,還必須同時提供一個正常role-play的system message,默認為"You are a helpful assistant.",您也可以根據(jù)實際需求進行自定義修改,例如“你是一個文本解讀專家。”等等。請參照文檔中的范例代碼作為參考。
示例代碼
以文檔id(fileid)方式輸入文檔:
單文檔
流式輸出
使用方法:stream 設(shè)置為True
from pathlib import Path
from openai import OpenAI
client = OpenAI(
api_key="$your-dashscope-api-key", # 替換成真實DashScope的API_KEY
base_url="https://dashscope.aliyuncs.com/compatible-mode/v1", # 填寫DashScopebase_url
)
# data.pdf 是一個示例文件
file = client.files.create(file=Path("data.pdf"), purpose="file-extract")
# 新文件上傳后需要等待模型解析,首輪rt可能較長
completion = client.chat.completions.create(
model="qwen-long",
messages=[
{
'role': 'system',
'content': 'You are a helpful assistant.'
},
{
'role': 'system',
'content': f'fileid://{file.id}'
},
{
'role': 'user',
'content': '這篇文章講了什么?'
}
],
stream=True
)
for chunk in completion:
if chunk.choices[0].delta.content is not None:
print(chunk.choices[0].dict())
非流式輸出
使用方法:stream 設(shè)置為False
from pathlib import Path
from openai import OpenAI
client = OpenAI(
api_key="$your-dashscope-api-key", # 替換成真實DashScope的API_KEY
base_url="https://dashscope.aliyuncs.com/compatible-mode/v1", # 填寫DashScopebase_url
)
# data.pdf 是一個示例文件
file = client.files.create(file=Path("data.pdf"), purpose="file-extract")
# 新文件上傳后需要等待模型解析,首輪rt可能較長
completion = client.chat.completions.create(
model="qwen-long",
messages=[
{
'role': 'system',
'content': 'You are a helpful assistant.'
},
{
'role': 'system',
'content': f'fileid://{file.id}'
},
{
'role': 'user',
'content': '這篇文章講了什么?'
}
],
stream=False
)
print(completion.choices[0].message.dict())
多文檔
流式輸出
使用方法:stream 設(shè)置為True
from openai import OpenAI
client = OpenAI(
api_key="$your-dashscope-api-key", # 替換成真實DashScope的API_KEY
base_url="https://dashscope.aliyuncs.com/compatible-mode/v1", # 填寫DashScopebase_url
)
# 首次對話會等待文檔解析完成,首次rt可能較長
completion = client.chat.completions.create(
model="qwen-long",
messages=[
{
'role': 'system',
'content': 'You are a helpful assistant.'
},
{
'role': 'system',
'content': 'fileid://file-fe-xxx,fileid://file-fe-yyy,fileid://file-fe-zzz'
},
{
'role': 'user',
'content': '這幾篇文章講了什么?'
}
],
stream=True
)
for chunk in completion:
if chunk.choices[0].delta.content is not None:
print(chunk.choices[0].dict())
非流式輸出
參數(shù)stream置為False即可。
追加文檔
可在file system message中提供文檔內(nèi)容進行對話。包括直接提供文本以及通過文檔服務上傳文檔后提供文檔id來進行對話的兩種方式。這兩種文檔輸入方式在messages中暫不支持混合使用。
流式輸出
使用方法:stream 設(shè)置為True
from pathlib import Path
from openai import OpenAI
client = OpenAI(
api_key="$your-dashscope-api-key", # 替換成真實DashScope的API_KEY
base_url="https://dashscope.aliyuncs.com/compatible-mode/v1", # 填寫DashScopebase_url
)
# 首次對話會等待文檔解析完成,首次rt可能較長
completion = client.chat.completions.create(
model="qwen-long",
messages=[
{
'role': 'system',
'content': 'You are a helpful assistant.'
},
{
'role': 'system',
'content': 'fileid://file-fe-xxx'
},
{
'role': 'user',
'content': '這篇文章講了什么?'
},
{
'role': 'assistant',
'content': '這篇文章主題為大模型預訓練方法,主要內(nèi)容是xxx'
},
{
'role': 'system',
'content': 'fileid://file-fe-yyy'
},
{
'role': 'user',
'content': '這兩篇文章討論的方法有什么異同點?'
},
],
stream=True
)
for chunk in completion:
if chunk.choices[0].delta.content is not None:
print(chunk.choices[0].dict())
非流式輸出
參數(shù)stream置為False即可。
以文本方式直接輸入文檔內(nèi)容:
單文檔
流式輸出
stream 設(shè)置為True
from openai import OpenAI
client = OpenAI(
api_key="$your-dashscope-api-key", # 替換成真實DashScope的API_KEY
base_url="https://dashscope.aliyuncs.com/compatible-mode/v1", # 填寫DashScopeendpoint
)
completion = client.chat.completions.create(
model="qwen-long",
messages=[
{
'role': 'system',
'content': 'You are a helpful assistant.'
},
{
'role': 'system',
'content': '大型語言模型(llm)已經(jīng)徹底改變了人工智能領(lǐng)域,使以前被認為是人類獨有的自然語言處理任務成為可能...'
},
{
'role': 'user',
'content': '文章講了什么?'
}
],
stream=True
)
for chunk in completion:
if chunk.choices[0].delta.content is not None:
print(chunk.choices[0].dict())
非流式輸出
參數(shù)stream置為False即可。
多文檔
您可直接輸入多文檔內(nèi)容進行對話。受API調(diào)用請求大小所限,大量輸入文本內(nèi)容可能會受到限制(當前的報文長度可支持約1M tokens的content長度,超過限制時無法保證)。為保證您的對話體驗,我們建議您在需要基于大量文本內(nèi)容進行對話時,尤其是使用了多文檔場景時,盡量單獨上傳文檔,再根據(jù)文檔id進行對話。
流式輸出
stream 設(shè)置為True
from openai import OpenAI
client = OpenAI(
api_key="$your-dashscope-api-key", # 替換成真實DashScope的API_KEY
base_url="https://dashscope.aliyuncs.com/compatible-mode/v1", # 填寫DashScopeendpoint
)
completion = client.chat.completions.create(
model="qwen-long",
messages=[
{
'role': 'system',
'content': 'You are a helpful assistant.'
},
{
'role': 'system',
'content': '大型語言模型(llm)已經(jīng)徹底改變了人工智能領(lǐng)域,使以前被認為是人類獨有的自然語言處理任務成為可能...'
},
{
'role': 'system',
'content': '大型語言模型的訓練分為兩個階段:...'
},
{
'role': 'user',
'content': '這兩篇文章討論的方法有什么異同點?'
},
],
stream=True
)
for chunk in completion:
if chunk.choices[0].delta.content is not None:
print(chunk.choices[0].dict())
非流式輸出
參數(shù)stream置為False即可。
追加文檔
您可直接輸入多文檔內(nèi)容進行對話。受API調(diào)用請求大小所限,大量輸入文本內(nèi)容可能會受到限制(當前的報文長度可支持約1M tokens的content長度,超過限制時無法保證)。為保證您的對話體驗,我們建議您在需要基于大量文本內(nèi)容進行對話時,尤其是使用了多文檔場景時,盡量單獨上傳文檔,再根據(jù)文檔id進行對話。
流式輸出
stream 設(shè)置為True
from openai import OpenAI
client = OpenAI(
api_key="$your-dashscope-api-key", # 替換成真實DashScope的API_KEY
base_url="https://dashscope.aliyuncs.com/compatible-mode/v1", # 填寫DashScopeendpoint
)
completion = client.chat.completions.create(
model="qwen-long",
messages=[
{
'role': 'system',
'content': 'You are a helpful assistant.'
},
{
'role': 'system',
'content': '大型語言模型(llm)已經(jīng)徹底改變了人工智能領(lǐng)域,使以前被認為是人類獨有的自然語言處理任務成為可能...'
},
{
'role': 'user',
'content': '文章講了什么?'
},
{
'role': 'assistant',
'content': '這篇文章主題為大模型預訓練方法,主要內(nèi)容是...'
},
{
'role': 'system',
'content': '大型語言模型的訓練分為兩個階段:...'
},
{
'role': 'user',
'content': '這兩篇文章討論的方法有什么異同點?'
},
],
stream=True
)
for chunk in completion:
if chunk.choices[0].delta.content is not None:
print(chunk.choices[0].dict())
非流式輸出
參數(shù)stream置為False即可。
參數(shù)配置與OpenAI的接口參數(shù)對齊,當前已支持的參數(shù)如下,更多參數(shù)支持添加中,詳情可參考輸入?yún)?shù)配置
參數(shù) | 類型 | 默認值 | 說明 |
model | string | - | 當前模型為qwen-long |
messages | list | - |
|
top_p (可選) | float | - | 生成過程中的核采樣方法概率閾值,例如,取值為0.8時,僅保留概率加起來大于等于0.8的最可能token的最小集合作為候選集。取值范圍為(0,1.0),取值越大,生成的隨機性越高;取值越低,生成的確定性越高。 |
temperature(可選) | float | - | 用于控制模型回復的隨機性和多樣性。具體來說,temperature值控制了生成文本時對每個候選詞的概率分布進行平滑的程度。較高的temperature值會降低概率分布的峰值,使得更多的低概率詞被選擇,生成結(jié)果更加多樣化;而較低的temperature值則會增強概率分布的峰值,使得高概率詞更容易被選擇,生成結(jié)果更加確定。 取值范圍: [0, 2),不建議取值為0,無意義。 |
max_tokens(可選) | integer | 2000 | 指定模型可生成的最大token個數(shù)。例如模型最大輸出長度為2k,您可以設(shè)置為1k,防止模型輸出過長的內(nèi)容。 不同的模型有不同的輸出上限,例如qwen-max輸出上限為2k,qwen-plus輸出上限為8k。 |
stream (可選) | boolean | False | 用于控制是否使用流式輸出。當以stream模式輸出結(jié)果時,接口返回結(jié)果為generator,需要通過迭代獲取結(jié)果,默認每次輸出為當前生成的整個序列,最后一次輸出為最終全部生成結(jié)果。 |
stop (可選) | string or array | None | stop參數(shù)用于實現(xiàn)內(nèi)容生成過程的精確控制,在模型生成的內(nèi)容即將包含指定的字符串或token_id時自動停止。stop可以為string類型或array類型。
|
返回結(jié)果
非stream返回結(jié)果示例
{
'content': '文章探討了大型語言模型訓練的兩個階段:無監(jiān)督預訓練和大規(guī)模指令微調(diào)與強化學習,并提出了一種名為LIMA的語言模型,它是一個基于LLaMa的650億參數(shù)模型,僅通過1000個精心挑選的提示和響應進行標準監(jiān)督損失微調(diào),未涉及強化學習或人類偏好建模。LIMA展示了強大的性能,能從少數(shù)示例中學習到特定的響應格式,處理包括規(guī)劃旅行行程到推測替代歷史等復雜查詢,并能較好地泛化到未見過的任務上。\n\n通過對比實驗,發(fā)現(xiàn)LIMA在43%的情況下,其生成的回復與GPT-4相比要么等同要么更受歡迎;與Bard相比這一比例為58%,與經(jīng)過人類反饋訓練的DaVinci003相比更是高達65%。這些結(jié)果強烈表明,大型語言模型中的幾乎所有知識都是在預訓練階段學到的,而只需要有限的指令微調(diào)數(shù)據(jù)即可教會模型產(chǎn)生高質(zhì)量的輸出。\n\n此外,文章還涵蓋了關(guān)于時間旅行的虛構(gòu)故事創(chuàng)作、對古埃及文明的興趣描述、以及如何幫助聰明孩子交友的建議等內(nèi)容,展示了語言模型在多樣任務上的應用能力。同時,提到了一個關(guān)于營銷策略的計劃概要,以及美國總統(tǒng)拜登面臨的經(jīng)濟挑戰(zhàn)與就業(yè)市場分析。最后,還包含了有關(guān)回答質(zhì)量影響的測試、如何以喬治·卡林風格編寫單口相聲段子的示例,以及如何制作shakshuka食譜的指導。',
'role': 'assistant',
'function_call': None,
'tool_calls': None
}
stream返回結(jié)果
{'delta': {'content': '文章', 'function_call': None, 'role': 'assistant', 'tool_calls': None}, 'finish_reason': 'null', 'index': 0, 'logprobs': None}
{'delta': {'content': '主要', 'function_call': None, 'role': None, 'tool_calls': None}, 'finish_reason': 'null', 'index': 0, 'logprobs': None}
{'delta': {'content': '探討', 'function_call': None, 'role': None, 'tool_calls': None}, 'finish_reason': 'null', 'index': 0, 'logprobs': None}
{'delta': {'content': '了一種名為LIMA', 'function_call': None, 'role': None, 'tool_calls': None}, 'finish_reason': 'null', 'index': 0, 'logprobs': None}
{'delta': {'content': '的語言模型的訓練方法及其對齊', 'function_call': None, 'role': None, 'tool_calls': None}, 'finish_reason': 'null', 'index': 0, 'logprobs': None}
{'delta': {'content': '能力的評估。LIMA是一個擁有', 'function_call': None, 'role': None, 'tool_calls': None}, 'finish_reason': 'null', 'index': 0, 'logprobs': None}
{'delta': {'content': '650億參數(shù)的大型語言', 'function_call': None, 'role': None, 'tool_calls': None}, 'finish_reason': 'null', 'index': 0, 'logprobs': None}
{'delta': {'content': '模型,它僅通過在10', 'function_call': None, 'role': None, 'tool_calls': None}, 'finish_reason': 'null', 'index': 0, 'logprobs': None}
{'delta': {'content': '00個精心挑選的提示和', 'function_call': None, 'role': None, 'tool_calls': None}, 'finish_reason': 'null', 'index': 0, 'logprobs': None}
{'delta': {'content': '響應上進行標準監(jiān)督微調(diào)來', 'function_call': None, 'role': None, 'tool_calls': None}, 'finish_reason': 'null', 'index': 0, 'logprobs': None}
{'delta': {'content': '完成訓練,過程中并未采用強化學習', 'function_call': None, 'role': None, 'tool_calls': None}, 'finish_reason': 'null', 'index': 0, 'logprobs': None}
{'delta': {'content': '或人類偏好建模。研究結(jié)果顯示', 'function_call': None, 'role': None, 'tool_calls': None}, 'finish_reason': 'null', 'index': 0, 'logprobs': None}
{'delta': {'content': ',盡管訓練數(shù)據(jù)有限,LIMA', 'function_call': None, 'role': None, 'tool_calls': None}, 'finish_reason': 'null', 'index': 0, 'logprobs': None}
{'delta': {'content': '仍能展現(xiàn)出強大的性能,能夠從', 'function_call': None, 'role': None, 'tool_calls': None}, 'finish_reason': 'null', 'index': 0, 'logprobs': None}
{'delta': {'content': '少數(shù)示例中學習到特定的', 'function_call': None, 'role': None, 'tool_calls': None}, 'finish_reason': 'null', 'index': 0, 'logprobs': None}
{'delta': {'content': '響應格式,并泛化到未見過', 'function_call': None, 'role': None, 'tool_calls': None}, 'finish_reason': 'null', 'index': 0, 'logprobs': None}
{'delta': {'content': '的任務上。\n\n對比實驗表明,在控制', 'function_call': None, 'role': None, 'tool_calls': None}, 'finish_reason': 'null', 'index': 0, 'logprobs': None}
{'delta': {'content': '條件下,相對于GPT-4、', 'function_call': None, 'role': None, 'tool_calls': None}, 'finish_reason': 'null', 'index': 0, 'logprobs': None}
{'delta': {'content': 'Bard和DaVinci00', 'function_call': None, 'role': None, 'tool_calls': None}, 'finish_reason': 'null', 'index': 0, 'logprobs': None}
{'delta': {'content': '3等其他模型,人們更傾向于', 'function_call': None, 'role': None, 'tool_calls': None}, 'finish_reason': 'null', 'index': 0, 'logprobs': None}
{'delta': {'content': 'LIMA生成的回復,分別有', 'function_call': None, 'role': None, 'tool_calls': None}, 'finish_reason': 'null', 'index': 0, 'logprobs': None}
{'delta': {'content': '43%、58%和', 'function_call': None, 'role': None, 'tool_calls': None}, 'finish_reason': 'null', 'index': 0, 'logprobs': None}
{'delta': {'content': '65%的情況下認為LIMA的表現(xiàn)', 'function_call': None, 'role': None, 'tool_calls': None}, 'finish_reason': 'null', 'index': 0, 'logprobs': None}
{'delta': {'content': '更好或至少相當。這表明大型', 'function_call': None, 'role': None, 'tool_calls': None}, 'finish_reason': 'null', 'index': 0, 'logprobs': None}
{'delta': {'content': '語言模型在預訓練階段已經(jīng)學', 'function_call': None, 'role': None, 'tool_calls': None}, 'finish_reason': 'null', 'index': 0, 'logprobs': None}
{'delta': {'content': '到了大量的知識,而只需少量的', 'function_call': None, 'role': None, 'tool_calls': None}, 'finish_reason': 'null', 'index': 0, 'logprobs': None}
{'delta': {'content': '指令微調(diào)數(shù)據(jù)即可讓模型產(chǎn)生', 'function_call': None, 'role': None, 'tool_calls': None}, 'finish_reason': 'null', 'index': 0, 'logprobs': None}
{'delta': {'content': '高質(zhì)量的輸出,強調(diào)了“少', 'function_call': None, 'role': None, 'tool_calls': None}, 'finish_reason': 'null', 'index': 0, 'logprobs': None}
{'delta': {'content': '即是多”(Less is More)', 'function_call': None, 'role': None, 'tool_calls': None}, 'finish_reason': 'null', 'index': 0, 'logprobs': None}
{'delta': {'content': '的理念在模型對齊上的有效性。', 'function_call': None, 'role': None, 'tool_calls': None}, 'finish_reason': 'null', 'index': 0, 'logprobs': None}
{'delta': {'content': '\n\n此外,文章還提及了關(guān)于模型', 'function_call': None, 'role': None, 'tool_calls': None}, 'finish_reason': 'null', 'index': 0, 'logprobs': None}
{'delta': {'content': '輸出質(zhì)量的測試,以及使用不同', 'function_call': None, 'role': None, 'tool_calls': None}, 'finish_reason': 'null', 'index': 0, 'logprobs': None}
{'delta': {'content': '數(shù)量的示例進行微調(diào)時', 'function_call': None, 'role': None, 'tool_calls': None}, 'finish_reason': 'null', 'index': 0, 'logprobs': None}
{'delta': {'content': '模型穩(wěn)定性的觀察,進一步證明了', 'function_call': None, 'role': None, 'tool_calls': None}, 'finish_reason': 'null', 'index': 0, 'logprobs': None}
{'delta': {'content': '即使是小規(guī)模的、經(jīng)過篩選的數(shù)據(jù)', 'function_call': None, 'role': None, 'tool_calls': None}, 'finish_reason': 'null', 'index': 0, 'logprobs': None}
{'delta': {'content': '也能顯著提升模型性能。同時,', 'function_call': None, 'role': None, 'tool_calls': None}, 'finish_reason': 'null', 'index': 0, 'logprobs': None}
{'delta': {'content': '文中還包含了一些示例輸出,', 'function_call': None, 'role': None, 'tool_calls': None}, 'finish_reason': 'null', 'index': 0, 'logprobs': None}
{'delta': {'content': '如有關(guān)如何幫助聰明的孩子交友的', 'function_call': None, 'role': None, 'tool_calls': None}, 'finish_reason': 'null', 'index': 0, 'logprobs': None}
{'delta': {'content': '建議、模仿喬治·卡林風格', 'function_call': None, 'role': None, 'tool_calls': None}, 'finish_reason': 'null', 'index': 0, 'logprobs': None}
{'delta': {'content': '的單口相聲段子,以及', 'function_call': None, 'role': None, 'tool_calls': None}, 'finish_reason': 'null', 'index': 0, 'logprobs': None}
{'delta': {'content': '如何制作北非風味的shak', 'function_call': None, 'role': None, 'tool_calls': None}, 'finish_reason': 'null', 'index': 0, 'logprobs': None}
{'delta': {'content': 'shuka菜譜等,展示了模型', 'function_call': None, 'role': None, 'tool_calls': None}, 'finish_reason': 'null', 'index': 0, 'logprobs': None}
{'delta': {'content': '根據(jù)特定要求生成多樣化內(nèi)容的能力。', 'function_call': None, 'role': None, 'tool_calls': None}, 'finish_reason': 'null', 'index': 0, 'logprobs': None}
{'delta': {'content': '', 'function_call': None, 'role': None, 'tool_calls': None}, 'finish_reason': 'stop', 'index': 0, 'logprobs': None}
返回參數(shù)詳情可參考返回參數(shù)
通過HTTP接口調(diào)用
您可以通過HTTP接口來調(diào)用服務,獲得與通過HTTP接口調(diào)用OpenAI服務相同結(jié)構(gòu)的返回結(jié)果。
前提條件
已開通DashScope并獲得API-KEY:API-KEY的獲取與配置。
我們推薦您將API-KEY配置到環(huán)境變量中以降低API-KEY的泄漏風險,配置方法可參考通過環(huán)境變量配置API-KEY。您也可以在代碼中配置API-KEY,但是泄漏風險會提高。
提交接口調(diào)用
POST https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions
請求示例
以下示例展示通過CURL
命令來調(diào)用API的腳本。
需要使用您的API-KEY替換示例中的 $DASHSCOPE_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-long",
"messages": [
{
"role": "system",
"content": "You are a helpful assistant."
},
{
"role": "system",
"content": "fileid://file-fe-xxx"
},
{
"role": "user",
"content": "文章講了什么?"
}
]
}'
運行命令可得到以下結(jié)果:
{
"choices": [
{
"message": {
"role": "assistant",
"content": "文章主要探討了大型語言模型的訓練方法及其對齊(alignment)問題,特別是關(guān)于如何使這些模型更好地服務于最終任務和用戶偏好。研究通過一個名為LIMA的項目進行,該項目基于一個650億參數(shù)的LLaMa語言模型,僅使用精心挑選的1000個提示及其響應進行微調(diào),未采用強化學習或人類反饋直接指導。結(jié)果顯示,即使在如此有限的指令調(diào)整數(shù)據(jù)下,LIMA仍能展現(xiàn)出強勁的性能,能夠從少數(shù)示例中學習到特定的響應格式,并泛化到未見過的任務上。\n\n研究強調(diào)了預訓練階段對于模型獲取廣泛知識的重要性,表明幾乎所有知識都是在這一無監(jiān)督階段習得的,而后續(xù)的指令調(diào)優(yōu)僅需少量數(shù)據(jù)即可引導模型產(chǎn)生高質(zhì)量輸出。此外,文中還提到了一些實驗細節(jié),比如使用過濾與未過濾數(shù)據(jù)源訓練模型產(chǎn)生的質(zhì)量差異,以及模型在不同場景下的應用示例,如提供建議、編寫故事、諷刺喜劇等,進一步證明了模型的有效性和靈活性。"
},
"finish_reason": "stop",
"index": 0,
"logprobs": null
}
],
"object": "chat.completion",
"usage": {
"prompt_tokens": 0,
"completion_tokens": 0,
"total_tokens": 0
},
"created": 1715324557,
"system_fingerprint": "",
"model": "qwen-long",
"id": "chatcmpl-07b1b68992a091a08d7e239bd5a4a566"
}
流式輸出
如果您需要使用流式輸出,請在請求體中指定stream參數(shù)為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-long",
"messages": [
{
"role": "system",
"content": "You are a helpful assistant."
},
{
"role": "system",
"content": "fileid://file-fe-xxx"
},
{
"role": "user",
"content": "文章講了什么?"
}
],
"stream":true
}'
運行命令可得到以下結(jié)果:
data:{"choices":[{"delta":{"content":"文章","role":"assistant"},"finish_reason":"null","index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1715334042,"system_fingerprint":"","model":"qwen-long","id":"chatcmpl-35e3c387759692f98041ed0a5dd9b72a"}
data:{"choices":[{"delta":{"content":"探討"},"finish_reason":"null","index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1715334042,"system_fingerprint":"","model":"qwen-long","id":"chatcmpl-35e3c387759692f98041ed0a5dd9b72a"}
data:{"choices":[{"delta":{"content":"了"},"finish_reason":"null","index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1715334042,"system_fingerprint":"","model":"qwen-long","id":"chatcmpl-35e3c387759692f98041ed0a5dd9b72a"}
data:{"choices":[{"delta":{"content":"大型語言模型的訓練"},"finish_reason":"null","index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1715334042,"system_fingerprint":"","model":"qwen-long","id":"chatcmpl-35e3c387759692f98041ed0a5dd9b72a"}
data:{"choices":[{"delta":{"content":"方法及其對齊(alignment)的重要性"},"finish_reason":"null","index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1715334042,"system_fingerprint":"","model":"qwen-long","id":"chatcmpl-35e3c387759692f98041ed0a5dd9b72a"}
data:{"choices":[{"delta":{"content":",主要分為兩個階段:無監(jiān)督"},"finish_reason":"null","index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1715334042,"system_fingerprint":"","model":"qwen-long","id":"chatcmpl-35e3c387759692f98041ed0a5dd9b72a"}
data:{"choices":[{"delta":{"content":"預訓練和大規(guī)模指令微調(diào)及"},"finish_reason":"null","index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1715334042,"system_fingerprint":"","model":"qwen-long","id":"chatcmpl-35e3c387759692f98041ed0a5dd9b72a"}
data:{"choices":[{"delta":{"content":"強化學習。研究通過一個名為L"},"finish_reason":"null","index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1715334042,"system_fingerprint":"","model":"qwen-long","id":"chatcmpl-35e3c387759692f98041ed0a5dd9b72a"}
data:{"choices":[{"delta":{"content":"IMA的650億參數(shù)語言"},"finish_reason":"null","index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1715334042,"system_fingerprint":"","model":"qwen-long","id":"chatcmpl-35e3c387759692f98041ed0a5dd9b72a"}
data:{"choices":[{"delta":{"content":"模型實驗,該模型僅使用精心"},"finish_reason":"null","index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1715334042,"system_fingerprint":"","model":"qwen-long","id":"chatcmpl-35e3c387759692f98041ed0a5dd9b72a"}
data:{"choices":[{"delta":{"content":"挑選的1000個提示"},"finish_reason":"null","index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1715334042,"system_fingerprint":"","model":"qwen-long","id":"chatcmpl-35e3c387759692f98041ed0a5dd9b72a"}
data:{"choices":[{"delta":{"content":"及其響應進行標準監(jiān)督損失微調(diào)"},"finish_reason":"null","index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1715334042,"system_fingerprint":"","model":"qwen-long","id":"chatcmpl-35e3c387759692f98041ed0a5dd9b72a"}
data:{"choices":[{"delta":{"content":",未采用任何強化學習或人類"},"finish_reason":"null","index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1715334042,"system_fingerprint":"","model":"qwen-long","id":"chatcmpl-35e3c387759692f98041ed0a5dd9b72a"}
data:{"choices":[{"delta":{"content":"偏好建模。LIMA展現(xiàn)出強大的"},"finish_reason":"null","index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1715334042,"system_fingerprint":"","model":"qwen-long","id":"chatcmpl-35e3c387759692f98041ed0a5dd9b72a"}
data:{"choices":[{"delta":{"content":"性能,能從少數(shù)示例中"},"finish_reason":"null","index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1715334042,"system_fingerprint":"","model":"qwen-long","id":"chatcmpl-35e3c387759692f98041ed0a5dd9b72a"}
data:{"choices":[{"delta":{"content":"學習到特定的回答格式,處理復雜"},"finish_reason":"null","index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1715334042,"system_fingerprint":"","model":"qwen-long","id":"chatcmpl-35e3c387759692f98041ed0a5dd9b72a"}
data:{"choices":[{"delta":{"content":"查詢,并且在未見過的任務上"},"finish_reason":"null","index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1715334042,"system_fingerprint":"","model":"qwen-long","id":"chatcmpl-35e3c387759692f98041ed0a5dd9b72a"}
data:{"choices":[{"delta":{"content":"表現(xiàn)出了良好的泛化能力。\n\n對比"},"finish_reason":"null","index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1715334042,"system_fingerprint":"","model":"qwen-long","id":"chatcmpl-35e3c387759692f98041ed0a5dd9b72a"}
data:{"choices":[{"delta":{"content":"人類評估顯示,相比GPT-"},"finish_reason":"null","index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1715334042,"system_fingerprint":"","model":"qwen-long","id":"chatcmpl-35e3c387759692f98041ed0a5dd9b72a"}
data:{"choices":[{"delta":{"content":"4、Bard和DaVinci"},"finish_reason":"null","index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1715334042,"system_fingerprint":"","model":"qwen-long","id":"chatcmpl-35e3c387759692f98041ed0a5dd9b72a"}
data:{"choices":[{"delta":{"content":"003(后者經(jīng)過人類反饋"},"finish_reason":"null","index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1715334042,"system_fingerprint":"","model":"qwen-long","id":"chatcmpl-35e3c387759692f98041ed0a5dd9b72a"}
data:{"choices":[{"delta":{"content":"訓練),參與者更偏好或認為L"},"finish_reason":"null","index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1715334042,"system_fingerprint":"","model":"qwen-long","id":"chatcmpl-35e3c387759692f98041ed0a5dd9b72a"}
data:{"choices":[{"delta":{"content":"IMA的回答等同的比例分別達到了4"},"finish_reason":"null","index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1715334042,"system_fingerprint":"","model":"qwen-long","id":"chatcmpl-35e3c387759692f98041ed0a5dd9b72a"}
data:{"choices":[{"delta":{"content":"3%、58%和6"},"finish_reason":"null","index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1715334042,"system_fingerprint":"","model":"qwen-long","id":"chatcmpl-35e3c387759692f98041ed0a5dd9b72a"}
data:{"choices":[{"delta":{"content":"5%。這表明大型語言模型"},"finish_reason":"null","index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1715334042,"system_fingerprint":"","model":"qwen-long","id":"chatcmpl-35e3c387759692f98041ed0a5dd9b72a"}
data:{"choices":[{"delta":{"content":"在預訓練階段幾乎學到了所有"},"finish_reason":"null","index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1715334042,"system_fingerprint":"","model":"qwen-long","id":"chatcmpl-35e3c387759692f98041ed0a5dd9b72a"}
data:{"choices":[{"delta":{"content":"知識,只需有限的指令微調(diào)"},"finish_reason":"null","index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1715334042,"system_fingerprint":"","model":"qwen-long","id":"chatcmpl-35e3c387759692f98041ed0a5dd9b72a"}
data:{"choices":[{"delta":{"content":"數(shù)據(jù)即可產(chǎn)生高質(zhì)量輸出。此外,"},"finish_reason":"null","index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1715334042,"system_fingerprint":"","model":"qwen-long","id":"chatcmpl-35e3c387759692f98041ed0a5dd9b72a"}
data:{"choices":[{"delta":{"content":"文中還提到了質(zhì)量對模型輸出"},"finish_reason":"null","index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1715334042,"system_fingerprint":"","model":"qwen-long","id":"chatcmpl-35e3c387759692f98041ed0a5dd9b72a"}
data:{"choices":[{"delta":{"content":"的影響,以及不同類型的生成任務示"},"finish_reason":"null","index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1715334042,"system_fingerprint":"","model":"qwen-long","id":"chatcmpl-35e3c387759692f98041ed0a5dd9b72a"}
data:{"choices":[{"delta":{"content":"例,如關(guān)于育兒建議、模仿"},"finish_reason":"null","index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1715334042,"system_fingerprint":"","model":"qwen-long","id":"chatcmpl-35e3c387759692f98041ed0a5dd9b72a"}
data:{"choices":[{"delta":{"content":"喬治·卡林風格的單口"},"finish_reason":"null","index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1715334042,"system_fingerprint":"","model":"qwen-long","id":"chatcmpl-35e3c387759692f98041ed0a5dd9b72a"}
data:{"choices":[{"delta":{"content":"相聲、處理職場情感問題的建議"},"finish_reason":"null","index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1715334042,"system_fingerprint":"","model":"qwen-long","id":"chatcmpl-35e3c387759692f98041ed0a5dd9b72a"}
data:{"choices":[{"delta":{"content":"和制作北非風味菜肴shak"},"finish_reason":"null","index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1715334042,"system_fingerprint":"","model":"qwen-long","id":"chatcmpl-35e3c387759692f98041ed0a5dd9b72a"}
data:{"choices":[{"delta":{"content":"shuka的食譜。這些示"},"finish_reason":"null","index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1715334042,"system_fingerprint":"","model":"qwen-long","id":"chatcmpl-35e3c387759692f98041ed0a5dd9b72a"}
data:{"choices":[{"delta":{"content":"例進一步證明了模型在多樣任務"},"finish_reason":"null","index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1715334042,"system_fingerprint":"","model":"qwen-long","id":"chatcmpl-35e3c387759692f98041ed0a5dd9b72a"}
data:{"choices":[{"delta":{"content":"中的應用潛力與靈活性。"},"finish_reason":"null","index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1715334042,"system_fingerprint":"","model":"qwen-long","id":"chatcmpl-35e3c387759692f98041ed0a5dd9b72a"}
data:{"choices":[{"delta":{"content":""},"finish_reason":"stop","index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1715334042,"system_fingerprint":"","model":"qwen-long","id":"chatcmpl-35e3c387759692f98041ed0a5dd9b72a"}
異常響應示例
在訪問請求出錯的情況下,輸出的結(jié)果中會通過 code 和 message 指明出錯原因。
{
"error": {
"message": "Incorrect API key provided. ",
"type": "invalid_request_error",
"param": null,
"code": "invalid_api_key"
}
}
狀態(tài)碼說明
錯誤碼 | 說明 |
400 - Invalid file [id:xxx]. | 提供的文件id存在問題 |
400 - Too many files provided. | 提供的對話文檔數(shù)量大于等于100 |
400 - File [id:xxx] cannot be found. | 輸入的文件已經(jīng)被刪除 |
400 - File [id:xxx] exceeds size limit. | 文檔大小超限 |
400 - File [id:xxx] exceeds page limits (15000 pages). | 文檔頁數(shù)超限 |
400 - Multiple types of file system prompt detected, please do not mix file-id and text content in one request. | 輸入的文件中包含了file id 和文件內(nèi)容兩種方式,當前暫不支持兩種方式混用 |
400 - File [id:xxx] format is not supported. | 文檔格式不支持 |
400 - File [id:xxx] content blank. | 文檔內(nèi)容為空 |
400 - Total message token length exceed model limit (10000000 tokens). | 輸入的messages 總token數(shù)超過了10M |
400 - Single round file-content exceeds token limit, please use fileid to supply lengthy input. | 輸入的單條message token數(shù)超過了9K |
400 - Role specification invalid, please refer to API documentation for usage. | messages組裝格式存在問題,請參考上述參數(shù)描述與示例代碼進行參考 |
400 - File parsing in progress, please try again later. | 文檔解析中,請稍后再試 |
400 - Input data may contain inappropriate content. | 數(shù)據(jù)檢查錯誤,輸入包含疑似敏感內(nèi)容被綠網(wǎng)攔截 |
429 - You exceeded your current requests list. | 您超出了對模型訪問的限流值,請稍后再試 |
500 - File [id:xxx] parsing error. | 文檔解析失敗 |
500 - File [id:xxx] prasing timeout. | 文檔解析超時 |
500 - Preprocessor error. | 大模型前處理錯誤 |
500 - Postprocessor error. | 大模型后處理錯誤 |
500 - File content conversion error. | 文檔message處理錯誤 |
500 - An unspecified internal error has occured. | 調(diào)用大模型出現(xiàn)異常 |
500 - Response timeout. | 處理超時,可嘗試重試 |
503 - The engine is currently overloaded, please try again later. | 服務端負載過高,可重試 |