|
1 | 1 | # Together .NET SDK |
2 | 2 |
|
3 | | -C# SDK for Together.ai |
| 3 | +[](https://github.com/managedcode/Together/actions/workflows/dotnet.yml) |
| 4 | +[](https://www.nuget.org/packages/ManagedCode.Together) |
| 5 | +[](https://www.nuget.org/packages/ManagedCode.Together.SemanticKernel) |
| 6 | +[](https://www.nuget.org/packages/ManagedCode.Together) |
| 7 | +[](https://github.com/managedcode/Together/blob/main/LICENSE) |
4 | 8 |
|
5 | | -## Introduction |
| 9 | +Unofficial C# SDK for [Together.ai](https://www.together.ai/) with Semantic Kernel integration. |
6 | 10 |
|
7 | | -The Together .NET SDK provides a simple and efficient way to interact with the Together.ai API using C#. This SDK allows |
8 | | -you to easily integrate various AI capabilities such as completions, chat completions, embeddings, and image generations |
9 | | -into your .NET applications. |
| 11 | +## Introduction |
10 | 12 |
|
11 | | -## Features |
| 13 | +Together.ai provides access to the latest open-source AI models through a simple API. This SDK offers: |
12 | 14 |
|
13 | | -- **Completions**: Generate text completions based on a given prompt. |
14 | | -- **Chat Completions**: Generate chat-based completions for conversational AI. |
15 | | -- **Embeddings**: Generate vector embeddings for text. |
16 | | -- **Images**: Generate images based on a given prompt. |
| 15 | +- 🚀 Easy access to Together.ai's API from .NET applications |
| 16 | +- 🧠 Seamless integration with Microsoft Semantic Kernel |
| 17 | +- 🔧 Chat Completions, Text Generation, Embeddings and Image Generation |
| 18 | +- 🌊 Both synchronous and streaming responses |
| 19 | +- 🛠 Built-in function calling support |
17 | 20 |
|
18 | 21 | ## Installation |
19 | 22 |
|
20 | | -To install the Together .NET SDK, add the following package to your project: |
| 23 | +Choose the package(s) you need: |
21 | 24 |
|
22 | 25 | ```sh |
23 | | -dotnet add package Together |
24 | | -``` |
25 | | - |
26 | | -## Usage |
27 | | - |
28 | | -### Initialization |
29 | | - |
30 | | -To use the SDK, you need to initialize the `TogetherClient` with an `HttpClient`: |
| 26 | +# Core API client |
| 27 | +dotnet add package ManagedCode.Together |
31 | 28 |
|
32 | | -```csharp |
33 | | -using Together; |
34 | | -using System.Net.Http.Headers; |
35 | | - |
36 | | -var httpClient = new HttpClient { BaseAddress = new Uri(TogetherConstants.BASE_URL) }; |
37 | | -httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "YOUR_API_KEY"); |
38 | | -var client = new TogetherClient(httpClient); |
| 29 | +# Semantic Kernel integration |
| 30 | +dotnet add package ManagedCode.Together.SemanticKernel |
39 | 31 | ``` |
40 | 32 |
|
41 | | -### Completions |
| 33 | +## Usage Examples |
42 | 34 |
|
43 | | -To get a text completion: |
| 35 | +### Direct API Usage |
44 | 36 |
|
45 | 37 | ```csharp |
46 | | -var request = new CompletionRequest |
47 | | -{ |
48 | | - Prompt = "Hello, world!", |
49 | | - Model = "meta-llama/Meta-Llama-3-70B-Instruct-Turbo" |
50 | | -}; |
51 | | -var response = await client.GetCompletionResponseAsync(request); |
52 | | -Console.WriteLine(response.Choices.First().Text); |
53 | | -``` |
54 | | - |
55 | | -### Chat Completions |
56 | | - |
57 | | -To get a chat completion: |
| 38 | +using Together; |
58 | 39 |
|
59 | | -```csharp |
60 | | -var request = new ChatCompletionRequest |
| 40 | +var client = new TogetherClient("YOUR_API_KEY"); |
| 41 | +var response = await client.ChatCompletions.CreateAsync(new ChatCompletionRequest |
61 | 42 | { |
62 | | - Messages = new List<ChatCompletionMessage> { new ChatCompletionMessage { Role = "user", Content = "Hello!" } }, |
63 | | - Model = "meta-llama/Meta-Llama-3-70B-Instruct-Turbo" |
64 | | -}; |
65 | | -var response = await client.GetChatCompletionResponseAsync(request); |
66 | | -Console.WriteLine(response.Choices.First().Message.Content); |
| 43 | + Model = "mistralai/Mistral-7B-Instruct-v0.1", |
| 44 | + Messages = new[] |
| 45 | + { |
| 46 | + new ChatCompletionMessage { Role = "user", Content = "Hello!" } |
| 47 | + } |
| 48 | +}); |
67 | 49 | ``` |
68 | 50 |
|
69 | | -### Embeddings |
70 | | - |
71 | | -To get embeddings: |
| 51 | +### Semantic Kernel Integration |
72 | 52 |
|
73 | 53 | ```csharp |
74 | | -var request = new EmbeddingRequest |
75 | | -{ |
76 | | - Input = "Hello, world!", |
77 | | - Model = "togethercomputer/m2-bert-80M-2k-retrieval" |
78 | | -}; |
79 | | -var response = await client.GetEmbeddingResponseAsync(request); |
80 | | -Console.WriteLine(string.Join(", ", response.Data.First().Embedding)); |
| 54 | +using Microsoft.SemanticKernel; |
| 55 | +using Together.SemanticKernel; |
| 56 | + |
| 57 | +// Initialize kernel with multiple Together.ai capabilities |
| 58 | +var kernel = Kernel.CreateBuilder() |
| 59 | + .AddTogetherChatCompletion( |
| 60 | + "mistralai/Mistral-7B-Instruct-v0.1", |
| 61 | + "YOUR_API_KEY" |
| 62 | + ) |
| 63 | + .AddTogetherTextEmbeddingGeneration( |
| 64 | + "togethercomputer/m2-bert-80M-2k-retrieval", |
| 65 | + "YOUR_API_KEY" |
| 66 | + ) |
| 67 | + .AddTogetherTextToImage( |
| 68 | + "stabilityai/stable-diffusion-xl-base-1.0", |
| 69 | + "YOUR_API_KEY" |
| 70 | + ) |
| 71 | + .Build(); |
| 72 | + |
| 73 | +// Chat completion |
| 74 | +var chatResult = await kernel.InvokePromptAsync("What is quantum computing?"); |
| 75 | + |
| 76 | +// Generate embeddings |
| 77 | +var embeddingService = kernel.GetRequiredService<ITextEmbeddingGenerationService>(); |
| 78 | +var embeddings = await embeddingService.GenerateEmbeddingsAsync( |
| 79 | + ["What is quantum computing?"] |
| 80 | +); |
| 81 | + |
| 82 | +// Generate images |
| 83 | +var imageService = kernel.GetRequiredService<ITextToImageService>(); |
| 84 | +var images = await imageService.GetImageContentsAsync( |
| 85 | + "A cat playing piano", |
| 86 | + new TogetherTextToImageExecutionSettings |
| 87 | + { |
| 88 | + Height = 512, |
| 89 | + Width = 512 |
| 90 | + } |
| 91 | +); |
81 | 92 | ``` |
82 | 93 |
|
83 | | -### Images |
| 94 | +## 📚 Documentation |
84 | 95 |
|
85 | | -To generate an image: |
| 96 | +For more information about available models and features, visit the [Together.ai Documentation](https://docs.together.ai/) |
86 | 97 |
|
87 | | -```csharp |
88 | | -var request = new ImageRequest |
89 | | -{ |
90 | | - Prompt = "A beautiful sunset over the mountains", |
91 | | - Model = "black-forest-labs/FLUX.1-dev", |
92 | | - N = 1, |
93 | | - Steps = 10, |
94 | | - Height = 512, |
95 | | - Width = 512 |
96 | | -}; |
97 | | -var response = await client.GetImageResponseAsync(request); |
98 | | -Console.WriteLine(response.Data.First().Url); |
99 | | -``` |
100 | | - |
101 | | -## Constants |
| 98 | +## 💪 Contributing |
102 | 99 |
|
103 | | -The SDK provides various constants that can be used throughout your application. These constants are defined in the |
104 | | -`TogetherConstants` class. |
| 100 | +Contributions are welcome! Feel free to: |
| 101 | +- Open issues for bugs or feature requests |
| 102 | +- Submit pull requests |
| 103 | +- Improve documentation |
105 | 104 |
|
106 | | -## Contributing |
| 105 | +## 📄 License |
107 | 106 |
|
108 | | -Contributions are welcome! Please open an issue or submit a pull request on GitHub. |
| 107 | +This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details. |
109 | 108 |
|
110 | | -## License |
| 109 | +## ⭐ Support |
111 | 110 |
|
112 | | -This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details. |
| 111 | +If you find this project useful, please give it a star on GitHub! |
0 commit comments