# Chat Completion

{% hint style="danger" %}
**This feature is in beta.** It is free to use but may not be optimized for high-scale production environments.
{% endhint %}

### **Introduction**

This API provides an **AI-powered chat completion endpoint**. It takes user messages and generates responses using a lightweight AI model. The API **does not store user messages**—all responses are generated in real-time and discarded after processing. To be frank, I'm too broke to pay for D1 storage for your messages.&#x20;

***

### **Endpoint**

```http
POST  https://api.elliott.diy/v1/ai/chat/completions
```

***

### **Request Parameters**

| Parameter  | Type  | Required | Description                                                                |
| ---------- | ----- | -------- | -------------------------------------------------------------------------- |
| `messages` | Array | ✅ Yes    | A list of chat messages, where the last message is used for AI generation. |

#### **Example Request**

```bash
curl -X POST  https://api.elliott.diy/v1/ai/chat/completions/ \
     -H "Content-Type: application/json" \
     -d '{
       "messages": [
         { "role": "user", "content": "Hello, how are you?" }
       ]
     }'
```

***

### **Response Format**

* **Content-Type:** `application/json`
* **Status Codes:**
  * `200 OK` – Successful response
  * `400 Bad Request` – Invalid JSON or missing messages
  * `500 Internal Server Error` – AI service is unavailable

#### **Example Response**

```json
{
  "id": "cfai-12345678-1234-5678-1234-567812345678",
  "object": "chat.completion",
  "created": 1710766200,
  "model": "elliott-1",
  "choices": [
    {
      "index": 0,
      "message": { "role": "assistant", "content": "I'm doing great! How can I assist you?" },
      "finish_reason": "stop"
    }
  ]
}
```

***

### **Response Fields**

| Field             | Type    | Description                                          |
| ----------------- | ------- | ---------------------------------------------------- |
| `id`              | String  | Unique identifier for the response.                  |
| `object`          | String  | Always `"chat.completion"`.                          |
| `created`         | Integer | UNIX timestamp of when the response was generated.   |
| `model`           | String  | Always `"elliott-1"`.                                |
| `choices`         | Array   | Contains AI-generated message(s).                    |
| `message.role`    | String  | Always `"assistant"`.                                |
| `message.content` | String  | The AI-generated response.                           |
| `finish_reason`   | String  | Indicates why the response stopped (e.g., `"stop"`). |

***

### **Model Information**

* **Base Model Used:** `@hf/google/gemma-7b-it`
* **Custom Model Name:** `"elliott-1"` (I'm so creative)
* The model generates responses based on the **last user message** in the request.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.elliott.diy/ai/chat-completion.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
