docs
การตั้งค่าการเชื่อมต่อ

OpenAI SDK

Markdown

ไลบรารีทางการของ OpenAI สำหรับ Python และ Node.js ใช้งานกับ Ailoot ได้โดยไม่ต้องแก้โค้ด เปลี่ยนเพียงที่อยู่และคีย์.

คู่มือฉบับเต็ม

การติดตั้ง

pip install openai

การตั้งค่าการเชื่อมต่อ

ระบุที่อยู่หลัก https://api.ailoot.ai/v1. กำหนดในโค้ดหรือผ่านตัวแปรก็ได้ OPENAI_BASE_URL และ OPENAI_API_KEY — ถ้าใช้ตัวแปรก็ไม่ต้องแก้โค้ดเลย.

from openai import OpenAI
import os

client = OpenAI(max_retries=0, 
    base_url="https://api.ailoot.ai/v1",
    api_key=os.environ["AILOOT_API_KEY"],
)

resp = client.chat.completions.create(
    model="claude-sonnet-5-5",
    messages=[{"role": "user", "content": "อธิบายในสองประโยคว่า API คืออะไร"}],
)
print(resp.choices[0].message.content)

Responses API

รูปแบบใหม่ของ OpenAI ใช้ได้กับโมเดลที่แค็ตตาล็อกระบุว่ารองรับรูปแบบ Responses (53 ตัวระบุ รวมถึง Claude, GPT, DeepSeek, Qwen, Kimi, GLM).

resp = client.responses.create(
    model="gpt-6.1-sol",
    instructions="ตอบให้กระชับและตรงประเด็น",
    input="TCP ต่างจาก UDP อย่างไร?",
)
print(resp.output_text)

เครื่องมือ (function calling)

tools = [{
    "type": "function",
    "function": {
        "name": "get_weather",
        "description": "สภาพอากาศในเมือง",
        "parameters": {
            "type": "object",
            "properties": {"city": {"type": "string"}},
            "required": ["city"],
        },
    },
}]

import json

messages = [{"role": "user", "content": "อากาศที่คาซานเป็นอย่างไร?"}]
# ปิดการลองใหม่อัตโนมัติ: หากหมดเวลารอ ให้ตรวจสอบ Request ID ในประวัติการใช้งานก่อน
client = client.with_options(max_retries=0)
for turn in range(4):
    resp = client.chat.completions.create(
        model="claude-sonnet-5-5", messages=messages, tools=tools,
    )
    message = resp.choices[0].message
    messages.append(message.model_dump(exclude_none=True))
    if not message.tool_calls:
        print(message.content)
        break
    for call in message.tool_calls:
        if call.function.name != "get_weather":
            raise ValueError("Unexpected function")
        arguments = json.loads(call.function.arguments)
        # แทนที่ด้วยฟังก์ชันสภาพอากาศของคุณเอง ตัวอย่างนี้เป็นข้อมูลสาธิตอย่างชัดเจน
        result = {"city": arguments["city"], "temperature_c": 20, "demo": True}
        messages.append({"role": "tool", "tool_call_id": call.id,
                         "content": json.dumps(result, ensure_ascii=False)})
else:
    raise RuntimeError("Tool loop limit reached")

เอ็มเบดดิง

emb = client.embeddings.create(
    model="nvidia/nemotron-3-embed-1b",
    input=["เอกสารฉบับแรก", "เอกสารฉบับที่สอง"],
)
print(len(emb.data), len(emb.data[0].embedding))

รูปภาพในอินพุต

resp = client.chat.completions.create(
    model="gpt-6.1-sol",
    messages=[{
        "role": "user",
        "content": [
            {"type": "text", "text": "ในภาพนี้มีอะไร?"},
            {"type": "image_url", "image_url": {"url": "https://example.com/photo.jpg"}},
        ],
    }],
)

การจัดการข้อผิดพลาด

ข้อผิดพลาดของ Ailoot ส่งมาในรูปแบบ OpenAI จึงดักจับด้วยข้อยกเว้นมาตรฐานของ SDK ได้ ในข้อความมี Request ID.

import openai

try:
    resp = client.chat.completions.create(model="claude-sonnet-5-5", messages=[{"role": "user", "content": "สวัสดี"}])
except openai.APIStatusError as e:
    print(e.status_code, e.response.json()["error"]["code"])
    print(e.response.headers.get("x-request-id"))
ยังมีคำถามอยู่ใช่ไหม ส่ง Request ID และรายละเอียดมาให้เรา เราจะช่วยตรวจ⁠สอบติดต่อฝ่ายสนับสนุน