Chat Completions
Generate text responses from our powerful language models.
Endpoint
POST
/v1/chat/completionsRequest Body
| Parameter | Type | Required | Description |
|---|---|---|---|
model | string | Yes | Model to use: pro, fast, or default |
messages | array | Yes | Array of message objects with role and content |
stream | boolean | No | Enable streaming responses. Default: false |
temperature | number | No | Sampling temperature (0-2). Default: 1 |
max_tokens | integer | No | Maximum tokens to generate |
response_format | object | No | Set to { "type": "json_object" } for JSON mode |
Message Format
Each message in the array must have a role and content:
{
"messages": [
{
"role": "system",
"content": "You are a helpful assistant."
},
{
"role": "user",
"content": "What is the capital of France?"
},
{
"role": "assistant",
"content": "The capital of France is Paris."
},
{
"role": "user",
"content": "What about Germany?"
}
]
} systemSets the behavior and context for the assistant
userMessages from the user/human
assistantPrevious responses (for conversation context)
Code Examples
curl https://api.llm.kiwi/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_API_KEY" \
-d '{
"model": "pro",
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hello!"}
]
}'Streaming
Set stream: true to receive responses as Server-Sent Events (SSE):
from openai import OpenAI
client = OpenAI(base_url = "https://api.llm.kiwi/v1", api_key = "YOUR_KEY")
stream = client.chat.completions.create(
model = "pro",
messages = [{ "role": "user", "content": "Write a poem" }],
stream = True
)
for chunk in stream:
if chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end = "")Response
{
"id": "chatcmpl-abc123",
"object": "chat.completion",
"created": 1703456789,
"model": "pro",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "The capital of France is Paris."
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 15,
"completion_tokens": 8,
"total_tokens": 23
}
}