Skip to content

Commit

Permalink
.Net: Fixed ImageContent usage in OpenAI connector (#6450)
Browse files Browse the repository at this point in the history
### Motivation and Context

<!-- Thank you for your contribution to the semantic-kernel repo!
Please help reviewers and future users, providing the following
information:
  1. Why is this change required?
  2. What problem does it solve?
  3. What scenario does it contribute to?
  4. If it fixes an open issue, please link to the issue here.
-->

Fixes: #6443

This is temporary fix before new updates to `ImageContent` class will be
in place: #6319

cc: @RogerBarreto

### Description

<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->

### Contribution Checklist

<!-- Before submitting this PR, please make sure: -->

- [x] The code builds clean without any errors or warnings
- [x] The PR follows the [SK Contribution
Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md)
and the [pre-submission formatting
script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts)
raises no violations
- [x] All unit tests pass, and I have added new tests where possible
- [x] I didn't break anyone 😄
  • Loading branch information
dmytrostruk committed May 31, 2024
1 parent fbb26b5 commit 8d9c3cb
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
using Resources;

namespace ChatCompletion;

// This example shows how to use GPT Vision model with different content types (text and image).
public class OpenAI_ChatCompletionWithVision(ITestOutputHelper output) : BaseTest(output)
{
[Fact]
public async Task RunAsync()
public async Task RemoteImageAsync()
{
const string ImageUri = "https://upload.wikimedia.org/wikipedia/commons/d/d5/Half-timbered_mansion%2C_Zirkel%2C_East_view.jpg";

Expand All @@ -31,4 +32,28 @@ public async Task RunAsync()

Console.WriteLine(reply.Content);
}

[Fact]
public async Task LocalImageAsync()
{
var imageBytes = await EmbeddedResource.ReadAllAsync("sample_image.jpg");

var kernel = Kernel.CreateBuilder()
.AddOpenAIChatCompletion("gpt-4-vision-preview", TestConfiguration.OpenAI.ApiKey)
.Build();

var chatCompletionService = kernel.GetRequiredService<IChatCompletionService>();

var chatHistory = new ChatHistory("You are a friendly assistant.");

chatHistory.AddUserMessage(
[
new TextContent("What’s in this image?"),
new ImageContent(imageBytes) { MimeType = "image/jpg" }
]);

var reply = await chatCompletionService.GetChatMessageContentAsync(chatHistory);

Console.WriteLine(reply.Content);
}
}
17 changes: 16 additions & 1 deletion dotnet/src/Connectors/Connectors.OpenAI/AzureSdk/ClientCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1267,7 +1267,7 @@ private static List<ChatRequestMessage> GetRequestMessages(ChatMessageContent me
return [new ChatRequestUserMessage(message.Items.Select(static (KernelContent item) => (ChatMessageContentItem)(item switch
{
TextContent textContent => new ChatMessageTextContentItem(textContent.Text),
ImageContent imageContent => new ChatMessageImageContentItem(imageContent.Uri),
ImageContent imageContent => GetImageContentItem(imageContent),
_ => throw new NotSupportedException($"Unsupported chat message content type '{item.GetType()}'.")
})))
{ Name = message.AuthorName }];
Expand Down Expand Up @@ -1337,6 +1337,21 @@ private static List<ChatRequestMessage> GetRequestMessages(ChatMessageContent me
throw new NotSupportedException($"Role {message.Role} is not supported.");
}

private static ChatMessageImageContentItem GetImageContentItem(ImageContent imageContent)
{
if (imageContent.Data is { IsEmpty: false } data)
{
return new ChatMessageImageContentItem(BinaryData.FromBytes(data), imageContent.MimeType);
}

if (imageContent.Uri is not null)
{
return new ChatMessageImageContentItem(imageContent.Uri);
}

throw new ArgumentException($"{nameof(ImageContent)} must have either Data or a Uri.");
}

private static ChatRequestMessage GetRequestMessage(ChatResponseMessage message)
{
if (message.Role == ChatRole.System)
Expand Down

0 comments on commit 8d9c3cb

Please sign in to comment.