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

# Chat Completions

> OpenAI-compatible chat completion endpoint with streaming support

## Request Body

<ParamField body="chatId" type="string" required>
  Chat session identifier
</ParamField>

<ParamField body="messages" type="array" required>
  Array of message objects

  <Expandable title="message properties">
    <ResponseField name="role" type="string" required>
      Message role: `system`, `user`, or `assistant`
    </ResponseField>

    <ResponseField name="content" type="string" required>
      Message content
    </ResponseField>

    <ResponseField name="name" type="string">
      Optional name for the message sender
    </ResponseField>
  </Expandable>
</ParamField>

<ParamField body="model" type="string" default="gpt-4o">
  Model identifier
</ParamField>

<ParamField body="provider" type="string">
  Provider name (e.g., OpenAI, Anthropic, Google)
</ParamField>

<ParamField body="temperature" type="number" default="0.7">
  Sampling temperature (0.0 to 2.0)
</ParamField>

<ParamField body="max_tokens" type="number">
  Maximum number of tokens to generate
</ParamField>

<ParamField body="stream" type="boolean" default="false">
  Enable streaming response
</ParamField>

## Response

<ResponseField name="id" type="string">
  Completion ID
</ResponseField>

<ResponseField name="object" type="string">
  Object type (chat.completion)
</ResponseField>

<ResponseField name="created" type="number">
  Unix timestamp of creation
</ResponseField>

<ResponseField name="model" type="string">
  Model used for completion
</ResponseField>

<ResponseField name="choices" type="array">
  Array of completion choices
</ResponseField>

<ResponseField name="usage" type="object">
  Token usage information
</ResponseField>

<RequestExample>
  ```bash cURL theme={null}
  curl --request POST \
    --url https://api.yourapp.com/api/ai/chat/completions \
    --header 'Authorization: Bearer <token>' \
    --header 'Content-Type: application/json' \
    --data '{
      "chatId": "chat_123",
      "messages": [
        {
          "role": "system",
          "content": "You are a helpful assistant."
        },
        {
          "role": "user",
          "content": "What is the capital of France?"
        }
      ],
      "model": "gpt-4o",
      "temperature": 0.7,
      "stream": false
    }'
  ```

  ```javascript JavaScript theme={null}
  const response = await fetch('https://api.yourapp.com/api/ai/chat/completions', {
    method: 'POST',
    headers: {
      'Authorization': 'Bearer <token>',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      chatId: 'chat_123',
      messages: [
        {
          role: 'system',
          content: 'You are a helpful assistant.'
        },
        {
          role: 'user',
          content: 'What is the capital of France?'
        }
      ],
      model: 'gpt-4o',
      temperature: 0.7,
      stream: false
    })
  });

  const data = await response.json();
  ```

  ```python Python theme={null}
  import requests

  url = "https://api.yourapp.com/api/ai/chat/completions"
  headers = {
      "Authorization": "Bearer <token>",
      "Content-Type": "application/json"
  }
  payload = {
      "chatId": "chat_123",
      "messages": [
          {
              "role": "system",
              "content": "You are a helpful assistant."
          },
          {
              "role": "user",
              "content": "What is the capital of France?"
          }
      ],
      "model": "gpt-4o",
      "temperature": 0.7,
      "stream": False
  }

  response = requests.post(url, json=payload, headers=headers)
  data = response.json()
  ```
</RequestExample>

<ResponseExample>
  ```json Success Response theme={null}
  {
    "id": "chatcmpl-123",
    "object": "chat.completion",
    "created": 1677652288,
    "model": "gpt-4o",
    "choices": [
      {
        "index": 0,
        "message": {
          "role": "assistant",
          "content": "The capital of France is Paris."
        },
        "finish_reason": "stop"
      }
    ],
    "usage": {
      "prompt_tokens": 20,
      "completion_tokens": 8,
      "total_tokens": 28
    }
  }
  ```

  ```json Streaming Response theme={null}
  data: {"id":"chatcmpl-123","object":"chat.completion.chunk","created":1677652288,"model":"gpt-4o","choices":[{"index":0,"delta":{"role":"assistant","content":"The"},"finish_reason":null}]}

  data: {"id":"chatcmpl-123","object":"chat.completion.chunk","created":1677652288,"model":"gpt-4o","choices":[{"index":0,"delta":{"content":" capital"},"finish_reason":null}]}

  data: {"id":"chatcmpl-123","object":"chat.completion.chunk","created":1677652288,"model":"gpt-4o","choices":[{"index":0,"delta":{"content":" of"},"finish_reason":null}]}

  data: [DONE]
  ```

  ```json Error Response theme={null}
  {
    "success": false,
    "message": "Unauthorized"
  }
  ```
</ResponseExample>
