> ## Documentation Index
> Fetch the complete documentation index at: https://60db.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Chat

> Powerful AI chat with text correction, streaming, and function calling

## Overview

60db's LM (Large Language Model) API provides intelligent chat completions with advanced text correction capabilities, streaming responses, and function calling support. Our Small Language Model (SLM) is optimized for fast, efficient responses.

## Features

<CardGroup cols={2}>
  <Card title="OpenAI Compatible" icon="code">
    Drop-in compatible with OpenAI's chat completion format
  </Card>

  <Card title="Text Correction" icon="edit">
    Smart text correction with dictionary and style options
  </Card>

  <Card title="Real-time Streaming" icon="signal-stream">
    Server-Sent Events for instant response streaming
  </Card>

  <Card title="Function Calling" icon="zap">
    Built-in tool/function calling support
  </Card>
</CardGroup>

## Basic Usage

<Tabs>
  <Tab title="JavaScript">
    ```javascript theme={null}
    import { SixtyDBClient } from '60db';

    const client = new SixtyDBClient('your-api-key');

    const response = await client.chat.completions.create({
      model: 'qcall/slm-3b-int4',
      messages: [
        { role: 'system', content: 'You are a helpful assistant.' },
        { role: 'user', content: 'How can I improve my English?' }
      ],
      stream: true
    });

    for await (const chunk of response) {
      console.log(chunk.choices[0]?.delta?.content);
    }
    ```
  </Tab>

  <Tab title="Python">
    ```python theme={null}
    from sixtydb import SixtyDBClient

    client = SixtyDBClient('your-api-key')

    response = client.chat.completion(
        model='qcall/slm-3b-int4',
        messages=[
            {'role': 'system', 'content': 'You are a helpful assistant.'},
            {'role': 'user', 'content': 'How can I improve my English?'}
        ],
        stream=True
    )

    print(response['choices'][0]['message']['content'])
    ```
  </Tab>

  <Tab title="cURL">
    ```bash theme={null}
    curl --location 'https://api.60db.ai/v1/chat/completions' \
    --header 'Content-Type: application/json' \
    --data '{
      "model": "qcall/slm-3b-int4",
      "messages": [
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Hello! How are you?"}
      ],
      "stream": true
    }'
    ```
  </Tab>
</Tabs>

## Streaming Responses

Real-time streaming with Server-Sent Events:

```javascript theme={null}
const response = await fetch("https://api.60db.ai/v1/chat/completions", {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({
    model: "qcall/slm-3b-int4",
    messages: [{ role: "user", content: "Tell me a story" }],
    stream: true,
  }),
});

const reader = response.body.getReader();
const decoder = new TextDecoder();

while (true) {
  const { done, value } = await reader.read();
  if (done) break;

  const chunk = decoder.decode(value);
  const lines = chunk.split("\n");

  for (const line of lines) {
    if (line.startsWith("data: ")) {
      const data = JSON.parse(line.slice(6));
      if (data.choices?.[0]?.delta?.content) {
        console.log(data.choices[0].delta.content);
      }
    }
  }
}
```

## Function Calling (Tools)

Define tools the model can use:

```javascript theme={null}
const response = await client.chat.completions.create({
  model: "qcall/slm-3b-int4",
  messages: [
    { role: "user", content: "What is the weather in San Francisco?" },
  ],
  tool: [
    {
      name: "get_weather",
      description: "Get the current weather for a location",
      parameters: {
        type: "object",
        properties: {
          location: {
            type: "string",
            description: "The city and state, e.g. San Francisco, CA",
          },
          unit: {
            type: "string",
            enum: ["celsius", "fahrenheit"],
          },
        },
        required: ["location"],
      },
    },
  ],
});
```

### Streaming Response (SSE)

```
data: {"chat_id": "550e8400-e29b-41d4-a716-446655440000", "type": "chat_id"}

data: {"id": "chatcmpl-123", "choices": [{"delta": {"content": "I'm"}}]}

data: {"id": "chatcmpl-123", "choices": [{"delta": {"content": " doing"}}]}

data: {"id": "chatcmpl-123", "choices": [{"delta": {"content": " well!"}}]}

data: {"type": "done", "response_time_ms": 1250}

data: [DONE]
```

## Application Contexts

Supported contexts for text correction:

| Context    | Description                      |
| ---------- | -------------------------------- |
| `email`    | Professional email communication |
| `chat`     | Casual chat/messaging            |
| `document` | Formal documents/reports         |
| `message`  | Short messages/notifications     |
| `social`   | Social media posts               |

```javascript theme={null}
const response = await client.chat.completions.create({
  text: "hey can u send me the report pls",
  appContext: "email",
  style: { tone: "professional", autoCapitalize: true },
});

// Output: "Hey, can you send me the report, please?"
```

## Best Practices

<AccordionGroup>
  <Accordion title="Prompt Engineering">
    * Be specific in your system prompt
    * Provide examples for few-shot learning
    * Use appropriate tone for your use case
    * Test different prompts for optimal results
  </Accordion>

  <Accordion title="Streaming">
    * Use streaming for better UX - Handle connection errors gracefully - Buffer
      chunks for smooth display - Implement timeout handling
  </Accordion>

  <Accordion title="Cost Optimization">
    * Cache common responses - Use shorter prompts when possible - Enable chat
      history for context - Monitor token usage
  </Accordion>

  <Accordion title="Text Correction">
    * Use dictionary for domain-specific terms
    * Set appropriate app context
    * Test style options for your use case
    * Combine multiple style options
  </Accordion>
</AccordionGroup>

## Use Cases

### Customer Support Chatbot

```javascript theme={null}
async function handleCustomerMessage(message) {
  const response = await client.chat.completions.create({
    model: "qcall/slm-3b-int4",
    messages: [
      {
        role: "system",
        content:
          "You are a helpful customer support assistant. Be friendly and professional.",
      },
      { role: "user", content: message },
    ],
    stream: true,
    save_chat: true,
    chat_id: customerId,
  });

  return response;
}
```

### Text Correction Service

```javascript theme={null}
async function correctText(text, context = "email") {
  const response = await client.chat.completions.create({
    text: text,
    appContext: context,
    dictionary: commonTypos,
    style: {
      tone: "professional",
      autoCapitalize: true,
      autoPunctuate: true,
    },
  });

  return response.choices[0].message.content;
}
```

### AI Assistant with Tools

```javascript theme={null}
async function aiAssistant(userQuery) {
  const response = await client.chat.completions.create({
    messages: [{ role: "user", content: userQuery }],
    tool: [getWeatherTool, searchDatabaseTool, sendEmailTool],
  });

  // Handle tool calls and return results
  return response;
}
```

## Pricing

Charges are based on token usage:

| Metric    | Cost        |
| --------- | ----------- |
| Per Token | \~\$0.00002 |

Token usage is tracked and billed to your workspace. Monitor usage via the Analytics API.

## API Reference

For detailed API documentation, see:

<CardGroup cols={1}>
  <Card title="Chat" icon="code" href="/api-reference/llm/chat-completion">
    Complete API reference with all parameters and examples
  </Card>
</CardGroup>
