Together AI 🤝

Access 100+ open-source models with unified API.

🚀

What you can do: Chat completions, embeddings, image generation, code models, and streaming - all with open-source models at competitive prices.

Setup

Add your Together AI API key in the ProtectMyAPI Dashboard.


Chat Completions

Basic Chat

let together = ProtectMyAPI.togetherService()
 
let response = try await together.createChatCompletion(
    request: TogetherChatRequest(
        model: "meta-llama/Llama-3.1-70B-Instruct-Turbo",
        messages: [
            .system("You are a helpful assistant."),
            .user("Explain microservices architecture")
        ]
    )
)
 
print(response.choices.first?.message?.content ?? "")

With Parameters

let response = try await together.createChatCompletion(
    request: TogetherChatRequest(
        model: "meta-llama/Llama-3.1-70B-Instruct-Turbo",
        messages: [.user("Write a creative story")],
        temperature: 0.8,
        maxTokens: 2000,
        topP: 0.95,
        topK: 50,
        repetitionPenalty: 1.1
    )
)

Streaming

for try await chunk in together.createChatCompletionStream(
    request: TogetherChatRequest(
        model: "meta-llama/Llama-3.1-70B-Instruct-Turbo",
        messages: [.user("Write a long essay about AI")]
    )
) {
    print(chunk.choices.first?.delta?.content ?? "", terminator: "")
}

Embeddings

Create vector embeddings for text:

let embeddings = try await together.createEmbeddings(
    request: TogetherEmbeddingsRequest(
        model: "togethercomputer/m2-bert-80M-8k-retrieval",
        input: [
            "The quick brown fox jumps over the lazy dog",
            "Machine learning is a subset of artificial intelligence"
        ]
    )
)
 
for embedding in embeddings.data {
    print("Vector dimensions: \(embedding.embedding.count)")
}

Image Generation

Generate images with FLUX and other models:

let image = try await together.createImage(
    request: TogetherImageRequest(
        model: "black-forest-labs/FLUX.1-schnell-Free",
        prompt: "A futuristic city at sunset, cyberpunk style",
        width: 1024,
        height: 1024,
        steps: 20,
        n: 1
    )
)
 
if let imageData = image.data.first {
    // imageData.b64Json contains the base64-encoded image
    let data = Data(base64Encoded: imageData.b64Json!)
}

Code Models

For code generation and analysis:

let response = try await together.createChatCompletion(
    request: TogetherChatRequest(
        model: "codellama/CodeLlama-70b-Instruct-hf",
        messages: [
            .system("You are an expert programmer."),
            .user("Write a Swift function to implement binary search")
        ],
        temperature: 0.1 // Lower for more deterministic code
    )
)

Chat Models

ModelParametersBest For
meta-llama/Llama-3.1-405B-Instruct-Turbo405BHighest quality
meta-llama/Llama-3.1-70B-Instruct-Turbo70BBest balance
meta-llama/Llama-3.1-8B-Instruct-Turbo8BFast responses
mistralai/Mixtral-8x22B-Instruct-v0.18x22BMoE efficiency
Qwen/Qwen2-72B-Instruct72BMultilingual

Code Models

ModelDescription
codellama/CodeLlama-70b-Instruct-hfBest code generation
deepseek-ai/deepseek-coder-33b-instructCode analysis
WizardLM/WizardCoder-Python-34B-V1.0Python specialist

Image Models

ModelDescription
black-forest-labs/FLUX.1-schnell-FreeFree, fast
black-forest-labs/FLUX.1-schnellFaster generation
stabilityai/stable-diffusion-xl-base-1.0SDXL base

Embedding Models

ModelDimensions
togethercomputer/m2-bert-80M-8k-retrieval768
BAAI/bge-large-en-v1.51024
sentence-transformers/msmarco-bert-base-dot-v5768

JSON Mode

Get structured JSON responses:

let response = try await together.createChatCompletion(
    request: TogetherChatRequest(
        model: "meta-llama/Llama-3.1-70B-Instruct-Turbo",
        messages: [
            .system("Return JSON only"),
            .user("List 3 programming languages with their use cases")
        ],
        responseFormat: .json
    )
)

Pricing

Together AI offers competitive pricing for open-source models. Generally:

  • Inference: $0.10-0.90 per million tokens (varies by model)
  • Images: $0.003-0.025 per image
  • Embeddings: $0.008 per million tokens

Check their pricing page for current rates.