使用 /v1/translations 進行文字對文字翻譯。
這個 endpoint 與 音訊翻譯 不同,後者會接收音訊檔案,並且一律輸出英文文字。
針對 agent workflow,建議先找出目前推薦的翻譯 model:
curl "https://api.tokenlab.sh/v1/models?recommended_for=translation" \
-H "Authorization: Bearer sk-your-api-key"
接著把選定的 model 明確傳給 /v1/translations。
recommended_for=translation 只適用於文字翻譯(POST /v1/translations),不適用於 音訊翻譯。
請求主體
由 /v1/models?recommended_for=translation 回傳的翻譯 model ID。
目標語言代碼或語言名稱,取決於 model 支援情況。
來源文字格式。支援值:text/plain、text/html。
curl -X POST "https://api.tokenlab.sh/v1/translations" \
-H "Authorization: Bearer sk-your-api-key" \
-H "Content-Type: application/json" \
-d '{
"model": "doubao-seed-translation",
"text": "你好,歡迎使用 TokenLab。",
"target_language": "en"
}'
from openai import OpenAI
import requests
client = OpenAI(
api_key="sk-your-api-key",
base_url="https://api.tokenlab.sh/v1"
)
response = requests.post(
"https://api.tokenlab.sh/v1/translations",
headers={
"Authorization": "Bearer sk-your-api-key",
"Content-Type": "application/json",
},
json={
"model": "doubao-seed-translation",
"text": "你好,歡迎使用 TokenLab。",
"target_language": "en",
},
timeout=30,
)
print(response.json()["text"])
{
"text": "Hello, welcome to TokenLab.",
"model": "doubao-seed-translation",
"source_language": "zh",
"target_language": "en"
}