Qwen Character API
阿里巴巴 Qwen Character 角色扮演模型的完整 API 參考。Qwen Character 模型使用 Flaq AI chat completions 端點,但也支援長期記憶、群聊模擬和 assistant 部分續寫等角色專用欄位。
模型變體
此 API 支援兩種模型變體:
快速比較
角色扮演如何運作
單角色扮演可使用一般的 system、user 和 assistant 訊息。將目前請求的穩定人設指令放在 system 訊息中。啟用長期記憶時,將可重用的角色設定放在 profile。
對於群聊模擬,Qwen Character 不會自動從 profile 推斷說話人名稱。呼叫方必須在每則訊息 content 的起始位置手動寫入說話人名稱,例如 Cheng Yi: 或 Ling Lu:。若要讓模型以特定角色繼續回覆,請追加最後一則 assistant 訊息,將 content 設為目標說話人的前綴,並設定 partial: true。
partial 是最後一則 assistant 訊息上的訊息層級欄位。它不是頂層請求參數。
Qwen Plus Character
端點
POST /api/v1/chat/completions
請求參數
必需
標頭
訊息支援
訊息欄位
可選
啟用長期記憶時,你必須提供:
x-session 請求標頭
- 請求主體中的
profile
enable_long_term_memory: true
skip_save_types 支援以下值:
基本角色扮演請求
const response = await fetch('https://api.flaq.ai/api/v1/chat/completions', {
method: 'POST',
headers: {
'Authorization': 'Bearer YOUR_API_KEY',
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
model: 'qwen-plus-character',
stream: false,
max_tokens: 1200,
temperature: 0.8,
messages: [
{
role: 'system',
content: 'You are Jiang Rang, a witty high-school Go prodigy. Stay playful, warm, and concise.'
},
{
role: 'user',
content: 'Why are you reading Go records outside the milk tea shop again?'
}
]
})
});
長期記憶請求
const response = await fetch('https://api.flaq.ai/api/v1/chat/completions', {
method: 'POST',
headers: {
'Authorization': 'Bearer YOUR_API_KEY',
'Accept': 'text/event-stream',
'Content-Type': 'application/json',
'x-session': 'your-session-id'
},
body: JSON.stringify({
model: 'qwen-plus-character',
messages: [
{
role: 'system',
content: 'You are a witty high-school student who speaks with humor and warmth.'
},
{
role: 'user',
content: 'Remember that I prefer lightly sweet milk tea.'
}
],
stream: true,
max_tokens: 1200,
temperature: 0.8,
seed: 12345,
profile: 'You are a witty high-school student who remembers user preferences and stays in character.',
enable_long_term_memory: true,
memory_entries: 50,
skip_save_types: []
})
});
使用 partial 進行群聊模擬
群聊模擬透過訊息文字實現。API 不會自動指派或遷移說話人名稱。請在 content 開頭手動寫入每個說話人名稱,然後追加最後一則 assistant 訊息,帶上目標說話人的前綴和 partial: true。
const response = await fetch('https://api.flaq.ai/api/v1/chat/completions', {
method: 'POST',
headers: {
'Authorization': 'Bearer YOUR_API_KEY',
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
model: 'qwen-plus-character',
stream: false,
max_tokens: 800,
temperature: 0.8,
messages: [
{
role: 'system',
content: 'In a musician group chat, Ling Lu is a 25-year-old gifted musician known for sharp humor and a regional accent. Cheng Yi is his music partner, Tao Le is his partner, and Ma Hui is a friend. Keep Ling Lu sarcastic but caring. Reply in one short message.'
},
{
role: 'user',
content: 'Cheng Yi: Are you free this weekend? I want feedback on the new song.'
},
{
role: 'user',
content: 'Tao Le: He is right, but do not stay up too late editing.'
},
{
role: 'user',
content: 'Ma Hui: Let us grab dinner and talk. My treat.'
},
{
role: 'assistant',
content: 'Ling Lu:',
partial: true
}
]
})
});
使用 partial 以角色身份繼續
當使用者尚未回覆,但你希望角色繼續說話時,也可使用相同的 partial 模式。加入最後一則 assistant 訊息,內容為角色前綴。
{
model: 'qwen-plus-character',
stream: false,
messages: [
{
role: 'system',
content: 'You are Jiang Rang, a high-school Go prodigy. Speak with humor and warmth.'
},
{
role: 'assistant',
content: 'Class monitor, what are you doing?'
},
{
role: 'assistant',
content: '(He waves at you.) Did being class monitor make you forget how to answer me?'
},
{
role: 'assistant',
content: 'Jiang Rang:',
partial: true
}
]
}
Qwen Flash Character
請求參數
協定與 Qwen Plus Character 相同,差異如下:
請求範例
{
model: 'qwen-flash-character',
messages: [
{
role: 'system',
content: 'You are a calm library assistant who speaks briefly and vividly.'
},
{
role: 'user',
content: 'Introduce yourself in three sentences.'
}
],
stream: true,
max_tokens: 800,
temperature: 0.7,
seed: 12345
}
回應格式
Qwen Character LLM 模型會傳回與 OpenAI 相容的補全回應。使用 stream: true 時,回應為伺服器傳送事件(SSE);使用 stream: false 時,回應為單一 JSON 物件。
最佳實務
- 將請求層級的人設指令放在
system 訊息中。
- 只有在啟用長期記憶時,才將可重用的角色背景放在
profile。
- 啟用長期記憶時,請在相關對話中重用相同的
x-session 值。365 天未使用的 session 可能會被清除。
- 有用時繼續傳送最近的對話訊息;長期記憶和短期上下文可以一起使用。
- 對於群聊模擬,請在
content 中手動寫入說話人名稱。API 不會從 profile 推斷名稱。
- 只有當模型應從某個前綴繼續時,才在最後一則 assistant 訊息中使用
partial: true。
- 使用 Qwen Flash Character 處理輕量級角色扮演,使用 Qwen Plus Character 取得更豐富的對話品質。