Skip to content

Commit

Permalink
Add support in C#
Browse files Browse the repository at this point in the history
  • Loading branch information
baijumeswani committed Aug 15, 2024
1 parent 4109586 commit 7a7ed0e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
24 changes: 16 additions & 8 deletions examples/csharp/HelloPhi3V/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License.

using Microsoft.ML.OnnxRuntimeGenAI;
using System.Linq;

class Program
{
Expand All @@ -13,29 +14,36 @@ static void Run(string modelPath)

while (true)
{
Console.WriteLine("Image Path (leave empty if no image):");
string imagePath = Console.ReadLine();
Console.WriteLine("Image Path (comma separated; leave empty if no image):");
string[] imagePaths = Console.ReadLine().Split(',').ToList<string>().Select(i => i.ToString()).ToArray();

Images images = null;
if (imagePath == String.Empty)
if (imagePaths.Length == 0)
{
Console.WriteLine("No image provided");
}
else
{
if (!File.Exists(imagePath))
for (int i = 0; i < imagePaths.Length; i++)
{
throw new Exception("Image file not found: " + imagePath);
string imagePath = imagePaths[i].Trim();
if (!File.Exists(imagePath))
{
throw new Exception("Image file not found: " + imagePath);
}
}
images = Images.Load(imagePath);
images = Images.Load(imagePaths);
}

Console.WriteLine("Prompt:");
string text = Console.ReadLine();
string prompt = "<|user|>\n";
if (images != null)
{
prompt += "<|image_1|>\n";
for (int i = 0; i < imagePaths.Length; i++)
{
prompt += "<|image_" + (i + 1) + "|>\n";
}
}
prompt += text + "<|end|>\n<|assistant|>\n";

Expand All @@ -44,7 +52,7 @@ static void Run(string modelPath)

Console.WriteLine("Generating response...");
using GeneratorParams generatorParams = new GeneratorParams(model);
generatorParams.SetSearchOption("max_length", 3072);
generatorParams.SetSearchOption("max_length", 7680);
generatorParams.SetInputs(inputTensors);

using var generator = new Generator(model, generatorParams);
Expand Down
6 changes: 3 additions & 3 deletions src/csharp/Images.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ private Images(IntPtr imagesHandle)

public static Images Load(string[] imagePaths)
{
Result.VerifySuccess(NativeMethods.CreateStringArray(out IntPtr stringArray));
Result.VerifySuccess(NativeMethods.OgaCreateStringArray(out IntPtr stringArray));
foreach (string imagePath in imagePaths)
{
Result.VerifySuccess(NativeMethods.OgaStringArrayAddString(stringArray, StringUtils.ToUtf8(imagePath)));
}
Result.VerifySuccess(NativeMethods.OgaLoadImages(imagePaths.Length, StringUtils.ToUtf8(imagePaths), out IntPtr imagesHandle));
OgaDestroyStringArray(stringArray);
Result.VerifySuccess(NativeMethods.OgaLoadImages(stringArray, out IntPtr imagesHandle));
NativeMethods.OgaDestroyStringArray(stringArray);
return new Images(imagesHandle);
}

Expand Down
2 changes: 1 addition & 1 deletion src/csharp/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ public static extern UIntPtr OgaSequencesGetSequenceCount(IntPtr /* const OgaSeq
out IntPtr /* const char** */ outStr);

[DllImport(NativeLib.DllName, CallingConvention = CallingConvention.Winapi)]
public static extern IntPtr /* OgaResult* */ OgaLoadImages(IntPtr /* const OgaStringArray* */ imagePath,
public static extern IntPtr /* OgaResult* */ OgaLoadImages(IntPtr /* const OgaStringArray* */ imagePaths,
out IntPtr /* const OgaImages** */ images);

[DllImport(NativeLib.DllName, CallingConvention = CallingConvention.Winapi)]
Expand Down

0 comments on commit 7a7ed0e

Please sign in to comment.