Skip to main content
Conduit.im

Chat Completions API

Create conversational AI applications with the Chat Completions endpoint.

Endpoint

POST https://api.conduit.im/v1/chat/completions

Request Body

ParameterTypeRequiredDescription
modelstringRequiredThe model to use for completion
messagesarrayRequiredList of messages in the conversation
max_tokensintegerOptionalMaximum number of tokens to generate
temperaturenumberOptionalControls randomness (0.0 to 2.0)
top_pnumberOptionalNucleus sampling parameter (0.0 to 1.0)
streambooleanOptionalWhether 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

FieldTypeDescription
idstringUnique identifier for the completion
choicesarrayList of completion choices
usageobjectToken usage information
createdintegerUnix 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 invalid
  • model_not_found - The specified model doesn't exist
  • invalid_request_error - Your request is malformed
  • rate_limit_exceeded - You've exceeded your rate limit
  • insufficient_quota - You don't have enough credits