สร้างข้อความแชท
ส่งคำขอ ShareAI chat-completions ด้วย Bearer API key และทำความเข้าใจเกี่ยวกับโมเดลที่รองรับ ข้อความ และฟิลด์สตรีม.
ในหน้านี้
https://api.shareai.now/api/v1/chat/completionsเรียกใช้คำขอแชทโดยใช้โมเดลที่เลือก.
- Base URL → URL พื้นฐาน
https://api.shareai.now- การตรวจสอบสิทธิ์
- Bearer API key → คีย์ API แบบ Bearer
นี่คือเส้นทางการรวมค่าเริ่มต้นสำหรับคำขอที่ชำระเงินและได้รับอนุญาตโดยพื้นที่ทำงาน API-key ของคุณ ข้อจำกัดของโมเดลและผู้ให้บริการของคีย์จะมีผลต่อการจัดสรร รวมถึงเส้นทางสำรองที่อนุญาต.
เนื้อหาคำขอ#
| ฟิลด์ | ประเภท | ความหมาย |
|---|---|---|
model | string, จำเป็น | ตัวระบุโมเดลที่มีอยู่จริง คัดลอกจาก Models. |
messages | array → อาร์เรย์ | ข้อความสนทนาที่มีบทบาทและเนื้อหา. |
stream | boolean → บูลีน | 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.
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 status จะเป็น 200 ผลลัพธ์ไม่มีอุปกรณ์ในปัจจุบันใช้ error.code = no_device พร้อม HTTP 200.
ข้อจำกัดของโมเดลและผู้ให้บริการ#
คีย์อาจถูกจำกัดเฉพาะโมเดลและผู้ให้บริการการดำเนินการบางราย หากการเข้าถูกปฏิเสธ ให้ตรวจสอบ ข้อมูลคีย์ และอัปเดตคีย์ใน Console เฉพาะเมื่อพื้นที่ทำงานที่เป็นเจ้าของตั้งใจที่จะให้การเข้าถึงนั้น.
การสตรีมหรือการอนุญาตของลูกค้า#
อัปเดตล่าสุด กันยายน 15, 2026