Chat Completions API
Create conversational AI applications with the Chat Completions endpoint.
Endpoint
POST https://api.conduit.im/v1/chat/completionsRequest Body
| Parameter | Type | Required | Description |
|---|---|---|---|
| model | string | Required | The model to use for completion |
| messages | array | Required | List of messages in the conversation |
| max_tokens | integer | Optional | Maximum number of tokens to generate |
| temperature | number | Optional | Controls randomness (0.0 to 2.0) |
| top_p | number | Optional | Nucleus sampling parameter (0.0 to 1.0) |
| stream | boolean | Optional | Whether to stream the response |
Message Format
Each message in the messages array should have the following structure:
{
"role": "user" | "assistant" | "system",
"content": "The message content"
}system: Instructions for the AI assistant
user: Messages from the user
assistant: Previous AI responses in the conversation
Example Request
curl -X POST "https://api.conduit.im/v1/chat/completions" \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-4",
"messages": [
{
"role": "system",
"content": "You are a helpful programming assistant."
},
{
"role": "user",
"content": "How do I create a simple REST API with Node.js?"
}
],
"max_tokens": 500,
"temperature": 0.7
}'Example Response
{
"id": "chatcmpl-abc123",
"object": "chat.completion",
"created": 1677858242,
"model": "gpt-4",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "To create a simple REST API with Node.js, you can use Express.js. Here's a basic example:\n\n1. First, initialize your project and install Express:\n\n```bash\nnpm init -y\nnpm install express\n```\n\n2. Create a simple server:\n\n```javascript\nconst express = require('express');\nconst app = express();\nconst PORT = 3000;\n\napp.use(express.json());\n\napp.get('/api/users', (req, res) => {\n res.json({ users: [] });\n});\n\napp.listen(PORT, () => {\n console.log(`Server running on port ${PORT}`);\n});\n```\n\nThis creates a basic API with one endpoint."
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 45,
"completion_tokens": 187,
"total_tokens": 232
}
}Response Fields
| Field | Type | Description |
|---|---|---|
| id | string | Unique identifier for the completion |
| choices | array | List of completion choices |
| usage | object | Token usage information |
| created | integer | Unix timestamp of creation |
Error Responses
When an error occurs, the API returns an error object:
{
"error": {
"message": "Invalid API key provided",
"type": "invalid_request_error",
"code": "invalid_api_key"
}
}Common Error Codes:
invalid_api_key- Your API key is invalidmodel_not_found- The specified model doesn't existinvalid_request_error- Your request is malformedrate_limit_exceeded- You've exceeded your rate limitinsufficient_quota- You don't have enough credits