前提条件
- SchneeAI アカウントと API キー。デザインパートナーアクセスを希望する場合は [email protected] までメールでご申請ください。
- 任意の HTTP クライアント。以下の例は
curl、Python 3.9+、TypeScript 5+ を使います。
ステップ1 — 最初のリクエスト
Gateway は OpenAI 互換のエンドポイント POST /v1/chat/completions を公開しています。最小の呼び出し:
curl https://api.schneeai.com/v1/chat/completions \
-H "Authorization: Bearer $SCHNEEAI_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "auto",
"messages": [
{ "role": "user", "content": "SchneeAI を一文で要約して。" }
]
}'
model: "auto" は SchneeAI のルータに「ルーティングポリシーに基づいて最適なモデルを選択」と指示します。特定モデルを固定したい場合は "auto" を モデルディレクトリ のモデル ID で置き換えてください。
レスポンス形状(抜粋):
{
"id": "chatcmpl-...",
"choices": [{
"message": { "role": "assistant", "content": "SchneeAI は..." }
}],
"usage": { "prompt_tokens": 12, "completion_tokens": 18, "total_tokens": 30 },
"model": "auto→gpt-4o-mini",
"request_id": "req_01HQ..."
}
model フィールドはルータが実際に選択したモデルを示します。request_id はサポート問い合わせ時の追跡ハンドルです。
ステップ2 — トークンをストリーミング
チャット UI ではストリーミングが必須。stream: true を設定すると、Gateway は Server-Sent Events (SSE) を返します。
Python
import json
import requests
res = requests.post(
"https://api.schneeai.com/v1/chat/completions",
headers={
"Authorization": f"Bearer {API_KEY}",
"Content-Type": "application/json",
},
json={
"model": "auto",
"messages": [{"role": "user", "content": "雪に関する俳句を詠んで。"}],
"stream": True,
},
stream=True,
)
for line in res.iter_lines():
if not line or not line.startswith(b"data: "):
continue
chunk = line[6:]
if chunk == b"[DONE]":
break
data = json.loads(chunk)
delta = data["choices"][0]["delta"].get("content", "")
print(delta, end="", flush=True)
TypeScript (ブラウザ / Node 18+)
const res = await fetch("https://api.schneeai.com/v1/chat/completions", {
method: "POST",
headers: {
Authorization: `Bearer ${API_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
model: "auto",
messages: [{ role: "user", content: "雪に関する俳句を詠んで。" }],
stream: true,
}),
});
const reader = res.body!.getReader();
const decoder = new TextDecoder();
let buffer = "";
while (true) {
const { done, value } = await reader.read();
if (done) break;
buffer += decoder.decode(value, { stream: true });
const lines = buffer.split("\n");
buffer = lines.pop()!;
for (const line of lines) {
if (!line.startsWith("data: ")) continue;
const chunk = line.slice(6);
if (chunk === "[DONE]") return;
const delta = JSON.parse(chunk).choices[0].delta?.content ?? "";
process.stdout.write(delta);
}
}
ステップ3 — 名前付きプロンプトを使う
ハードコードされたプロンプトは腐ります。PromptOps UI でプロンプトを登録、バージョニングし、名前で呼び出します:
curl https://api.schneeai.com/v1/chat/completions \
-H "Authorization: Bearer $SCHNEEAI_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"prompt": "support-reply",
"variables": {
"user_message": "注文 #1234 が届きません。",
"tone": "empathetic"
}
}'
SchneeAI は support-reply のアクティブバージョンを解決し、{{user_message}} と {{tone}} を埋め、登録されたモデルとパラメータを適用して補完を返します。モデル横断でうまく機能するプロンプトパターンは プロンプトテンプレートライブラリ を参照してください。
ステップ4 — エラーを正しく扱う
ネットワーク呼び出しは失敗します。本番コードは以下を満たす必要があります:
- 429, 500, 502, 503, 504 のみリトライ。
Retry-After があれば従う。Idempotency-Key を送り、リトライで二重課金されないようにする。
import uuid
import requests
from tenacity import retry, stop_after_attempt, wait_exponential_jitter, retry_if_exception_type
class RetryableError(Exception): pass
@retry(stop=stop_after_attempt(5),
wait=wait_exponential_jitter(initial=1, max=60, jitter=2),
retry=retry_if_exception_type(RetryableError))
def chat(messages, model="auto"):
res = requests.post(
"https://api.schneeai.com/v1/chat/completions",
headers={
"Authorization": f"Bearer {API_KEY}",
"Idempotency-Key": str(uuid.uuid4()),
"Content-Type": "application/json",
},
json={"model": model, "messages": messages},
timeout=60,
)
if res.status_code in {429, 500, 502, 503, 504}:
raise RetryableError(res.text)
res.raise_for_status()
return res.json()
完全なエラーコードリファレンス、リトライ規則、冪等性の詳細: エラーハンドリング。
ステップ5 — コストと利用を追跡
すべてのレスポンスは usage オブジェクトを含みます。SchneeAI はサーバー側でも構造化利用を記録し、GET /v1/usage で照会できます:
curl "https://api.schneeai.com/v1/usage?since=2026-07-01" \
-H "Authorization: Bearer $SCHNEEAI_API_KEY"
送信前にコストを見積もるには、プロンプトを トークンカウンタ に貼り付け、得られたトークン数を コスト電卓 に渡してください。
次のステップ
インタラクティブ版を準備中: CodeMirror ベースのプレイグラウンド(各スニペットをブラウザ内で編集・実行)がロードマップにあります。現在は、任意のブロックをターミナルまたはエディタにコピーして実行してください。