Skip to content

Commit e4ce2bc

Browse files
committed
a lot of work here
1 parent 00c50e9 commit e4ce2bc

19 files changed

+1033
-222
lines changed

Directory.Build.props

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<Project>
22

33
<PropertyGroup>
4+
<TargetFramework>net9.0</TargetFramework>
45
<LangVersion>13</LangVersion>
56
<EnableNETAnalyzers>true</EnableNETAnalyzers>
67
<Nullable>enable</Nullable>
@@ -24,8 +25,8 @@
2425
<RepositoryUrl>https://github.com/managedcode/together-dotnet</RepositoryUrl>
2526
<PackageProjectUrl>https://github.com/managedcode/together-dotnet</PackageProjectUrl>
2627
<Product>Together.AI .NET/C# SDK</Product>
27-
<Version>1.0.0</Version>
28-
<PackageVersion>1.0.0</PackageVersion>
28+
<Version>0.0.1</Version>
29+
<PackageVersion>0.0.1</PackageVersion>
2930

3031
</PropertyGroup>
3132
<PropertyGroup Condition="'$(GITHUB_ACTIONS)' == 'true'">

README.md

Lines changed: 79 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,112 +1,111 @@
11
# Together .NET SDK
22

3-
C# SDK for Together.ai
3+
[![.NET](https://github.com/managedcode/Together/actions/workflows/dotnet.yml/badge.svg)](https://github.com/managedcode/Together/actions/workflows/dotnet.yml)
4+
[![NuGet](https://img.shields.io/nuget/v/ManagedCode.Together.svg)](https://www.nuget.org/packages/ManagedCode.Together)
5+
[![NuGet](https://img.shields.io/nuget/v/ManagedCode.Together.SemanticKernel.svg)](https://www.nuget.org/packages/ManagedCode.Together.SemanticKernel)
6+
[![Downloads](https://img.shields.io/nuget/dt/ManagedCode.Together.svg)](https://www.nuget.org/packages/ManagedCode.Together)
7+
[![License](https://img.shields.io/github/license/managedcode/Together)](https://github.com/managedcode/Together/blob/main/LICENSE)
48

5-
## Introduction
9+
Unofficial C# SDK for [Together.ai](https://www.together.ai/) with Semantic Kernel integration.
610

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
1012

11-
## Features
13+
Together.ai provides access to the latest open-source AI models through a simple API. This SDK offers:
1214

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
1720

1821
## Installation
1922

20-
To install the Together .NET SDK, add the following package to your project:
23+
Choose the package(s) you need:
2124

2225
```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
3128

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
3931
```
4032

41-
### Completions
33+
## Usage Examples
4234

43-
To get a text completion:
35+
### Direct API Usage
4436

4537
```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;
5839

59-
```csharp
60-
var request = new ChatCompletionRequest
40+
var client = new TogetherClient("YOUR_API_KEY");
41+
var response = await client.ChatCompletions.CreateAsync(new ChatCompletionRequest
6142
{
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+
});
6749
```
6850

69-
### Embeddings
70-
71-
To get embeddings:
51+
### Semantic Kernel Integration
7252

7353
```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+
);
8192
```
8293

83-
### Images
94+
## 📚 Documentation
8495

85-
To generate an image:
96+
For more information about available models and features, visit the [Together.ai Documentation](https://docs.together.ai/)
8697

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
10299

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
105104

106-
## Contributing
105+
## 📄 License
107106

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.
109108

110-
## License
109+
## ⭐ Support
111110

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!

Together.SemanticKernel/ChatOptionsConverter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using Microsoft.Extensions.AI;
22
using Together.Models.ChatCompletions;
33

4-
namespace Together.Extensions;
4+
namespace Together.SemanticKernel;
55

66
public static class ChatOptionsConverter
77
{
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using Microsoft.SemanticKernel;
2+
3+
namespace Together.SemanticKernel.Converters;
4+
5+
public static class ChatOptionsConverter
6+
{
7+
public static Dictionary<string, object> Convert(PromptExecutionSettings settings)
8+
{
9+
var result = new Dictionary<string, object>();
10+
11+
if (settings?.ExtensionData == null)
12+
{
13+
return result;
14+
}
15+
16+
foreach (var kvp in settings.ExtensionData)
17+
{
18+
result[kvp.Key] = kvp.Value;
19+
}
20+
21+
return result;
22+
}
23+
}
Lines changed: 72 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
using Microsoft.Extensions.AI;
21
using System.Text.Json;
3-
using Microsoft.SemanticKernel;
4-
using Microsoft.SemanticKernel.ChatCompletion;
2+
using Microsoft.Extensions.AI;
3+
using Together.Models.ChatCompletions;
54

6-
namespace Together.Models.ChatCompletions;
5+
namespace Together.SemanticKernel.Extensions;
76

87
public static class ChatMessageExtensions
98
{
@@ -36,55 +35,81 @@ public static ChatCompletionRequest CreateChatCompletionRequest(
3635
Stream = stream
3736
};
3837
}
39-
}
4038

41-
public static class ResponseFormatExtensions
42-
{
43-
public static ResponseFormat ToResponseFormat(this ChatResponseFormat chatResponseFormat)
39+
private static string GetRole(JsonElement message)
4440
{
45-
return new ResponseFormat
41+
if (message.TryGetProperty("role", out var roleElement))
4642
{
47-
Type = chatResponseFormat is ChatResponseFormatText ? ResponseFormatType.JsonObject : ResponseFormatType.JsonSchema,
48-
Schema = chatResponseFormat is ChatResponseFormatJson jsonFormat
49-
? JsonSerializer.Deserialize<Dictionary<string, object>>(jsonFormat.Schema ?? "{}")
50-
: new Dictionary<string, object>()
51-
};
52-
}
53-
54-
public static ChatResponseFormat ToChatResponseFormat(this ResponseFormat responseFormat)
55-
{
56-
return responseFormat.Type == ResponseFormatType.JsonObject
57-
? ChatResponseFormat.Text
58-
: ChatResponseFormat.ForJsonSchema(JsonSerializer.Serialize(responseFormat.Schema));
59-
}
60-
61-
public static StreamingChatMessageContent ToStreamingChatMessageContent(this ChatCompletionChunk chunk)
62-
{
63-
var content = chunk.Choices.FirstOrDefault()?.Delta?.Content;
64-
return content != null ? new StreamingChatMessageContent(AuthorRole.Assistant, content) : null;
43+
return roleElement.ValueKind == JsonValueKind.String
44+
? roleElement.GetString() ?? string.Empty
45+
: string.Empty;
46+
}
47+
48+
return string.Empty;
6549
}
66-
}
6750

68-
public static class MessagesExtensions
69-
{
70-
public static ChatCompletionMessage ToChatCompletionMessage(this ChatMessageContent messageContent)
51+
private static string GetContent(JsonElement message)
7152
{
72-
ArgumentNullException.ThrowIfNull(messageContent);
73-
74-
return new ChatCompletionMessage
53+
if (message.TryGetProperty("content", out var contentElement))
7554
{
76-
Role = new ChatRole(messageContent.Role.ToString()),
77-
Content = messageContent.Content,
78-
};
79-
}
80-
81-
public static IEnumerable<ChatCompletionMessage> ToChatCompletionMessages(this ChatHistory history)
82-
{
83-
ArgumentNullException.ThrowIfNull(history);
84-
85-
foreach (var item in history)
86-
{
87-
yield return item.ToChatCompletionMessage();
55+
return contentElement.ValueKind == JsonValueKind.String
56+
? contentElement.GetString() ?? string.Empty
57+
: string.Empty;
8858
}
59+
60+
return string.Empty;
8961
}
90-
}
62+
}
63+
64+
// public static class ResponseFormatExtensions
65+
// {
66+
// public static ResponseFormat ToResponseFormat(this ChatResponseFormat chatResponseFormat)
67+
// {
68+
// if (chatResponseFormat == null)
69+
// {
70+
// return new ResponseFormat { Type = ResponseFormatType.JsonObject };
71+
// }
72+
//
73+
// var responseFormat = new ResponseFormat();
74+
//
75+
// if (chatResponseFormat is ChatResponseFormatJson jsonFormat)
76+
// {
77+
// responseFormat.Type = ResponseFormatType.JsonObject;
78+
// if (jsonFormat.Schema is not null)
79+
// {
80+
// try
81+
// {
82+
// responseFormat.Schema = JsonSerializer.Deserialize<Dictionary<string, object>>(jsonFormat.Schema);
83+
// }
84+
// catch
85+
// {
86+
// responseFormat.Schema = new Dictionary<string, object>();
87+
// }
88+
// }
89+
// else
90+
// {
91+
// responseFormat.Schema = new Dictionary<string, object>();
92+
// }
93+
// }
94+
// else // ChatResponseFormatText
95+
// {
96+
// responseFormat.Type = ResponseFormatType.JsonSchema;
97+
// }
98+
//
99+
// return responseFormat;
100+
// }
101+
//
102+
// public static ChatResponseFormat ToChatResponseFormat(this ResponseFormat responseFormat)
103+
// {
104+
// if (responseFormat == null || responseFormat.Type == ResponseFormatType.JsonObject)
105+
// {
106+
// return ChatResponseFormat.Text;
107+
// }
108+
//
109+
// var schema = responseFormat.Schema != null
110+
// ? JsonSerializer.Serialize(responseFormat.Schema)
111+
// : "{}";
112+
//
113+
// return ChatResponseFormat.ForJsonSchema(schema);
114+
// }
115+
// }

0 commit comments

Comments
 (0)