/v1/chat/completionsปลายทางนี้เข้ากันได้กับ OpenAI หากคุณมี OpenAI client ให้ชี้ base URL มาที่ Grid ก็ใช้งานได้ คีย์ระบุโปรเจกต์อยู่แล้ว จึงไม่ต้องส่งเฮดเดอร์เพิ่ม:
from openai import OpenAI
client = OpenAI(
api_key="aig_...",
base_url="https://api.aigridapp.com/v1",
)
resp = client.chat.completions.create(
model="openai/gpt-oss-20b", # get real ids from GET /v1/models
messages=[{"role": "user", "content": "Summarise this in one line."}],
)ฟิลด์คำขอ#
ต้องส่ง model และ messages ส่วนฟิลด์อื่นเป็นตัวเลือก
| ฟิลด์ | ชนิด | หมายเหตุ |
|---|---|---|
model |
string | id จาก /v1/models — product หรือ sandbox product ที่ใช้กับแชตได้ |
messages |
array | 1–100 ข้อความ; role คือ system, user, assistant, tool |
stream |
bool | ดู การสตรีม |
stream_options |
object | |
max_tokens |
integer | 0–8192 และต้องไม่เกิน max_output_tokens ของโมเดลนั้นเอง |
temperature |
number | 0–2 |
top_p |
number | มากกว่า 0 และไม่เกิน 1 |
n |
integer | ต้องเป็น 1 — คิดราคาหนึ่ง completion ต่อคำขอ |
seed |
integer | |
stop |
string หรือ array | ได้สูงสุด 4 sequence แต่ละรายการยาว 1–64 อักขระ |
tools |
array | ได้สูงสุด 64 tool definition รวมไม่เกิน 32 KB |
tool_choice |
string หรือ object | ไม่เกิน 2 KB |
response_format |
object | ไม่เกิน 8 KB |
project_id |
string | ระบุซ้ำได้เฉพาะโปรเจกต์ที่คีย์ผูกไว้ |
รายละเอียด message: message ที่เป็น tool ต้องมี tool_call_id ที่ตอบกลับ ส่วน assistant อาจมี tool_calls และสำหรับผู้ให้บริการที่ส่งกลับ อาจมี reasoning history (reasoning_content / reasoning)
ขีดจำกัด messages รวมกับ forwarded parameter ทั้งหมดต้องไม่เกิน 128,000 bytes และคำขอทั้งหมดต้องอยู่ภายใน context และ output limit ที่โมเดลเผยแพร่ (422 context_limit หากเกิน) forwarded field นับรวมในขีดจำกัดเดียวกัน ดังนั้น tool schema ขนาดใหญ่ไม่อาจทำให้ยอดสุดท้ายเกินเงินที่กันไว้
สิ่งที่ model ระบุได้#
- published product ที่กำหนดให้โปรเจกต์ของคุณ — กรณีปกติ
- chat-compatible sandbox product ของโปรเจกต์: sandbox flow ที่ deploy แล้วและมี contract เป็นข้อความเข้าหนึ่งรายการ/ข้อความออกหนึ่งรายการ การตอบกลับเป็น
chat.completionปกติ โดยฟิลด์aigridมี sandbox execution id โปรเจกต์ และcharged_micro; เมื่อใช้stream: trueข้อความที่เสร็จจะมาถึงเป็นหนึ่ง chunk sandbox product ที่มี contract อื่นตอบ422 sandbox_contractให้เรียกผ่าน/v1/sandbox/{id}/invokeแทน
ระบบตรวจ eligibility ซ้ำภายใน admission transaction: id ที่โปรเจกต์เรียกไม่ได้ ในขณะนี้ เช่น draft, deployment ที่ถูก suspend, assignment ที่ถูกลบ หรือ resource ของโปรเจกต์อื่น จะเป็น 404 model_not_found; resource ที่เรียกได้แต่ถูกตัดออกโดย allowlist ของคีย์จะเป็น 403 key_scope
Idempotency#
Idempotency-Key เป็น ตัวเลือก สำหรับปลายทางนี้ ยาว 8–128 อักขระ การส่ง key เดิมซ้ำจะ replay คำตอบที่บันทึกไว้โดยไม่กันเงินครั้งที่สอง หากไม่ส่ง ระบบรับเป็น invocation ใหม่ (server สร้าง key ภายในที่ไม่ซ้ำ) ดังนั้น retry โดยไม่มีเฮดเดอร์คือการเรียกใหม่ที่คิดค่าใช้จ่ายแยก ต่างจาก mutation endpoint อื่นที่ยังต้องมีเฮดเดอร์นี้
การตอบกลับ#
รูปแบบมาตรฐานของ OpenAI: choices, usage และ invocation id ในเฮดเดอร์ X-AIGrid-Invocation เก็บ id นี้ไว้ เพราะเชื่อมการใช้งาน ค่าใช้จ่าย และการช่วยเหลือ ดู การใช้งานและการวัดปริมาณ
ข้อผิดพลาด#
ความล้มเหลวด้านวงเงินและการเข้าถึงมีรหัสของตนเอง ได้แก่ insufficient_budget, key_limit, key_scope, project_mismatch, model_not_found แต่ละรหัสระบุสิ่งที่ทำให้แก้ได้ ดู ข้อผิดพลาด
การเรียกที่ถูกปฏิเสธก่อนถึงผู้ให้บริการจะไม่ถูกคิดค่าใช้จ่าย