> ## Documentation Index
> Fetch the complete documentation index at: https://docs.compilelabs.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Chat Completions (OpenAI)

> Build chat-based AI applications using OpenAI-compatible API

## Overview

Chat completions are the primary interface for text generation and conversational AI. Use the OpenAI-compatible API to access language models from leading providers.

## Quick Start

```bash theme={null}
curl https://api.compilelabs.com/v1/chat/completions \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "moonshotai/kimi-k2-0905",
    "messages": [
      {"role": "user", "content": "Hello! What can you do?"}
    ]
  }'
```

## Using the OpenAI Python SDK

```python theme={null}
import openai

client = openai.OpenAI(
    api_key="YOUR_API_KEY",
    base_url="https://api.compilelabs.com/v1"
)

response = client.chat.completions.create(
    model="moonshotai/kimi-k2-0905",
    messages=[
        {"role": "user", "content": "Hello!"}
    ]
)

print(response.choices[0].message.content)
```

## Core Concepts

### Messages

Messages are organized by role:

* `system`: Instructions for the assistant's behavior
* `user`: User inputs and queries
* `assistant`: Previous assistant responses

### Parameters

* **model**: The model to use (e.g., `moonshotai/kimi-k2-0905`)
* **messages**: Array of message objects
* **temperature**: Randomness (0-2, default 1)
* **max\_tokens**: Maximum response length
* **stream**: Return streaming response

## Streaming

Stream responses for better user experience:

```bash theme={null}
curl https://api.compilelabs.com/v1/chat/completions \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "moonshotai/kimi-k2-0905",
    "messages": [
      {"role": "user", "content": "Tell me a long story"}
    ],
    "stream": true
  }'
```

## Multi-Turn Conversations

```python theme={null}
messages = [
    {"role": "system", "content": "You are a helpful assistant."},
    {"role": "user", "content": "What's 2+2?"},
    {"role": "assistant", "content": "2+2 equals 4."},
    {"role": "user", "content": "What about 3+3?"}
]

response = client.chat.completions.create(
    model="moonshotai/kimi-k2-0905",
    messages=messages
)
```

## Listing Available Models

Get a list of all available models:

```bash theme={null}
curl "https://api.compilelabs.com/v1/models?type=text_to_text" \
  -H "Authorization: Bearer YOUR_API_KEY"
```

## Next Steps

<CardGroup cols={2}>
  <Card title="Image Generation" icon="image" href="/modalities/image-generation">
    Generate images with AI models
  </Card>

  <Card title="Examples" icon="code" href="/examples">
    See more complete examples
  </Card>

  <Card title="API Reference" icon="book" href="/api-reference">
    Full API documentation
  </Card>
</CardGroup>
