# API ของ Grok 4.6 เอกสารอ้างอิง API ฉบับสมบูรณ์สำหรับโมเดล LLM Grok 4.6 ที่ขับเคลื่อนโดย xAI ## รุ่นย่อยของโมเดล API นี้รองรับโมเดล Grok 4.6 สองรุ่นย่อย: | โมเดล | ID | คำอธิบาย | |-------|----|-------------| | **Grok 4.6 Text to Text** | `grok-4.6-text-to-text` | การให้เหตุผลและการสร้างข้อความล่าสุดของ Grok | | **Grok 4.6 Image to Text** | `grok-4.6-image-to-text` | การทำความเข้าใจและวิเคราะห์ภาพล่าสุดของ Grok | ## การเปรียบเทียบอย่างรวดเร็ว | คุณสมบัติ | Text to Text | Image to Text | |---------|--------------|---------------| | อินพุตข้อความ | จำเป็น ✅ | ไม่บังคับ | | อินพุตรูปภาพ | ไม่รองรับ ❌ | ไม่บังคับ | | อินพุตไฟล์ | ไม่รองรับ ❌ | ไม่รองรับ ❌ | | การตอบกลับแบบสตรีม | SSE | SSE | | `max_tokens` | ไม่บังคับ | ไม่บังคับ | ## Grok 4.6 Text to Text ### เอนด์พอยต์ ```http POST /api/v1/chat/completions ``` ### พารามิเตอร์คำขอ #### จำเป็น | พารามิเตอร์ | ประเภท | คำอธิบาย | |-----------|------|-------------| | `model` | string | ต้องเป็น `"grok-4.6-text-to-text"` | | `messages` | array | ข้อความที่เรียงตามลำดับ ดูโครงสร้างข้อความได้ที่ LLM API | | `stream` | boolean | ใช้ `true` สำหรับ SSE และ `false` สำหรับการตอบกลับ JSON รายการเดียว | #### การรองรับข้อความ | ฟิลด์ | ข้อความ | ไฟล์ | รูปภาพ | |-------|------|------|-------| | `messages` | จำเป็น ✅ | ไม่รองรับ ❌ | ไม่รองรับ ❌ | #### ไม่บังคับ | พารามิเตอร์ | ประเภท | ช่วง | คำอธิบาย | |-----------|------|-------|-------------| | `max_tokens` | integer | `> 1` | จำนวนโทเค็นเอาต์พุตสูงสุด | ### ตัวอย่างคำขอ ```javascript 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' }, body: JSON.stringify({ model: 'grok-4.6-text-to-text', messages: [ { role: 'user', content: 'Explain the key differences between REST and GraphQL APIs.' } ], stream: true, max_tokens: 2048 }) }); ``` ## Grok 4.6 Image to Text ### เอนด์พอยต์ ```http POST /api/v1/chat/completions ``` ### พารามิเตอร์คำขอ #### จำเป็น | พารามิเตอร์ | ประเภท | คำอธิบาย | |-----------|------|-------------| | `model` | string | ต้องเป็น `"grok-4.6-image-to-text"` | | `messages` | array | ข้อความที่เรียงตามลำดับ ดู LLM API และไฟล์แนบข้อความ | | `stream` | boolean | ใช้ `true` สำหรับ SSE และ `false` สำหรับการตอบกลับ JSON รายการเดียว | #### การรองรับข้อความ | ฟิลด์ | ข้อความ | ไฟล์ | รูปภาพ | |-------|------|------|-------| | `messages` | ไม่บังคับ | ไม่รองรับ ❌ | ไม่บังคับ | #### ไม่บังคับ | พารามิเตอร์ | ประเภท | ช่วง | คำอธิบาย | |-----------|------|-------|-------------| | `max_tokens` | integer | `> 1` | จำนวนโทเค็นเอาต์พุตสูงสุด | ### ตัวอย่างคำขอ ```javascript 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' }, body: JSON.stringify({ model: 'grok-4.6-image-to-text', messages: [ { role: 'user', content: [ { type: 'text', text: 'Describe the image and extract any visible text.' }, { type: 'image_url', image_url: { url: 'https://example.com/sample-image.jpg' } } ] } ], stream: true, max_tokens: 2048 }) }); ``` ## รูปแบบการตอบกลับ โมเดล LLM Grok 4.6 ส่งคืนการตอบกลับ completion ที่เข้ากันได้กับ OpenAI เมื่อใช้ `stream: true` การตอบกลับจะเป็น Server-Sent Events และเมื่อใช้ `stream: false` การตอบกลับจะเป็นออบเจ็กต์ JSON รายการเดียว ### การตอบกลับสำเร็จ ```text data: {"id":"chatcmpl-...","object":"chat.completion.chunk","created":1710000000,"model":"grok-4.6-text-to-text","choices":[{"index":0,"delta":{"role":"assistant"},"finish_reason":null}]} data: {"id":"chatcmpl-...","object":"chat.completion.chunk","created":1710000000,"model":"grok-4.6-text-to-text","choices":[{"index":0,"delta":{"content":"Here is a concise explanation"},"finish_reason":null}]} data: {"id":"chatcmpl-...","object":"chat.completion.chunk","created":1710000000,"model":"grok-4.6-text-to-text","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]} data: {"id":"chatcmpl-...","object":"chat.completion.chunk","created":1710000000,"model":"grok-4.6-text-to-text","choices":[],"usage":{"prompt_tokens":12,"completion_tokens":8,"total_tokens":20}} data: [DONE] ``` ### การตอบกลับข้อผิดพลาด ```text event: error data: {"error":{"message":"API requests too frequent, exceeding rate limit","type":"rate_limit_error","code":"1302","param":null}} ``` ## แนวทางปฏิบัติที่แนะนำ 1. **ใช้ข้อความที่มีโครงสร้าง:** ส่งประวัติการสนทนาผ่าน `messages[]` แทนการรวมบริบทไว้ในพรอมต์เดียว 2. **เลือกรุ่นย่อยของโมเดลที่เหมาะสม:** ใช้ Text to Text สำหรับการให้เหตุผลด้วยข้อความเท่านั้น และใช้ Image to Text เมื่อต้องทำความเข้าใจรูปภาพ 3. **ประมวลผลเหตุการณ์ SSE:** ต่อท้าย `choices[0].delta.content` เพื่อแสดงผลแบบสตรีม และถือว่า `data: [DONE]` คือการดำเนินการเสร็จสมบูรณ์ 4. **ส่งเฉพาะอินพุตที่รองรับ:** Text to Text ไม่รองรับไฟล์แนบที่เป็นรูปภาพหรือไฟล์