API Teks

Chat, Legacy Completions, dan Responses; mendukung streaming SSE

API Teks menyediakan Chat, penyelesaian teks Legacy, dan Responses. Ketiganya mendukung JSON non-streaming dan streaming SSE.

Ringkasan

Gateway YunSell AI mengekspos tiga endpoint teks. Pilih berdasarkan skenario:

EndpointGunakan saatJenis endpoint
POST /v1/chat/completionsObrolan umum, tool calling, input multimodal (gambar, dll.), streaming SSEopenai
POST /v1/completionsPenyelesaian teks legacy (prompt → kelanjutan)openai
POST /v1/responsesResponses multi-putaran, reasoning, output terstruktur, tool callingopenai-response

Tips pemilihan:

  • Sebagian besar proyek baru sebaiknya memilih Chat Completions (/v1/chat/completions).
  • Gunakan Completions (/v1/completions) hanya untuk kelanjutan prompt tunggal atau kompatibilitas SDK legacy.
  • Gunakan Responses (/v1/responses) saat Anda membutuhkan konteks multi-putaran previous_response_id atau output terstruktur Responses.

Prasyarat: model harus muncul di daftar API Model dan mencakup jenis endpoint yang sesuai di supported_endpoint_types.

Konvensi: Auth, Base URL, Content-Type, dll. dijelaskan di Konvensi umum. Format error ada di Konvensi umum.

Endpoint

MethodPathDeskripsi
POST/v1/chat/completionsChat (mendukung streaming SSE)
POST/v1/completionsPenyelesaian teks
POST/v1/responsesObrolan Responses

POST /v1/chat/completions

Penyelesaian chat dengan JSON non-streaming dan streaming SSE.

Auth: Bearer Token

Content-Type: application/json

Body permintaan

{
  "model": "gpt-4o",
  "messages": [
    {
      "role": "system",
      "content": "You are a helpful assistant."
    },
    {
      "role": "user",
      "content": "Hello, who are you?"
    }
  ],
  "temperature": 0.7,
  "max_tokens": 1024,
  "stream": false
}

Field permintaan

FieldTipeWajibDeskripsi
modelstringYaID model
messagesarrayYaDaftar pesan percakapan
messages[].rolestringYasystem / user / assistant / tool
messages[].contentstring / arrayYaTeks atau konten multimodal
streambooleanTidakSaat true, mengembalikan aliran SSE; default false
temperaturenumberTidakTemperatur sampling, 0–2
top_pnumberTidakNucleus sampling, 0–1
max_tokensintegerTidakMaks. token output
max_completion_tokensintegerTidakMaks. token completion
stopstring / arrayTidakUrutan stop
toolsarrayTidakDefinisi tool
tool_choicestring / objectTidakStrategi pemilihan tool
response_formatobjectTidakFormat output terstruktur
stream_optionsobjectTidakOpsi streaming, mis. { "include_usage": true }

Contoh pesan multimodal (input gambar):

{
  "model": "gpt-4o",
  "messages": [
    {
      "role": "user",
      "content": [
        { "type": "text", "text": "What's in this image?" },
        {
          "type": "image_url",
          "image_url": { "url": "https://example.com/image.jpg" }
        }
      ]
    }
  ]
}

Contoh permintaan (curl)

Non-streaming:

curl http://www.mangoto.ai/v1/chat/completions \
  -H "Authorization: Bearer sk-xxx" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-4o",
    "messages": [
      {"role": "system", "content": "You are a helpful assistant."},
      {"role": "user", "content": "Hello, who are you?"}
    ],
    "temperature": 0.7,
    "max_tokens": 1024,
    "stream": false
  }'

Streaming (SSE):

curl -N http://www.mangoto.ai/v1/chat/completions \
  -H "Authorization: Bearer sk-xxx" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-4o",
    "messages": [{"role": "user", "content": "Hello"}],
    "stream": true
  }'

Body respons (non-streaming)

HTTP 200, Content-Type: application/json

{
  "id": "chatcmpl-abc123",
  "object": "chat.completion",
  "created": 1712697600,
  "model": "gpt-4o",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "Hello! I'm an AI assistant. How can I help you today?"
      },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 28,
    "completion_tokens": 15,
    "total_tokens": 43
  }
}

Field respons

FieldTipeDeskripsi
idstringID respons, mis. chatcmpl-xxx
objectstringSelalu chat.completion
createdintegerUnix timestamp (detik)
modelstringModel yang benar-benar digunakan
choicesarrayHasil generasi; panjang sama dengan n permintaan (default 1)
choices[].indexintegerIndeks hasil, dimulai dari 0
choices[].message.rolestringSelalu assistant
choices[].message.contentstring / nullTeks balasan; mungkin null untuk tool call
choices[].message.tool_callsarrayDaftar tool call (saat tools diaktifkan)
choices[].message.tool_calls[].idstringID tool call
choices[].message.tool_calls[].typestringSelalu function
choices[].message.tool_calls[].function.namestringNama fungsi
choices[].message.tool_calls[].function.argumentsstringArgumen fungsi sebagai string JSON
choices[].message.reasoning_contentstringTeks reasoning (model reasoning)
choices[].finish_reasonstringAlasan berhenti; lihat tabel di bawah
usageobjectPenggunaan token; lihat Objek respons bersama

Nilai finish_reason:

NilaiDeskripsi
stopBerhenti alami atau mencapai urutan stop
lengthMencapai batas max_tokens
tool_callsModel memulai tool call
content_filterKonten diblokir oleh kebijakan keamanan

Contoh respons tool call:

{
  "id": "chatcmpl-abc123",
  "object": "chat.completion",
  "created": 1712697600,
  "model": "gpt-4o",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": null,
        "tool_calls": [
          {
            "id": "call_abc123",
            "type": "function",
            "function": {
              "name": "get_weather",
              "arguments": "{\"location\":\"Beijing\"}"
            }
          }
        ]
      },
      "finish_reason": "tool_calls"
    }
  ],
  "usage": {
    "prompt_tokens": 50,
    "completion_tokens": 20,
    "total_tokens": 70
  }
}

Body respons (streaming)

HTTP 200, Content-Type: text/event-stream

Dengan "stream": true di body permintaan, server mendorong event SSE. Setiap pesan terlihat seperti:

data: {"id":"chatcmpl-abc123","object":"chat.completion.chunk","created":1712697600,"model":"gpt-4o","choices":[{"index":0,"delta":{"role":"assistant","content":"Hello"},"finish_reason":null}]}

data: {"id":"chatcmpl-abc123","object":"chat.completion.chunk","created":1712697600,"model":"gpt-4o","choices":[{"index":0,"delta":{"content":"!"},"finish_reason":null}]}

data: {"id":"chatcmpl-abc123","object":"chat.completion.chunk","created":1712697600,"model":"gpt-4o","choices":[{"index":0,"delta":{},"finish_reason":"stop"}],"usage":{"prompt_tokens":28,"completion_tokens":15,"total_tokens":43}}

data: [DONE]

Field chunk streaming

FieldTipeDeskripsi
idstringID yang sama dengan respons akhir
objectstringSelalu chat.completion.chunk
createdintegerUnix timestamp (detik)
modelstringModel yang digunakan
system_fingerprintstringSidik jari sistem (beberapa model)
choices[].indexintegerIndeks hasil
choices[].delta.rolestringChunk pertama mungkin menyertakan assistant
choices[].delta.contentstringFragmen teks inkremental
choices[].delta.reasoning_contentstringTeks reasoning inkremental (model reasoning)
choices[].delta.tool_callsarrayFragmen tool call inkremental
choices[].finish_reasonstring / nullAlasan berhenti di chunk terakhir; selain itu null
usageobjectHanya muncul di chunk terakhir (memerlukan stream_options.include_usage=true)

Penanda akhir streaming: baris terakhir data: [DONE].


POST /v1/completions

Endpoint penyelesaian teks legacy.

Auth: Bearer Token

Content-Type: application/json

Body permintaan

{
  "model": "gpt-4o",
  "prompt": "Say this is a test",
  "max_tokens": 100,
  "temperature": 0.7,
  "stream": false
}

Field permintaan

FieldTipeWajibDeskripsi
modelstringYaID model
promptstring / arrayYaTeks prompt
max_tokensintegerTidakMaks. token output
temperaturenumberTidakTemperatur sampling
top_pnumberTidakNucleus sampling
nintegerTidakJumlah completion; default 1
streambooleanTidakApakah streaming
stopstring / arrayTidakUrutan stop

Contoh permintaan (curl)

Non-streaming:

curl http://www.mangoto.ai/v1/completions \
  -H "Authorization: Bearer sk-xxx" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-4o",
    "prompt": "Say this is a test",
    "max_tokens": 100,
    "temperature": 0.7,
    "stream": false
  }'

Streaming (SSE):

curl -N http://www.mangoto.ai/v1/completions \
  -H "Authorization: Bearer sk-xxx" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-4o",
    "prompt": "Say this is a test",
    "stream": true
  }'

Body respons (non-streaming)

HTTP 200

{
  "id": "cmpl-abc123",
  "object": "text_completion",
  "created": 1712697600,
  "model": "gpt-4o",
  "choices": [
    {
      "index": 0,
      "text": "This is indeed a test.",
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 5,
    "completion_tokens": 6,
    "total_tokens": 11
  }
}

Field respons

FieldTipeDeskripsi
idstringID respons, mis. cmpl-xxx
objectstringSelalu text_completion
createdintegerUnix timestamp (detik)
modelstringModel yang digunakan
choicesarrayHasil generasi
choices[].indexintegerIndeks hasil
choices[].textstringTeks completion
choices[].finish_reasonstringstop / length / content_filter
usageobjectPenggunaan token; lihat Objek respons bersama

Body respons (streaming)

data: {"id":"cmpl-abc123","object":"text_completion","created":1712697600,"choices":[{"index":0,"text":"This","finish_reason":null}]}

data: {"id":"cmpl-abc123","object":"text_completion","created":1712697600,"choices":[{"index":0,"text":" is","finish_reason":null}]}

data: [DONE]

Field chunk streaming

FieldTipeDeskripsi
idstringID respons
objectstringSelalu text_completion
createdintegerUnix timestamp (detik)
choices[].indexintegerIndeks hasil
choices[].textstringFragmen teks inkremental
choices[].finish_reasonstring / nullAlasan berhenti di chunk terakhir

POST /v1/responses

Endpoint obrolan Responses yang mendukung percakapan multi-putaran, tool calling, reasoning, dan lainnya.

Auth: Bearer Token

Content-Type: application/json

Body permintaan

{
  "model": "gpt-4o",
  "input": [
    {
      "role": "user",
      "content": [
        {
          "type": "input_text",
          "text": "Explain quantum computing in one sentence."
        }
      ]
    }
  ],
  "temperature": 0.7,
  "max_output_tokens": 1024,
  "stream": false
}

Field permintaan

FieldTipeWajibDeskripsi
modelstringYaID model
inputstring / arrayYaKonten input; string atau array pesan
instructionsstringTidakInstruksi sistem
temperaturenumberTidakTemperatur sampling
max_output_tokensintegerTidakMaks. token output
streambooleanTidakApakah streaming
toolsarrayTidakDefinisi tool
tool_choicestring / objectTidakStrategi pemilihan tool
previous_response_idstringTidakID respons sebelumnya untuk obrolan multi-putaran
storebooleanTidakApakah menyimpan percakapan
metadataobjectTidakMetadata kustom

Contoh permintaan (curl)

Non-streaming:

curl http://www.mangoto.ai/v1/responses \
  -H "Authorization: Bearer sk-xxx" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-4o",
    "input": [
      {
        "role": "user",
        "content": [
          {"type": "input_text", "text": "Explain quantum computing in one sentence."}
        ]
      }
    ],
    "temperature": 0.7,
    "max_output_tokens": 1024,
    "stream": false
  }'

Streaming (SSE):

curl -N http://www.mangoto.ai/v1/responses \
  -H "Authorization: Bearer sk-xxx" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-4o",
    "input": "Hello",
    "stream": true
  }'

Body respons (non-streaming)

HTTP 200

{
  "id": "resp_abc123",
  "object": "response",
  "created_at": 1712697600,
  "status": "completed",
  "model": "gpt-4o",
  "output": [
    {
      "type": "message",
      "id": "msg_abc123",
      "status": "completed",
      "role": "assistant",
      "content": [
        {
          "type": "output_text",
          "text": "Quantum computing uses quantum bits that can exist in superposition to perform certain calculations exponentially faster than classical computers.",
          "annotations": []
        }
      ]
    }
  ],
  "usage": {
    "input_tokens": 15,
    "output_tokens": 28,
    "total_tokens": 43
  },
  "temperature": 0.7,
  "top_p": 1.0
}

Field respons

FieldTipeDeskripsi
idstringID respons, mis. resp_xxx
objectstringSelalu response
created_atintegerUnix timestamp (detik)
statusstringStatus respons; lihat tabel di bawah
modelstringModel yang digunakan
outputarrayDaftar item output
output[].typestringJenis output; lihat tabel di bawah
output[].idstringID item output
output[].statusstringStatus item output
output[].rolestringPeran (assistant untuk tipe message)
output[].contentarrayDaftar blok konten
output[].content[].typestringJenis konten, mis. output_text
output[].content[].textstringKonten teks
output[].content[].annotationsarrayDaftar anotasi/kutipan
output[].call_idstringID tool call (tipe function_call)
output[].namestringNama fungsi (tipe function_call)
output[].argumentsstring / objectArgumen fungsi (tipe function_call)
output[].qualitystringKualitas gambar (tipe image_generation_call)
output[].sizestringUkuran gambar (tipe image_generation_call)
instructionsstringInstruksi sistem (echo)
temperaturenumberTemperatur sampling (echo)
top_pnumberNucleus sampling (echo)
max_output_tokensintegerMaks. token output (echo)
previous_response_idstringID respons sebelumnya
parallel_tool_callsbooleanApakah panggilan tool paralel diaktifkan
storebooleanApakah percakapan disimpan
metadataobjectMetadata kustom
incomplete_details.reasonstringAlasan tidak lengkap (saat status=incomplete)
errorobjectInfo error (saat status=failed)
usageobjectPenggunaan token; lihat Objek respons bersama

Nilai status:

NilaiDeskripsi
completedGenerasi selesai
in_progressGenerasi sedang berlangsung
failedGenerasi gagal
incompleteTidak lengkap karena batas token atau serupa

Nilai output[].type:

NilaiDeskripsi
messagePesan teks asisten
function_callPanggilan fungsi/tool
image_generation_callPanggilan generasi gambar
web_search_callPanggilan pencarian web

Body respons (streaming)

HTTP 200, Content-Type: text/event-stream

data: {"type":"response.created","response":{"id":"resp_abc123","object":"response","status":"in_progress"}}

data: {"type":"response.output_text.delta","delta":"Quantum","output_index":0,"content_index":0}

data: {"type":"response.output_text.delta","delta":" computing","output_index":0,"content_index":0}

data: {"type":"response.completed","response":{"id":"resp_abc123","status":"completed","usage":{"input_tokens":15,"output_tokens":28,"total_tokens":43}}}

data: [DONE]

Field event streaming

FieldTipeDeskripsi
typestringJenis event; lihat tabel di bawah
responseobjectObjek respons lengkap atau sebagian
deltastringTeks inkremental
output_indexintegerIndeks item output
content_indexintegerIndeks blok konten
itemobjectItem output yang ditambahkan/selesai
item_idstringID item output
partobjectFragmen ringkasan reasoning

Nilai type umum:

NilaiDeskripsi
response.createdRespons dibuat
response.output_text.deltaDelta teks
response.output_item.addedItem output ditambahkan
response.output_item.doneItem output selesai
response.function_call_arguments.deltaDelta argumen fungsi
response.function_call_arguments.doneArgumen fungsi selesai
response.completedRespons selesai