Chat Completions
curl --request POST \
--url https://api.k-router.com/v1/chat/completions \
--header 'Authorization: <authorization>' \
--header 'Content-Type: <content-type>' \
--data '
{
"model": "<string>",
"messages": [
{}
],
"stream": true,
"temperature": 123,
"max_tokens": 123,
"top_p": 123,
"fallback": [
{}
]
}
'import requests
url = "https://api.k-router.com/v1/chat/completions"
payload = {
"model": "<string>",
"messages": [{}],
"stream": True,
"temperature": 123,
"max_tokens": 123,
"top_p": 123,
"fallback": [{}]
}
headers = {
"Authorization": "<authorization>",
"Content-Type": "<content-type>"
}
response = requests.post(url, json=payload, headers=headers)
print(response.text)const options = {
method: 'POST',
headers: {Authorization: '<authorization>', 'Content-Type': '<content-type>'},
body: JSON.stringify({
model: '<string>',
messages: [{}],
stream: true,
temperature: 123,
max_tokens: 123,
top_p: 123,
fallback: [{}]
})
};
fetch('https://api.k-router.com/v1/chat/completions', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.k-router.com/v1/chat/completions",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'model' => '<string>',
'messages' => [
[
]
],
'stream' => true,
'temperature' => 123,
'max_tokens' => 123,
'top_p' => 123,
'fallback' => [
[
]
]
]),
CURLOPT_HTTPHEADER => [
"Authorization: <authorization>",
"Content-Type: <content-type>"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "https://api.k-router.com/v1/chat/completions"
payload := strings.NewReader("{\n \"model\": \"<string>\",\n \"messages\": [\n {}\n ],\n \"stream\": true,\n \"temperature\": 123,\n \"max_tokens\": 123,\n \"top_p\": 123,\n \"fallback\": [\n {}\n ]\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("Authorization", "<authorization>")
req.Header.Add("Content-Type", "<content-type>")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://api.k-router.com/v1/chat/completions")
.header("Authorization", "<authorization>")
.header("Content-Type", "<content-type>")
.body("{\n \"model\": \"<string>\",\n \"messages\": [\n {}\n ],\n \"stream\": true,\n \"temperature\": 123,\n \"max_tokens\": 123,\n \"top_p\": 123,\n \"fallback\": [\n {}\n ]\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.k-router.com/v1/chat/completions")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Authorization"] = '<authorization>'
request["Content-Type"] = '<content-type>'
request.body = "{\n \"model\": \"<string>\",\n \"messages\": [\n {}\n ],\n \"stream\": true,\n \"temperature\": 123,\n \"max_tokens\": 123,\n \"top_p\": 123,\n \"fallback\": [\n {}\n ]\n}"
response = http.request(request)
puts response.read_bodyAPI Reference
Chat Completions
OpenAI 호환 Chat Completions API
POST
/
v1
/
chat
/
completions
Chat Completions
curl --request POST \
--url https://api.k-router.com/v1/chat/completions \
--header 'Authorization: <authorization>' \
--header 'Content-Type: <content-type>' \
--data '
{
"model": "<string>",
"messages": [
{}
],
"stream": true,
"temperature": 123,
"max_tokens": 123,
"top_p": 123,
"fallback": [
{}
]
}
'import requests
url = "https://api.k-router.com/v1/chat/completions"
payload = {
"model": "<string>",
"messages": [{}],
"stream": True,
"temperature": 123,
"max_tokens": 123,
"top_p": 123,
"fallback": [{}]
}
headers = {
"Authorization": "<authorization>",
"Content-Type": "<content-type>"
}
response = requests.post(url, json=payload, headers=headers)
print(response.text)const options = {
method: 'POST',
headers: {Authorization: '<authorization>', 'Content-Type': '<content-type>'},
body: JSON.stringify({
model: '<string>',
messages: [{}],
stream: true,
temperature: 123,
max_tokens: 123,
top_p: 123,
fallback: [{}]
})
};
fetch('https://api.k-router.com/v1/chat/completions', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.k-router.com/v1/chat/completions",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'model' => '<string>',
'messages' => [
[
]
],
'stream' => true,
'temperature' => 123,
'max_tokens' => 123,
'top_p' => 123,
'fallback' => [
[
]
]
]),
CURLOPT_HTTPHEADER => [
"Authorization: <authorization>",
"Content-Type: <content-type>"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "https://api.k-router.com/v1/chat/completions"
payload := strings.NewReader("{\n \"model\": \"<string>\",\n \"messages\": [\n {}\n ],\n \"stream\": true,\n \"temperature\": 123,\n \"max_tokens\": 123,\n \"top_p\": 123,\n \"fallback\": [\n {}\n ]\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("Authorization", "<authorization>")
req.Header.Add("Content-Type", "<content-type>")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://api.k-router.com/v1/chat/completions")
.header("Authorization", "<authorization>")
.header("Content-Type", "<content-type>")
.body("{\n \"model\": \"<string>\",\n \"messages\": [\n {}\n ],\n \"stream\": true,\n \"temperature\": 123,\n \"max_tokens\": 123,\n \"top_p\": 123,\n \"fallback\": [\n {}\n ]\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.k-router.com/v1/chat/completions")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Authorization"] = '<authorization>'
request["Content-Type"] = '<content-type>'
request.body = "{\n \"model\": \"<string>\",\n \"messages\": [\n {}\n ],\n \"stream\": true,\n \"temperature\": 123,\n \"max_tokens\": 123,\n \"top_p\": 123,\n \"fallback\": [\n {}\n ]\n}"
response = http.request(request)
puts response.read_bodyPOST /v1/chat/completions
OpenAI Chat Completions API와 100% 호환되는 엔드포인트입니다. 기존 OpenAI SDK에서base_url만 변경하면 바로 사용할 수 있습니다.
헤더
Bearer kr-your-api-keyapplication/json요청 본문
모델 ID.
kr/gpt54, kr/claude-sonnet-46, auto-smart 등.메시지 배열. 각 메시지는
role (system, user, assistant)과 content (문자열)를 포함합니다. 최대 100개.true 시 SSE 스트리밍 응답.0~2. 낮을수록 결정적, 높을수록 창의적.
최대 출력 토큰 수. 최대 128,000.
Nucleus sampling 파라미터.
(고급) 폴백 모델 목록. 주 모델 실패 시 순서대로 시도합니다. 대부분의 경우 원하는 모델을 직접 지정하는 것으로 충분합니다. 예:
["kr/gpt41", "kr/claude-sonnet-46"]응답
200 - 성공
{
"id": "chatcmpl-abc123",
"object": "chat.completion",
"created": 1711000000,
"model": "kr/gpt54",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "안녕하세요! 무엇을 도와드릴까요?"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 10,
"completion_tokens": 15,
"total_tokens": 25
}
}
SSE 스트리밍
stream: true 설정 시:
data: {"id":"chatcmpl-abc123","object":"chat.completion.chunk","created":1711000000,"model":"kr/gpt54","choices":[{"index":0,"delta":{"content":"안녕"},"finish_reason":null}]}
data: {"id":"chatcmpl-abc123","object":"chat.completion.chunk","created":1711000000,"model":"kr/gpt54","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}
data: [DONE]
예시
from openai import OpenAI
client = OpenAI(
api_key="kr-your-api-key",
base_url="https://api.k-router.com/v1"
)
response = client.chat.completions.create(
model="kr/gpt54",
messages=[{"role": "user", "content": "안녕하세요"}],
temperature=0.7,
max_tokens=1000
)
print(response.choices[0].message.content)
import OpenAI from "openai";
const client = new OpenAI({
apiKey: "kr-your-api-key",
baseURL: "https://api.k-router.com/v1",
});
const response = await client.chat.completions.create({
model: "kr/gpt54",
messages: [{ role: "user", content: "안녕하세요" }],
});
console.log(response.choices[0].message.content);
curl -X POST https://api.k-router.com/v1/chat/completions \
-H "Authorization: Bearer kr-your-api-key" \
-H "Content-Type: application/json" \
-d '{
"model": "kr/gpt54",
"messages": [{"role": "user", "content": "안녕하세요"}],
"stream": false
}'
⌘I