Chuyển tới nội dung chính

Gọi Inference API

Lấy endpoint URL

Sau khi endpoint đạt trạng thái Running, mở trang chi tiết endpoint để lấy endpoint URL. Inference gateway URL theo mẫu sau (tùy môi trường):

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

Endpoint cung cấp chat completions API chuẩn.

Gọi bằng 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
}'

Thay <ddi-gateway-domain> bằng gateway domain lấy từ trang chi tiết endpoint, và thay sk-vgw-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx bằng key của bạn ở tab API keys.

Gọi bằng 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 response

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)

Thử trong Playground

Mở trang chi tiết endpoint và nhấn tab Playground để gửi request thử trực tiếp từ trình duyệt, không cần viết code. Bạn chỉnh được Temperature, Top-pMax tokens ở panel Parameters bên phải.

Trang chi tiết endpoint — tab Playground với ô nhập prompt và panel parameters

Các lỗi HTTP thường gặp

Nguyên nhânCách xử lý
401 UnauthorizedThiếu API key hoặc key không hợp lệKiểm tra lại API key ở tab API keys
429 rate_limitedVượt rate limit (RPM, TPM hoặc số request đồng thời)Thử lại sau một khoảng, hoặc liên hệ support để nâng hạn mức
503 Service UnavailableEndpoint chưa ở trạng thái RunningChờ endpoint đạt Running trong My endpoints