メインコンテンツまでスキップ

Inference API を呼び出す

endpoint URL を取得する

endpoint が Running になったら、詳細ページを開いて endpoint URL を確認します。inference gateway URL は次のパターンです(環境ごとに異なります)。

https://<ddi-gateway-domain>/v1/chat/completions

endpoint は標準の chat completions API を公開しています。

curl で呼び出す

curl https://<ddi-gateway-domain>/v1/chat/completions \
-H "Authorization: Bearer sk-vgw-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" \
-H "Content-Type: application/json" \
-d '{
"model": "mistralai/Mistral-7B-Instruct-v0.3",
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Summarize the benefits of dedicated GPU inference."}
],
"max_tokens": 256
}'

<ddi-gateway-domain> は endpoint 詳細ページの gateway domain に、sk-vgw-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxAPI keys タブで取得した key に置き換えます。

Python で呼び出す

import requests

url = "https://<ddi-gateway-domain>/v1/chat/completions"
headers = {
"Authorization": "Bearer sk-vgw-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"Content-Type": "application/json",
}
payload = {
"model": "mistralai/Mistral-7B-Instruct-v0.3",
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hello!"},
],
"max_tokens": 256,
}

response = requests.post(url, headers=headers, json=payload)
result = response.json()
print(result["choices"][0]["message"]["content"])

streaming レスポンス

import requests, json

url = "https://<ddi-gateway-domain>/v1/chat/completions"
headers = {
"Authorization": "Bearer sk-vgw-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"Content-Type": "application/json",
}
payload = {
"model": "mistralai/Mistral-7B-Instruct-v0.3",
"messages": [{"role": "user", "content": "Write a short poem."}],
"max_tokens": 200,
"stream": True,
}

with requests.post(url, headers=headers, json=payload, stream=True) as r:
for line in r.iter_lines():
if line and line.startswith(b"data: "):
data = line[6:]
if data != b"[DONE]":
chunk = json.loads(data)
content = chunk["choices"][0]["delta"].get("content", "")
print(content, end="", flush=True)

Playground で試す

endpoint 詳細ページを開き、Playground タブをクリックすると、コードを書かずにブラウザから直接テストリクエストを送信できます。右側の Parameters パネルで TemperatureTop-pMax tokens を調整できます。

endpoint 詳細ページ — プロンプト入力欄と parameters パネルを備えた Playground タブ

よくある HTTP エラー

ステータス原因対処
401 UnauthorizedAPI key が未指定または無効API keys タブで API key を確認します
429 rate_limitedレート制限を超過(RPM、TPM、同時リクエスト数)時間をおいて再試行するか、上限引き上げをサポートに依頼します
503 Service Unavailableendpoint が Running ではないMy endpoints で endpoint が Running になるまで待ちます