創建一個聊天完成
用 Bearer API 密鑰發送 ShareAI 聊天完成請求,並了解支持嘅模型、消息同流字段。
喺呢頁面
POST
https://api.shareai.now/api/v1/chat/completions使用所選模型運行聊天請求。
- 基礎 URL
https://api.shareai.now- 認證
- Bearer API 密鑰
呢係默認嘅集成路徑,用於由你嘅 API 密鑰工作區支付同授權嘅請求。密鑰嘅模型同供應商限制適用於分配,包括允許嘅後備路徑。
請求正文#
| 字段 | 類型 | 意思 |
|---|---|---|
model | 字符串,必需 | 精確可用嘅模型標識符。喺 Models 中複製佢。 |
messages | 陣列 | 包含角色同內容嘅對話消息。 |
stream | 布爾值 | false 返回一個 JSON 結果;true 請求服務器發送事件。 |
使用文本消息內容。發送例如 system, user 同埋 assistant 適合對話嘅角色。包括你希望模型使用嘅對話歷史;唔好假設請求共享存儲嘅對話。
cURL
curl --fail-with-body --request POST \
"https://api.shareai.now/api/v1/chat/completions" \
-H "Authorization: Bearer $SHAREAI_API_KEY" \
-H "Content-Type: application/json" \
--data '{
"model": "MODEL_IDENTIFIER",
"messages": [
{
"role": "system",
"content": "Be concise and helpful."
},
{
"role": "user",
"content": "Give me three ideas for a local community event."
}
],
"stream": false
}'
Python
import json
import os
import urllib.request
headers = {"Authorization": "Bearer " + os.environ["SHAREAI_API_KEY"]}
headers["Content-Type"] = "application/json"
payload = {'model': 'MODEL_IDENTIFIER', 'messages': [{'role': 'system', 'content': 'Be concise and helpful.'}, {'role': 'user', 'content': 'Give me three ideas for a local community event.'}], 'stream': False}
data = json.dumps(payload).encode()
request = urllib.request.Request('https://api.shareai.now/api/v1/chat/completions', data=data, headers=headers, method='POST')
with urllib.request.urlopen(request, timeout=60) as response:
print(json.load(response))
TypeScript
const url = "https://api.shareai.now/api/v1/chat/completions";
const headers: Record = {
Authorization: `Bearer ${process.env.SHAREAI_API_KEY}`,
};
headers["Content-Type"] = "application/json";
const payload = {
"model": "MODEL_IDENTIFIER",
"messages": [
{
"role": "system",
"content": "Be concise and helpful."
},
{
"role": "user",
"content": "Give me three ideas for a local community event."
}
],
"stream": false
};
const response = await fetch(url, { method: "POST", headers, body: JSON.stringify(payload) });
if (!response.ok) throw new Error(`${response.status}: ${await response.text()}`);
console.log(await response.json());
回應#
完成嘅非流式結果設置 done 為 true,並喺 message.content. 返回生成嘅文本。頂層仲攜帶 done_reason 同埋 consumption_type。流式傳輸使用唔同嘅 chat-completion-chunk 格式。唔好假設非流式結果有 OpenAI 風格嘅 choices 陣列。
JSON
{
"done": true,
"done_reason": "stop",
"consumption_type": "credits",
"message": {
"role": "assistant",
"content": "Here are three ideas for your community event.",
"done": true,
"done_reason": "stop"
}
}
呢係一個示例完成結果;consumption_type 描述實際分配模式。即使 HTTP 狀態係 200,都要檢查頂層錯誤。無設備結果目前使用 error.code = no_device 同 HTTP 200。
模型同供應商限制#
密鑰可能限於特定模型同執行供應商。如果訪問被拒,請檢查 密鑰信息 並只喺擁有工作區打算授予訪問權限時喺控制台更新密鑰。
流式傳輸或者客戶授權#
最後更新日期:2026年9月15日