completions 介面
Completions 介面專為文本補全情境設計,適合代碼補全、內容續寫等情境。
本文檔僅適用於中國內地(北京地區),需使用中國(北京)地區的API Key。
支援的模型
當前支援 Qwen Coder 部分模型:
qwen-coder-turbo
前提條件
您需要已擷取API Key並配置API Key到環境變數。如果通過 OpenAI SDK 調用,需要安裝SDK。
開始使用
您可以通過 Completions 介面實現文本補全,當前支援以下兩種文本補全情境:
-
通過給定的首碼產生後續內容;
-
通過給定的首碼與尾碼產生中間內容;
暫不支援通過給定的尾碼產生首碼內容。
快速開始
您可以在首碼中傳入函數的名稱、輸入參數、使用說明等資訊,Completions 介面將返回產生的程式碼。
提示詞模板為:
<|fim_prefix|>{prefix_content}<|fim_suffix|>
其中{prefix_content}是您需要傳入的首碼資訊。
import os
from openai import OpenAI
client = OpenAI(
base_url="https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1",
api_key=os.getenv("DASHSCOPE_API_KEY")
)
completion = client.completions.create(
model="qwen-coder-turbo",
prompt="<|fim_prefix|>寫一個python的快速排序函數,def quick_sort(arr):<|fim_suffix|>",
)
print(completion.choices[0].text)import OpenAI from "openai";
const openai = new OpenAI(
{
// 若沒有配置環境變數,請用阿里雲百鍊API Key將下行替換為:apiKey: "sk-xxx",
apiKey: process.env.DASHSCOPE_API_KEY,
baseURL: "https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1"
}
);
async function main() {
const completion = await openai.completions.create({
model: "qwen-coder-turbo",
prompt: "<|fim_prefix|>寫一個python的快速排序函數,def quick_sort(arr):<|fim_suffix|>",
});
console.log(completion.choices[0].text)
}
main();curl -X POST https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1/completions \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "qwen-coder-turbo",
"prompt": "<|fim_prefix|>寫一個python的快速排序函數,def quick_sort(arr):<|fim_suffix|>"
}'根據首碼和尾碼產生中間內容
Completions 介面支援通過您給定的首碼與尾碼產生中間內容,您可以在首碼中傳入函數的名稱、輸入參數、使用說明等資訊,在尾碼中傳入函數的返回參數等資訊,Completions 介面將返回產生的程式碼。
提示詞模板為:
<|fim_prefix|>{prefix_content}<|fim_suffix|>{suffix_content}<|fim_middle|>
其中{prefix_content}是您需要傳入的首碼資訊,{suffix_content}為您需要傳入的尾碼資訊。
import os
from openai import OpenAI
client = OpenAI(
base_url="https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1",
api_key=os.getenv("DASHSCOPE_API_KEY")
)
prefix_content = f"""def reverse_words_with_special_chars(s):
'''
反轉字串中的每個單詞(保留非字母字元的位置),並保持單詞順序。
樣本:
reverse_words_with_special_chars("Hello, world!") -> "olleH, dlrow!"
參數:
s (str): 輸入字串(可能包含標點符號)
返回:
str: 處理後的字串,單詞反轉但非字母字元位置不變
'''
"""
suffix_content = "return result"
completion = client.completions.create(
model="qwen-coder-turbo",
prompt=f"<|fim_prefix|>{prefix_content}<|fim_suffix|>{suffix_content}<|fim_middle|>",
)
print(completion.choices[0].text)import OpenAI from 'openai';
const client = new OpenAI({
baseURL: "https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1",
apiKey: process.env.DASHSCOPE_API_KEY
});
const prefixContent = `def reverse_words_with_special_chars(s):
'''
反轉字串中的每個單詞(保留非字母字元的位置),並保持單詞順序。
樣本:
reverse_words_with_special_chars("Hello, world!") -> "olleH, dlrow!"
參數:
s (str): 輸入字串(可能包含標點符號)
返回:
str: 處理後的字串,單詞反轉但非字母字元位置不變
'''
`;
const suffixContent = "return result";
async function main() {
const completion = await client.completions.create({
model: "qwen-coder-turbo",
prompt: `<|fim_prefix|>${prefixContent}<|fim_suffix|>${suffixContent}<|fim_middle|>`
});
console.log(completion.choices[0].text);
}
main();curl -X POST https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1/completions \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "qwen-coder-turbo",
"prompt": "<|fim_prefix|>def reverse_words_with_special_chars(s):\n\"\"\"\n反轉字串中的每個單詞(保留非字母字元的位置),並保持單詞順序。\n 樣本:\n reverse_words_with_special_chars(\"Hello, world!\") -> \"olleH, dlrow!\"\n 參數:\n s (str): 輸入字串(可能包含標點符號)\n 返回:\n str: 處理後的字串,單詞反轉但非字母字元位置不變\n\"\"\"\n<|fim_suffix|>return result<|fim_middle|>"
}'輸入與輸出參數
輸入參數
|
參數 |
類型 |
必選 |
說明 |
|
model |
string |
是 |
調用的模型名稱。 |
|
prompt |
string |
是 |
要產生補全的提示。 |
|
max_tokens |
integer |
否 |
本次請求返回的最大 Token 數。
|
|
temperature |
float |
否 |
採樣溫度,控制模型產生文本的多樣性。 temperature越高,產生的文本更多樣,反之,產生的文本更確定。 取值範圍: [0, 2.0)。 由於temperature與top_p均可以控制產生文本的多樣性,因此建議您只設定其中一個值。 |
|
top_p |
float |
否 |
核採樣的機率閾值,控制模型產生文本的多樣性。 top_p越高,產生的文本更多樣。反之,產生的文本更確定。 取值範圍:(0,1.0] 由於temperature與top_p均可以控制產生文本的多樣性,因此建議您只設定其中一個值。 |
|
stream |
boolean |
否 |
是否流式輸出回複。參數值:
|
|
stream_options |
object |
否 |
當啟用流式輸出時,可通過將本參數設定為 |
|
stop |
string 或 array |
否 |
當模型產生的文本即將包含stop參數中指定的字串或 您可以在stop參數中傳入敏感詞來控制模型的輸出。 |
|
seed |
integer |
否 |
設定seed參數會使文本產生過程更具有確定性,通常用於使模型每次啟動並執行結果一致。 在每次模型調用時傳入相同的seed值(由您指定),並保持其他參數不變,模型將儘可能返回相同的結果。 取值範圍:0到231−1。 |
|
presence_penalty |
float |
否 |
控制模型產生文本時的內容重複度。 取值範圍:[-2.0, 2.0]。正數會減少重複度,負數會增加重複度。 |
輸出參數
|
參數 |
類型 |
說明 |
|
id |
string |
本次調用的唯一識別碼。 |
|
choices |
array |
模型產生內容的數組。 |
|
choices[0].text |
string |
本次請求產生的內容。 |
|
choices[0].finish_reason |
string |
模型停止產生的原因。 |
|
choices[0].index |
integer |
當前元素在數組中的索引,固定為0。 |
|
choices[0].logprobs |
object |
當前固定為 |
|
created |
integer |
本次請求被建立時的時間戳記。 |
|
model |
string |
本次請求使用的模型名稱。 |
|
system_fingerprint |
string |
該參數當前固定為 |
|
object |
string |
物件類型,始終為 |
|
usage |
object |
本次請求的使用統計資訊。 |
|
usage.prompt_tokens |
integer |
|
|
usage.completion_tokens |
integer |
|
|
usage.total_tokens |
integer |
|
錯誤碼
如果模型調用失敗並返回報錯資訊,請參見錯誤碼進行解決。