Skip to content

managedcode/together-dotnet

Repository files navigation

Together .NET SDK

C# SDK for Together.ai

Introduction

The Together .NET SDK provides a simple and efficient way to interact with the Together.ai API using C#. This SDK allows you to easily integrate various AI capabilities such as completions, chat completions, embeddings, and image generations into your .NET applications.

Features

  • Completions: Generate text completions based on a given prompt.
  • Chat Completions: Generate chat-based completions for conversational AI.
  • Embeddings: Generate vector embeddings for text.
  • Images: Generate images based on a given prompt.

Installation

To install the Together .NET SDK, add the following package to your project:

dotnet add package Together

Usage

Initialization

To use the SDK, you need to initialize the TogetherClient with an HttpClient:

using Together;
using System.Net.Http.Headers;

var httpClient = new HttpClient { BaseAddress = new Uri(TogetherConstants.BASE_URL) };
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "YOUR_API_KEY");
var client = new TogetherClient(httpClient);

Completions

To get a text completion:

var request = new CompletionRequest
{
    Prompt = "Hello, world!",
    Model = "meta-llama/Meta-Llama-3-70B-Instruct-Turbo"
};
var response = await client.GetCompletionResponseAsync(request);
Console.WriteLine(response.Choices.First().Text);

Chat Completions

To get a chat completion:

var request = new ChatCompletionRequest
{
    Messages = new List<ChatCompletionMessage> { new ChatCompletionMessage { Role = "user", Content = "Hello!" } },
    Model = "meta-llama/Meta-Llama-3-70B-Instruct-Turbo"
};
var response = await client.GetChatCompletionResponseAsync(request);
Console.WriteLine(response.Choices.First().Message.Content);

Embeddings

To get embeddings:

var request = new EmbeddingRequest
{
    Input = "Hello, world!",
    Model = "togethercomputer/m2-bert-80M-2k-retrieval"
};
var response = await client.GetEmbeddingResponseAsync(request);
Console.WriteLine(string.Join(", ", response.Data.First().Embedding));

Images

To generate an image:

var request = new ImageRequest
{
    Prompt = "A beautiful sunset over the mountains",
    Model = "black-forest-labs/FLUX.1-dev",
    N = 1,
    Steps = 10,
    Height = 512,
    Width = 512
};
var response = await client.GetImageResponseAsync(request);
Console.WriteLine(response.Data.First().Url);

Constants

The SDK provides various constants that can be used throughout your application. These constants are defined in the TogetherConstants class.

Contributing

Contributions are welcome! Please open an issue or submit a pull request on GitHub.

License

This project is licensed under the MIT License. See the LICENSE file for details.