Chat
檔案分析
使用 Chat Completions API 分析圖像、音訊與檔案。
Chat Completions API 透過 messages 陣列中的內容部分支援多模態輸入。使用者訊息可包含文字、圖像、音訊與檔案。
端點與驗證方式同建立聊天回應。
請先確認所選模型支援對應媒體類型。file_id 必須來自同一服務與帳戶可讀取的上傳結果;本頁未定義上傳端點,不可直接使用其他供應商的檔案 ID。網址須可由服務端存取,本機路徑不能當作遠端檔案網址。
目前本地網關的 /v1/files 上傳路由尚未實作。首次串接請使用本頁的 URL 或 Base64 輸入方式;只有已確認部署環境支援上傳,且已取得同帳戶可讀取的檔案 ID 時,才使用 file_id 範例。
內容部分類型
| 類型 | 說明 |
|---|---|
text | 文字提示(必須包含 text 欄位) |
image_url | 透過 URL 或 Base64 資料輸入圖像;可選 detail:auto、low、high |
input_audio | 透過 Base64 資料輸入音訊;format:wav 或 mp3 |
file | 使用 file_id,或以 file_data 搭配 filename 傳入檔案;檔名本身不包含檔案內容 |
圖像分析
透過 URL 分析圖像:
json
{
"model": "gpt-5",
"messages": [
{
"role": "user",
"content": [
{"type": "text", "text": "What is in this image?"},
{
"type": "image_url",
"image_url": {
"url": "https://example.com/image.jpg",
"detail": "auto"
}
}
]
}
],
"max_completion_tokens": 300,
"stream": false
}Base64 編碼圖像:
json
{
"model": "gpt-5",
"messages": [
{
"role": "user",
"content": [
{"type": "text", "text": "Describe this image."},
{
"type": "image_url",
"image_url": {
"url": "data:image/jpeg;base64,/9j/4AAQSkZJRg..."
}
}
]
}
]
}音訊分析
以 Base64 編碼的內容部分傳送音訊:
json
{
"model": "gpt-4o-audio-preview",
"messages": [
{
"role": "user",
"content": [
{
"type": "input_audio",
"input_audio": {
"data": "<base64-encoded-audio>",
"format": "wav"
}
},
{"type": "text", "text": "Transcribe and summarize this audio."}
]
}
]
}支援的音訊格式:wav、mp3。
檔案分析
使用 Files API 傳回的檔案 ID 分析文件:
json
{
"model": "gpt-5",
"messages": [
{
"role": "user",
"content": [
{"type": "text", "text": "Summarize this document"},
{
"type": "file",
"file": {
"file_id": "file-abc123"
}
}
]
}
]
}請求範例
執行方式見多語言範例說明。先設定 API_KEY,並替換模型、檔案網址及 ID 占位值;四種方式會顯示相同請求的原始回應。
bash
curl --fail-with-body --silent --show-error --max-time 180 \
--request POST \
--url "https://api.tokatlas.ai/v1/chat/completions" \
--header "Authorization: Bearer $API_KEY" \
--header "Content-Type: application/json" \
--data '{
"model": "gpt-5",
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": "What is in this image?"
},
{
"type": "image_url",
"image_url": {
"url": "https://example.com/image.jpg"
}
}
]
}
],
"stream": false
}'python
import os
import requests
headers = {
'Authorization': 'Bearer ' + os.environ["API_KEY"],
'Content-Type': 'application/json',
}
payload = {'model': 'gpt-5',
'messages': [{'role': 'user',
'content': [{'type': 'text', 'text': 'What is in this image?'},
{'type': 'image_url',
'image_url': {'url': 'https://example.com/image.jpg'}}]}],
'stream': False}
response = requests.request(
'POST', 'https://api.tokatlas.ai/v1/chat/completions', headers=headers,
json=payload,
timeout=180,
)
response.raise_for_status()
print(response.text)js
if (!process.env.API_KEY) throw new Error("Set API_KEY first.");
const response = await fetch("https://api.tokatlas.ai/v1/chat/completions", {
method: "POST",
headers: {
"Authorization": "Bearer " + process.env.API_KEY,
"Content-Type": "application/json",
},
body: JSON.stringify({
"model": "gpt-5",
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": "What is in this image?"
},
{
"type": "image_url",
"image_url": {
"url": "https://example.com/image.jpg"
}
}
]
}
],
"stream": false
}),
signal: AbortSignal.timeout(180_000),
});
if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${await response.text()}`);
}
console.log(await response.text());java
import java.net.URI;
import java.net.http.*;
import java.time.Duration;
public class Example {
public static void main(String[] args) throws Exception {
String apiKey = System.getenv("API_KEY");
if (apiKey == null || apiKey.isBlank()) {
throw new IllegalArgumentException("Set API_KEY first.");
}
String payload = String.join("\n",
"{",
" \"model\": \"gpt-5\",",
" \"messages\": [",
" {",
" \"role\": \"user\",",
" \"content\": [",
" {",
" \"type\": \"text\",",
" \"text\": \"What is in this image?\"",
" },",
" {",
" \"type\": \"image_url\",",
" \"image_url\": {",
" \"url\": \"https://example.com/image.jpg\"",
" }",
" }",
" ]",
" }",
" ],",
" \"stream\": false",
"}"
);
HttpClient client = HttpClient.newBuilder()
.connectTimeout(Duration.ofSeconds(30)).build();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://api.tokatlas.ai/v1/chat/completions"))
.timeout(Duration.ofSeconds(180))
.header("Authorization", "Bearer " + apiKey)
.header("Content-Type", "application/json")
.method("POST", HttpRequest.BodyPublishers.ofString(payload))
.build();
HttpResponse<String> response = client.send(
request, HttpResponse.BodyHandlers.ofString());
if (response.statusCode() < 200 || response.statusCode() >= 300) {
throw new IllegalStateException("HTTP " + response.statusCode() + ": "
+ response.body());
}
System.out.println(response.body());
}
}回應範例
json
{
"code": 200,
"data": {
"id": "chatcmpl-B9MHDbslfkBeAs8l4bebGdFOJ6PeG",
"object": "chat.completion",
"created": 1741570283,
"model": "gpt-5",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "The image shows a wooden boardwalk path running through a lush green field or meadow. The sky is bright blue with some scattered clouds.",
"refusal": null
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 1117,
"completion_tokens": 46,
"total_tokens": 1163,
"prompt_tokens_details": {
"image_tokens": 1100
}
}
}
}