Endpoint và giao thức
Chọn endpoint theo model và tính năng.
Chọn model có hỗ trợ endpoint, công cụ và đầu vào cần dùng. Chat Completions, Responses và Messages có cấu trúc khác nhau; không thể đổi đường dẫn mà giữ nguyên mọi trường.
Điểm cuối API
| Phương thức | Đường dẫn | Mô tả |
|---|---|---|
| POST | /v1/messages | Anthropic Messages API (khuyến nghị) |
| POST | /v1/chat/completions | OpenAI Chat Completions (tương thích) |
| POST | /v1/responses | OpenAI Responses API (tương thích) |
| POST | /v1/embeddings | OpenAI Embeddings (req/text-embedding-3-small, req/text-embedding-3-large, req/gemini-embedding-2) |
| POST | /v1/images/generations | OpenAI Images Generations (req/gpt-image-2, req/gpt-image-2.6) |
| POST | /v1/images/edits | OpenAI Images Edits / ảnh tham chiếu (req/gpt-image-2) |
| POST | /v1/audio/speech | OpenAI Text-to-Speech Audio |
| POST | /v1/videos | OpenAI Videos — tạo video |
| GET | /v1/videos/{id} | Tra tiến độ video (không trừ thêm tiền) |
| GET | /v1/videos/{id}/content | Tải file MP4 (không trừ thêm tiền) |
| DELETE | /v1/videos/{id} | Hủy task đang chờ (không trừ thêm tiền) |
| GET | /v1/models | Danh sách model và giá |
| GET | /v1/models/media | Media Models Catalog (image, video, audio) |
| POST | /v1/messages/count_tokens | Ước lượng token (miễn phí) |
/v1/messages/count_tokens
Ước lượng cục bộ miễn phí, không dùng tokenizer gốc của hãng và không áp trần đầu vào. Cước phí thực tế tính theo usage của lượt gọi tạo nội dung.
curl --fail-with-body "https://thapi.cloud/v1/messages/count_tokens" \
-H "x-api-key: ${API_KEY:?API_KEY}" \
-H "Content-Type: application/json" \
-d '{"model":"claude-fable-5","messages":[{"role":"user","content":"Hello"}]}'Responses và gọi công cụ
Ví dụ Responses dùng POST /v1/responses và tự mang ngữ cảnh trong input. Tài liệu này không hứa hỗ trợ các thao tác lưu, lấy lại hoặc liệt kê response chưa được công bố.
import os
from getpass import getpass
from openai import OpenAI
client = OpenAI(api_key=os.getenv("STALI_API_KEY") or getpass("STALI_API_KEY: "), base_url="https://thapi.cloud/v1", max_retries=0)
stream = client.responses.create(model="gpt-5.6-sol", input="Hello", stream=True, store=False)
completed = False
for event in stream:
if event.type == "response.output_text.delta":
print(event.delta, end="", flush=True)
elif event.type == "response.completed":
completed = event.response.status == "completed"
elif event.type in ("error", "response.failed", "response.incomplete"):
raise RuntimeError(str(event))
if not completed:
raise RuntimeError("Stream ended without response.completed; preserve the request ID and check usage before retrying.")Hai mẫu JSON dùng cho Chat Completions và Messages tương ứng. Mẫu Python minh họa đầy đủ vòng gọi công cụ với phép cộng cục bộ: kiểm tra tên và tham số, thực thi hàm rồi gửi kết quả kèm đúng call ID. Gateway không tự chạy hàm. Mỗi vòng gọi model có thể tính phí riêng; ví dụ giới hạn tối đa 4 vòng và tắt tự thử lại ở SDK.
Ví dụ đã kiểm tra ở mức giao thức; hãy chạy thử một yêu cầu nhỏ và đối chiếu trang Sử dụng trước khi đưa vào production.