Skip to content

Commit ad4f762

Browse files
committed
LoRa images
1 parent fb63193 commit ad4f762

File tree

6 files changed

+208
-39
lines changed

6 files changed

+208
-39
lines changed

Directory.Build.props

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
<RepositoryUrl>https://github.com/managedcode/together-dotnet</RepositoryUrl>
2626
<PackageProjectUrl>https://github.com/managedcode/together-dotnet</PackageProjectUrl>
2727
<Product>Together.AI .NET/C# SDK</Product>
28-
<Version>0.0.2</Version>
29-
<PackageVersion>0.0.2</PackageVersion>
28+
<Version>0.0.3</Version>
29+
<PackageVersion>0.0.3</PackageVersion>
3030

3131
</PropertyGroup>
3232
<PropertyGroup Condition="'$(GITHUB_ACTIONS)' == 'true'">

Together.SemanticKernel/Services/TogetherTextToImageService.cs

Lines changed: 94 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -93,51 +93,111 @@ public async Task<IReadOnlyList<ImageContent>> GetImageContentsAsync(TextContent
9393
}
9494
}
9595

96-
private static void ApplyTogetherSettings(ImageRequest request, TogetherTextToImageExecutionSettings settings)
96+
private static void ApplyTogetherSettings(ImageRequest request, TogetherTextToImageExecutionSettings settings)
97+
{
98+
if (settings.Width.HasValue)
9799
{
98-
if (settings.Width.HasValue)
99-
{
100-
request.Width = settings.Width.Value;
101-
}
100+
request.Width = settings.Width.Value;
101+
}
102102

103-
if (settings.Height.HasValue)
104-
{
105-
request.Height = settings.Height.Value;
106-
}
103+
if (settings.Height.HasValue)
104+
{
105+
request.Height = settings.Height.Value;
106+
}
107107

108-
if (!string.IsNullOrEmpty(settings.NegativePrompt))
109-
{
110-
request.NegativePrompt = settings.NegativePrompt;
111-
}
108+
if (!string.IsNullOrEmpty(settings.NegativePrompt))
109+
{
110+
request.NegativePrompt = settings.NegativePrompt;
111+
}
112112

113-
if (settings.Seed.HasValue)
114-
{
115-
request.Seed = settings.Seed.Value;
116-
}
113+
if (settings.Seed.HasValue)
114+
{
115+
request.Seed = settings.Seed.Value;
117116
}
118117

119-
private static void ApplyExecutionSettings(ImageRequest request, IDictionary<string, object> settings)
118+
if (!string.IsNullOrEmpty(settings.Prompt))
120119
{
121-
if (settings.TryGetValue("width", out var width) && width is int widthValue)
122-
{
123-
request.Width = widthValue;
124-
}
120+
request.Prompt = settings.Prompt;
121+
}
125122

126-
if (settings.TryGetValue("height", out var height) && height is int heightValue)
127-
{
128-
request.Height = heightValue;
129-
}
123+
if (settings.Steps.HasValue)
124+
{
125+
request.Steps = settings.Steps.Value;
126+
}
130127

131-
if (settings.TryGetValue("negative_prompt", out var negativePrompt) && negativePrompt is string negativePromptValue)
132-
{
133-
request.NegativePrompt = negativePromptValue;
134-
}
128+
if (settings.N.HasValue)
129+
{
130+
request.N = settings.N.Value;
131+
}
135132

136-
if (settings.TryGetValue("seed", out var seed) && seed is ulong seedValue)
137-
{
138-
request.Seed = seedValue;
139-
}
133+
if (!string.IsNullOrEmpty(settings.ImageUrl))
134+
{
135+
request.ImageUrl = settings.ImageUrl;
136+
}
137+
138+
if (settings.ImageLoras != null)
139+
{
140+
request.ImageLoras = settings.ImageLoras;
141+
}
142+
143+
if (!string.IsNullOrEmpty(settings.ResponseFormat))
144+
{
145+
request.ResponseFormat = settings.ResponseFormat;
146+
}
147+
}
148+
149+
private static void ApplyExecutionSettings(ImageRequest request, IDictionary<string, object> settings)
150+
{
151+
if (settings.TryGetValue("width", out var width) && width is int widthValue)
152+
{
153+
request.Width = widthValue;
154+
}
155+
156+
if (settings.TryGetValue("height", out var height) && height is int heightValue)
157+
{
158+
request.Height = heightValue;
159+
}
160+
161+
if (settings.TryGetValue("negative_prompt", out var negativePrompt) && negativePrompt is string negativePromptValue)
162+
{
163+
request.NegativePrompt = negativePromptValue;
164+
}
165+
166+
if (settings.TryGetValue("seed", out var seed) && seed is ulong seedValue)
167+
{
168+
request.Seed = seedValue;
169+
}
170+
171+
if (settings.TryGetValue("prompt", out var prompt) && prompt is string promptValue)
172+
{
173+
request.Prompt = promptValue;
174+
}
175+
176+
if (settings.TryGetValue("steps", out var steps) && steps is int stepsValue)
177+
{
178+
request.Steps = stepsValue;
179+
}
180+
181+
if (settings.TryGetValue("n", out var n) && n is int nValue)
182+
{
183+
request.N = nValue;
184+
}
185+
186+
if (settings.TryGetValue("image_url", out var imageUrl) && imageUrl is string imageUrlValue)
187+
{
188+
request.ImageUrl = imageUrlValue;
189+
}
190+
191+
if (settings.TryGetValue("image_loras", out var loras) && loras is List<ImageLora> lorasValue)
192+
{
193+
request.ImageLoras = lorasValue;
194+
}
195+
196+
if (settings.TryGetValue("response_format", out var format) && format is string formatValue)
197+
{
198+
request.ResponseFormat = formatValue;
140199
}
200+
}
141201

142202
private static void ValidateImageRequest(ImageRequest request)
143203
{

Together.SemanticKernel/TogetherTextToImageExecutionSettings.cs

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Text.Json.Serialization;
22
using Microsoft.SemanticKernel;
3+
using Together.Models.Images;
34

45
namespace Together.SemanticKernel;
56

@@ -10,9 +11,12 @@ public sealed class TogetherTextToImageExecutionSettings : PromptExecutionSettin
1011
private int? _n = 1;
1112
private string? _negativePrompt;
1213
private ulong? _seed;
13-
1414
private int? _steps = 20;
1515
private int? _width = 1024;
16+
private string? _prompt;
17+
private string? _imageUrl;
18+
private List<ImageLora>? _imageLoras;
19+
private string _responseFormat = "url";
1620

1721
[JsonPropertyName("steps")]
1822
public int? Steps
@@ -68,6 +72,18 @@ public int? Width
6872
_width = value;
6973
}
7074
}
75+
76+
77+
[JsonPropertyName("prompt")]
78+
public string? Prompt
79+
{
80+
get => _prompt;
81+
set
82+
{
83+
ThrowIfFrozen();
84+
_prompt = value;
85+
}
86+
}
7187

7288
[JsonPropertyName("negative_prompt")]
7389
public string? NegativePrompt
@@ -80,17 +96,55 @@ public string? NegativePrompt
8096
}
8197
}
8298

99+
100+
[JsonPropertyName("image_url")]
101+
public string? ImageUrl
102+
{
103+
get => _imageUrl;
104+
set
105+
{
106+
ThrowIfFrozen();
107+
_imageUrl = value;
108+
}
109+
}
110+
111+
[JsonPropertyName("image_loras")]
112+
public List<ImageLora>? ImageLoras
113+
{
114+
get => _imageLoras;
115+
set
116+
{
117+
ThrowIfFrozen();
118+
_imageLoras = value;
119+
}
120+
}
121+
122+
[JsonPropertyName("response_format")]
123+
public string ResponseFormat
124+
{
125+
get => _responseFormat;
126+
set
127+
{
128+
ThrowIfFrozen();
129+
_responseFormat = value;
130+
}
131+
}
132+
83133
public override PromptExecutionSettings Clone()
84134
{
85135
return new TogetherTextToImageExecutionSettings
86136
{
87137
ModelId = ModelId,
138+
Prompt = Prompt,
88139
Steps = Steps,
89140
Seed = Seed,
90141
N = N,
91142
Height = Height,
92143
Width = Width,
93144
NegativePrompt = NegativePrompt,
145+
ImageUrl = ImageUrl,
146+
ImageLoras = ImageLoras?.ToList(),
147+
ResponseFormat = ResponseFormat,
94148
ExtensionData = ExtensionData != null ? new Dictionary<string, object>(ExtensionData) : null
95149
};
96150
}

Together.Tests/SemanticKernelIntegraionTests.cs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class SemanticKernelIntegraionTests
1717

1818

1919
private string TextModel = "mistralai/Mistral-7B-Instruct-v0.1";//"meta-llama/Llama-3.3-70B-Instruct-Turbo";
20-
private string ImageModel = "black-forest-labs/FLUX.1-dev";
20+
private string ImageModel = "black-forest-labs/FLUX.1-depth";
2121
private string EmbeddedModel = "togethercomputer/m2-bert-80M-2k-retrieval";
2222

2323
[Fact
@@ -193,6 +193,38 @@ public async Task ImageTest()
193193
});
194194

195195

196+
images.Count.ShouldBePositive();
197+
images.First()
198+
.Uri
199+
.ShouldNotBeNull();
200+
}
201+
202+
[Fact
203+
#if !API_TEST
204+
(Skip = "This test is skipped because it requires a valid API key")
205+
#endif
206+
]
207+
[Experimental("SKEXP0001")]
208+
public async Task ImageTestLora()
209+
{
210+
var kernel = Kernel.CreateBuilder()
211+
.AddTogetherTextToImage(ImageModel, API_KEY)
212+
.Build();
213+
214+
215+
var imageService = kernel.GetRequiredService<ITextToImageService>();
216+
217+
var images = await imageService.GetImageContentsAsync(new TextContent
218+
{
219+
Text = "Make a cat base on photo eating popcorn"
220+
}, new TogetherTextToImageExecutionSettings
221+
{
222+
Height = 512,
223+
Width = 512,
224+
ImageUrl = "https://avatars.githubusercontent.com/u/1024025?v=4"
225+
});
226+
227+
196228
images.Count.ShouldBePositive();
197229
images.First()
198230
.Uri
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System.Text.Json.Serialization;
2+
3+
namespace Together.Models.Images;
4+
5+
public class ImageLora
6+
{
7+
[JsonPropertyName("path")]
8+
public string Path { get; set; }
9+
10+
[JsonPropertyName("scale")]
11+
public float Scale { get; set; }
12+
}

Together/Models/Images/ImageRequest.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,16 @@ public class ImageRequest
2626
public int? Width { get; set; } = 1024;
2727

2828
[JsonPropertyName("negative_prompt")]
29-
public string NegativePrompt { get; set; }
29+
public string? NegativePrompt { get; set; }
30+
31+
[JsonPropertyName("image_url")]
32+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
33+
public string? ImageUrl { get; set; }
34+
35+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
36+
[JsonPropertyName("image_loras")]
37+
public List<ImageLora>? ImageLoras { get; set; }
38+
39+
[JsonPropertyName("response_format")]
40+
public string ResponseFormat { get; set; } = "url";
3041
}

0 commit comments

Comments
 (0)