Skip to content

Commit 76e904c

Browse files
authored
.Net: Address image base64 content example (#9540)
### Motivation and Context Currently we do support binary image generation, but was lacking examples how to do it in the `Concepts`. This PR adds concept tests and resolves the issue. - Resolves #6991
1 parent 8f81caf commit 76e904c

3 files changed

Lines changed: 93 additions & 11 deletions

File tree

‎dotnet/samples/Concepts/README.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,4 +203,6 @@ dotnet test -l "console;verbosity=detailed" --filter "FullyQualifiedName=ChatCom
203203

204204
### TextToImage - Using [`TextToImage`](https://github.com/microsoft/semantic-kernel/blob/main/dotnet/src/SemanticKernel.Abstractions/AI/TextToImage/ITextToImageService.cs) services to generate images
205205

206-
- [OpenAI_TextToImage](https://github.com/microsoft/semantic-kernel/blob/main/dotnet/samples/Concepts/TextToImage/OpenAI_TextToImageDalle3.cs)
206+
- [OpenAI_TextToImage](https://github.com/microsoft/semantic-kernel/blob/main/dotnet/samples/Concepts/TextToImage/OpenAI_TextToImage.cs)
207+
- [OpenAI_TextToImageLegacy](https://github.com/microsoft/semantic-kernel/blob/main/dotnet/samples/Concepts/TextToImage/OpenAI_TextToImageLegacy.cs)
208+
- [AzureOpenAI_TextToImage](https://github.com/microsoft/semantic-kernel/blob/main/dotnet/samples/Concepts/TextToImage/AzureOpenAI_TextToImage.cs)
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
3+
using Microsoft.SemanticKernel;
4+
using Microsoft.SemanticKernel.Connectors.OpenAI;
5+
using Microsoft.SemanticKernel.TextToImage;
6+
7+
namespace TextToImage;
8+
9+
// The following example shows how to use Semantic Kernel with OpenAI DALL-E 2 to create images
10+
public class AzureOpenAI_TextToImage(ITestOutputHelper output) : BaseTest(output)
11+
{
12+
[Fact]
13+
public async Task SimpleDallE3ImageUriAsync()
14+
{
15+
var builder = Kernel.CreateBuilder()
16+
.AddAzureOpenAITextToImage( // Add your text to image service
17+
deploymentName: TestConfiguration.AzureOpenAI.ImageDeploymentName,
18+
endpoint: TestConfiguration.AzureOpenAI.ImageEndpoint,
19+
apiKey: TestConfiguration.AzureOpenAI.ImageApiKey,
20+
modelId: TestConfiguration.AzureOpenAI.ImageModelId);
21+
22+
var kernel = builder.Build();
23+
var service = kernel.GetRequiredService<ITextToImageService>();
24+
25+
var generatedImages = await service.GetImageContentsAsync(
26+
new TextContent("A cute baby sea otter"),
27+
new OpenAITextToImageExecutionSettings { Size = (Width: 1792, Height: 1024) });
28+
29+
this.Output.WriteLine(generatedImages[0].Uri!.ToString());
30+
}
31+
32+
[Fact]
33+
public async Task SimpleDallE3ImageBinaryAsync()
34+
{
35+
var builder = Kernel.CreateBuilder()
36+
.AddAzureOpenAITextToImage( // Add your text to image service
37+
deploymentName: TestConfiguration.AzureOpenAI.ImageDeploymentName,
38+
endpoint: TestConfiguration.AzureOpenAI.ImageEndpoint,
39+
apiKey: TestConfiguration.AzureOpenAI.ImageApiKey,
40+
modelId: TestConfiguration.AzureOpenAI.ImageModelId);
41+
42+
var kernel = builder.Build();
43+
var service = kernel.GetRequiredService<ITextToImageService>();
44+
45+
var generatedImages = await service.GetImageContentsAsync(new TextContent("A cute baby sea otter"),
46+
new OpenAITextToImageExecutionSettings
47+
{
48+
Size = (Width: 1024, Height: 1024),
49+
50+
// Response Format also accepts the OpenAI.Images.GeneratedImageFormat type.
51+
ResponseFormat = "bytes",
52+
});
53+
54+
this.Output.WriteLine($"Generated Image Bytes: {generatedImages[0].Data!.Value.Length}");
55+
this.Output.WriteLine($"Generated Image DataUri: {generatedImages[0].DataUri}");
56+
}
57+
}

‎dotnet/samples/Concepts/TextToImage/OpenAI_TextToImage.cs‎

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace TextToImage;
1313
public class OpenAI_TextToImage(ITestOutputHelper output) : BaseTest(output)
1414
{
1515
[Fact]
16-
public async Task OpenAIDallE2Async()
16+
public async Task ChatDallE2Async()
1717
{
1818
Console.WriteLine("======== OpenAI DALL-E 2 Text To Image ========");
1919

@@ -82,32 +82,55 @@ A cute baby sea otter
8282
}
8383

8484
[Fact]
85-
public async Task SimpleTextToImageExampleAsync()
85+
public async Task SimpleDallE3ImageUriAsync()
8686
{
8787
var builder = Kernel.CreateBuilder()
88-
.AddAzureOpenAITextToImage( // Add your text to image service
89-
deploymentName: TestConfiguration.AzureOpenAI.ImageDeploymentName,
90-
endpoint: TestConfiguration.AzureOpenAI.ImageEndpoint,
91-
apiKey: TestConfiguration.AzureOpenAI.ImageApiKey,
92-
modelId: TestConfiguration.AzureOpenAI.ImageModelId);
88+
.AddOpenAITextToImage( // Add your text to image service
89+
modelId: "dall-e-3",
90+
apiKey: TestConfiguration.OpenAI.ApiKey);
9391

9492
var kernel = builder.Build();
9593
var service = kernel.GetRequiredService<ITextToImageService>();
9694

97-
var generatedImages = await service.GetImageContentsAsync(new TextContent("A cute baby sea otter"), new OpenAITextToImageExecutionSettings { Size = (Width: 1792, Height: 1024) });
95+
var generatedImages = await service.GetImageContentsAsync(
96+
new TextContent("A cute baby sea otter"),
97+
new OpenAITextToImageExecutionSettings { Size = (Width: 1792, Height: 1024) });
9898

9999
this.Output.WriteLine(generatedImages[0].Uri!.ToString());
100100
}
101101

102102
[Fact]
103-
public async Task OpenAIDallE3Async()
103+
public async Task SimpleDallE3ImageBinaryAsync()
104+
{
105+
var builder = Kernel.CreateBuilder()
106+
.AddOpenAITextToImage( // Add your text to image service
107+
modelId: "dall-e-3",
108+
apiKey: TestConfiguration.OpenAI.ApiKey);
109+
110+
var kernel = builder.Build();
111+
var service = kernel.GetRequiredService<ITextToImageService>();
112+
113+
var generatedImages = await service.GetImageContentsAsync(new TextContent("A cute baby sea otter"),
114+
new OpenAITextToImageExecutionSettings
115+
{
116+
Size = (Width: 1024, Height: 1024),
117+
// Response Format also accepts the OpenAI.Images.GeneratedImageFormat type.
118+
ResponseFormat = "bytes",
119+
});
120+
121+
this.Output.WriteLine($"Generated Image Bytes: {generatedImages[0].Data!.Value.Length}");
122+
this.Output.WriteLine($"Generated Image DataUri: {generatedImages[0].DataUri}");
123+
}
124+
125+
[Fact]
126+
public async Task ChatDallE3Async()
104127
{
105128
Console.WriteLine("======== OpenAI DALL-E 3 Text To Image ========");
106129

107130
var builder = Kernel.CreateBuilder()
108131
.AddOpenAITextToImage( // Add your text to image service
109132
modelId: "dall-e-3",
110-
apiKey: TestConfiguration.OpenAI.ApiKey) //DALL-E 3 is only supported in this version
133+
apiKey: TestConfiguration.OpenAI.ApiKey)
111134
.AddOpenAIChatCompletion( // Add your chat completion service
112135
modelId: TestConfiguration.OpenAI.ChatModelId,
113136
apiKey: TestConfiguration.OpenAI.ApiKey);

0 commit comments

Comments
 (0)