Docs
Chat Completions
Chat Completions
Use the chat completions endpoint to create conversational AI responses.
The chat completions endpoint creates AI responses for conversational messages. This is the primary endpoint for most applications.
Endpoint
POST https://api.elyxir.ai/v1/chat/completions
Request
Headers
| Header | Required | Description |
|---|---|---|
| Authorization | Yes | Bearer token: Bearer elyxir_xxx |
| Content-Type | Yes | Must be application/json |
Body Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
| model | string | Yes | Model ID (e.g., gpt-4o-mini) |
| messages | array | Yes | Array of message objects |
| temperature | number | No | Randomness (0-2, default: 1) |
| max_tokens | integer | No | Maximum response tokens |
| stream | boolean | No | Enable streaming (default: false) |
| top_p | number | No | Nucleus sampling (0-1) |
| stop | string/array | No | Stop sequences |
| presence_penalty | number | No | Penalize new topics (-2 to 2) |
| frequency_penalty | number | No | Penalize repetition (-2 to 2) |
Message Object
| Field | Type | Required | Description |
|---|---|---|---|
| role | string | Yes | system, user, or assistant |
| content | string | Yes | Message content |
Example Request
curl -X POST https://api.elyxir.ai/v1/chat/completions \
-H "Authorization: Bearer elyxir_your_api_key" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-4o-mini",
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "What is the capital of France?"}
],
"temperature": 0.7,
"max_tokens": 150
}'Response
Success (200 OK)
{
"id": "chatcmpl-abc123",
"object": "chat.completion",
"created": 1234567890,
"model": "gpt-4o-mini",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "The capital of France is Paris."
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 25,
"completion_tokens": 10,
"total_tokens": 35
}
}Response Fields
| Field | Description |
|---|---|
| id | Unique completion ID |
| object | Object type (chat.completion) |
| created | Unix timestamp |
| model | Model used for completion |
| choices | Array of completion choices |
| usage | Token usage statistics |
Finish Reasons
| Reason | Description |
|---|---|
| stop | Natural completion or stop sequence |
| length | Hit max_tokens limit |
| content_filter | Filtered by content moderation |
Multi-turn Conversations
Include previous messages for context:
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "What is Python?"},
{"role": "assistant", "content": "Python is a programming language..."},
{"role": "user", "content": "How do I install it?"}
]
response = requests.post(
"https://api.elyxir.ai/v1/chat/completions",
headers=headers,
json={"model": "gpt-4o-mini", "messages": messages}
)Streaming
Enable streaming for real-time responses:
curl -X POST https://api.elyxir.ai/v1/chat/completions \
-H "Authorization: Bearer elyxir_your_api_key" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-4o-mini",
"messages": [{"role": "user", "content": "Tell me a story"}],
"stream": true
}'Streaming returns Server-Sent Events (SSE):
data: {"id":"chatcmpl-abc","choices":[{"delta":{"content":"The"}}]}
data: {"id":"chatcmpl-abc","choices":[{"delta":{"content":" capital"}}]}
data: [DONE]
Available Models
| Model | Provider | Best For |
|---|---|---|
| gpt-4o | OpenAI | General purpose |
| gpt-4o-mini | OpenAI | Cost-effective |
| claude-3-5-sonnet-20241022 | Anthropic | Coding, analysis |
| claude-3-haiku-20240307 | Anthropic | Fast, affordable |
| gemini-1.5-pro | Long context | |
| llama-3.2-90b | Meta | Open source |
Error Responses
| Status | Meaning |
|---|---|
| 400 | Bad request (invalid parameters) |
| 401 | Authentication failed |
| 402 | Insufficient credits |
| 429 | Rate limit exceeded |
| 500 | Server error |
See Error Handling for details.
Related
- API Overview - Getting started
- Authentication - API key setup
- Embeddings - Text embeddings