dotnet/ai-samples

ImageContent sample

markheath opened this issue · 1 comments

Any chance of a sample showing how to use ImageContent? I have been trying to use Microsoft.Extensions.AI to add an image to a ChatMessage but with no success so far, and there doesn't seem to be any sample showing how to correctly do this.

FWIW, the code I'd attempted to use does actually work. It was just that my model in Azure OpenAI was rate limiting me, but the IChatClient was simply hanging rather than reporting an error (perhaps a bug - doesn't seem a very helpful behaviour). Here's what I got working after upping the rate limit on my deployment a bit:

var message = new ChatMessage(ChatRole.User, "Can you provide a brief, one-sentence description of this image.");
var data = File.ReadAllBytes(@"C:\temp\example.jpeg");
message.Contents.Add(new ImageContent(data, "image/jpeg"));
chatHistory.Add(message);

await foreach (var item in
    chatClient.CompleteStreamingAsync(chatHistory))
{
    Console.Write(item.Text);
}

Or to use an image from the internet, this works:

message.Contents.Add(new ImageContent(new Uri("https://website.net/example.jpg")));