Closed aherrick closed 1 month ago
@aherrick
The error message you're seeing suggests that the image content is not being properly processed or displayed. Here are a few potential issues and solutions:
Here's an example of how you might add error handling to your code:
try
{
var testImgPath = Path.Combine(Directory.GetCurrentDirectory(), "imgs", "test.png");
if (!File.Exists(testImgPath))
{
Console.WriteLine("Image file not found.");
return;
}
var collectionItems = new ChatMessageContentItemCollection
{
new TextContent("What is the image?"),
new ImageContent(File.ReadAllBytes(testImgPath), "image/png")
};
history.AddUserMessage(collectionItems);
Console.Write($"Phi3: ");
var result = await chat.GetChatMessageContentsAsync(history);
Console.WriteLine(result.Content);
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
}
Your code looks mostly valid, but there are a few things to check and consider:
await
, make sure your method is marked as async
.Here's a revised version with these considerations:
using System;
using System.IO;
using Microsoft.Extensions.DependencyInjection;
using YourNamespace.Services; // Replace with actual namespace for IChatCompletionService and other services
public class Program
{
public static async Task Main(string[] args)
{
var modelPath = @"C:\models\Phi-3-vision-128k-instruct-onnx-cpu\cpu-int4-rtn-block-32-acc-level-4";
#pragma warning disable SKEXP0070 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
// create kernel
var kernel = Kernel
.CreateBuilder()
.AddOnnxRuntimeGenAIChatCompletion(
modelId: "microsoft/Phi-3-vision-128k-instruct",
//modelId: "Phi-3-vision-128k-instruct-onnx-cp",
modelPath: modelPath
)
.Build();
// create chat
var chat = kernel.GetRequiredService<IChatCompletionService>();
var history = new ChatHistory();
var testImgPath = Path.Combine(Directory.GetCurrentDirectory(), "imgs", "test.png");
// create chat collection items
var collectionItems = new ChatMessageContentItemCollection
{
new TextContent("What is the image?"),
new ImageContent(File.ReadAllBytes(testImgPath), "image/png")
};
history.AddUserMessage(collectionItems);
Console.Write($"Phi3: ");
var result = await chat.GetChatMessageContentsAsync(history);
Console.WriteLine(result.Content);
Console.Read();
}
}
Can you try using ONNX as service ? Maybe you can use Hugging face connector in SK
@aherrick you can see an example of how to use the Hugging Face Connector in Semantic Kernel at https://devblogs.microsoft.com/semantic-kernel/how-to-use-hugging-face-models-with-semantic-kernel/
Using the following local Model pulled from HF here is my code:
I keep getting responses like this: (image below)
Is there something wrong with my code?