# Together AI Integration via LowCodeAPI

## Overview

Together AI makes it easy to run, finetune, and train open source AI models with transparency and privacy. The Together AI API provides:

- **Chat Completions** - Generate responses with chat models
- **Text Completions** - Generate text with language models
- **Embeddings** - Create vector embeddings for text
- **Image Generation** - Generate images with diffusion models
- **Model List** - Browse available open source models

## Base Endpoint

```
https://api.lowcodeapi.com/togetherai/
```

## Authentication

LowCodeAPI handles authentication automatically using Bearer token authentication. You only need to:

1. **Sign up** at [Together AI](https://www.together.ai) to get your API Key
2. **Connect your account** in the LowCodeAPI dashboard
3. **Use your `api_token`** in all requests

The `api_token` is your LowCodeAPI authentication token. LowCodeAPI will automatically:
- Fetch your Together AI API key
- Apply it to each request as a Bearer token

**Auth Type**: Bearer Token

## API Categories

- **AI Cloud** - Open source AI model inference

## Common Endpoints

### Category: Chat

#### Create Chat Completion

**Method**: `POST` | **LowCodeAPI Path**: `/v1/chat/completions`

**Full URL**:
```
https://api.lowcodeapi.com/togetherai/v1/chat/completions?api_token={api_token}
```

**Description**: Generate chat completions using open source language models.

**Request Body**:
```json
{
  "model": "meta-llama/Llama-3-8b-chat-hf",
  "messages": [
    {
      "role": "system",
      "content": "You are a helpful assistant."
    },
    {
      "role": "user",
      "content": "Explain quantum computing in simple terms."
    }
  ],
  "max_tokens": 1024,
  "temperature": 0.7,
  "top_p": 0.7,
  "top_k": 50
}
```

**Request Body Fields**:

| Field | Type | Required | Description |
|-------|------|----------|-------------|
| `model` | string | Yes | ID of the model to use |
| `messages` | array | Yes | List of messages in the conversation |
| `max_tokens` | integer | No | Maximum tokens to generate |
| `temperature` | number | No | Sampling temperature (0-2, default: depends on model) |
| `top_p` | number | No | Nucleus sampling threshold |
| `top_k` | number | No | Limit to K most probable tokens |
| `repetition_penalty` | number | No | Penalty for repetition (>1 penalize, <1 encourage) |
| `stop` | array | No | Up to 4 sequences where generation stops |
| `stream` | boolean | No | Stream partial message deltas |
| `tools` | array | No | List of tools (functions) the model may call |

**Example Request**:
```bash
curl -X POST "https://api.lowcodeapi.com/togetherai/v1/chat/completions?api_token=YOUR_API_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "meta-llama/Llama-3-8b-chat-hf",
    "messages": [
      {"role": "system", "content": "You are a helpful assistant."},
      {"role": "user", "content": "Explain quantum computing in simple terms."}
    ],
    "max_tokens": 1024,
    "temperature": 0.7
  }'
```

**Official Documentation**: [Chat Completions](https://docs.together.ai/intro)

---

#### Create Completion

**Method**: `POST` | **LowCodeAPI Path**: `/v1/completions`

**Full URL**:
```
https://api.lowcodeapi.com/togetherai/v1/completions?api_token={api_token}
```

**Description**: Generate text completions using language models.

**Request Body**:
```json
{
  "model": "mistralai/Mistral-7B-Instruct-v0.2",
  "prompt": "Once upon a time in a land far away,",
  "max_tokens": 512,
  "temperature": 0.8,
  "top_p": 0.9,
  "top_k": 40
}
```

**Request Body Fields**:

| Field | Type | Required | Description |
|-------|------|----------|-------------|
| `model` | string | Yes | ID of the model to use |
| `prompt` | string | Yes | Text prompt for completion |
| `max_tokens` | integer | No | Maximum tokens to generate |
| `temperature` | number | No | Sampling temperature |
| `top_p` | number | No | Nucleus sampling threshold |
| `top_k` | number | No | Top-K sampling parameter |
| `repetition_penalty` | number | No | Repetition penalty |
| `stop` | array | No | Stop sequences |

**Example Request**:
```bash
curl -X POST "https://api.lowcodeapi.com/togetherai/v1/completions?api_token=YOUR_API_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "mistralai/Mistral-7B-Instruct-v0.2",
    "prompt": "Once upon a time in a land far away,",
    "max_tokens": 512,
    "temperature": 0.8
  }'
```

**Official Documentation**: [Completions](https://docs.together.ai/intro)

---

### Category: Models

#### List Models

**Method**: `GET` | **LowCodeAPI Path**: `/v1/models`

**Full URL**:
```
https://api.lowcodeapi.com/togetherai/v1/models?api_token={api_token}
```

**Description**: List all available models on the Together AI platform.

**Example Request**:
```bash
curl -X GET "https://api.lowcodeapi.com/togetherai/v1/models?api_token=YOUR_API_TOKEN"
```

**Official Documentation**: [List Models](https://docs.together.ai/intro)

---

### Category: Embeddings

#### Create Embeddings

**Method**: `POST` | **LowCodeAPI Path**: `/v1/embeddings`

**Full URL**:
```
https://api.lowcodeapi.com/togetherai/v1/embeddings?api_token={api_token}
```

**Description**: Create vector embeddings for text input.

**Request Body**:
```json
{
  "model": "togethercomputer/m2-bert-80M-32k-retrieval",
  "input": "This is a sample text to create embeddings for."
}
```

**Request Body Fields**:

| Field | Type | Required | Description |
|-------|------|----------|-------------|
| `model` | string | Yes | Embedding model to use |
| `input` | string | Yes | Text to create embeddings for |

**Example Request**:
```bash
curl -X POST "https://api.lowcodeapi.com/togetherai/v1/embeddings?api_token=YOUR_API_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "togethercomputer/m2-bert-80M-32k-retrieval",
    "input": "This is a sample text to create embeddings for."
  }'
```

**Official Documentation**: [Embeddings](https://docs.together.ai/intro)

---

## Usage Examples

### Example 1: Chat Completion

Interactive chat with AI:

```bash
# Simple chat request
# No ID needed - generates new completion
curl -X POST "https://api.lowcodeapi.com/togetherai/v1/chat/completions?api_token=YOUR_API_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "meta-llama/Llama-3-8b-chat-hf",
    "messages": [
      {"role": "system", "content": "You are a helpful assistant."},
      {"role": "user", "content": "What are the benefits of open source AI?"}
    ],
    "max_tokens": 1024,
    "temperature": 0.7
  }'
```

### Example 2: Text Completion

Generate text continuations:

```bash
# Complete text
# No ID needed - generates new completion
curl -X POST "https://api.lowcodeapi.com/togetherai/v1/completions?api_token=YOUR_API_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "mistralai/Mistral-7B-Instruct-v0.2",
    "prompt": "The future of artificial intelligence is",
    "max_tokens": 512,
    "temperature": 0.8,
    "top_p": 0.9
  }'
```

### Example 3: Create Embeddings

Generate vector embeddings:

```bash
# Create embeddings
# No ID needed - generates new embeddings
curl -X POST "https://api.lowcodeapi.com/togetherai/v1/embeddings?api_token=YOUR_API_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "togethercomputer/m2-bert-80M-32k-retrieval",
    "input": "Search for relevant documents using semantic similarity"
  }'

# List available models
# No ID needed - lists all models
curl -X GET "https://api.lowcodeapi.com/togetherai/v1/models?api_token=YOUR_API_TOKEN"
```

## Complete Endpoint Reference

For a complete list of all 24 endpoints and their parameters, refer to:

- **OpenAPI Definition**: https://backend.lowcodeapi.com/togetherai/definition
- **Official Together AI Documentation**: https://docs.together.ai/intro

## Rate Limits & Best Practices

- **Rate Limit**: Refer to your Together AI plan for rate limits
- **Best Practices**:
  - Choose appropriate model for your use case (size, speed, capability)
  - Use streaming for long responses
  - Implement proper prompt engineering for better results
  - Use repetition_penalty to reduce repetitive output
  - Cache model responses when possible
  - Set reasonable max_tokens to manage costs
  - Use lower temperatures for more focused outputs

## Error Handling

All responses are wrapped in a `data` key:
```json
{
  "data": {
    // Actual response from Together AI
  }
}
```

Common errors:
- **400**: Invalid request parameters
- **401**: Invalid API key
- **429**: Rate limit exceeded
- **500**: Model inference failed