You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

LlavaInteractiveModeExecute.cs 5.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using System.Text.RegularExpressions;
  2. using LLama.Common;
  3. using Spectre.Console;
  4. using LLama.Native;
  5. namespace LLama.Examples.Examples
  6. {
  7. // This example shows how to chat with LLaVA model with both image and text as input.
  8. // It uses the interactive executor to inference.
  9. public class LlavaInteractiveModeExecute
  10. {
  11. public static async Task Run()
  12. {
  13. string multiModalProj = UserSettings.GetMMProjPath();
  14. string modelPath = UserSettings.GetModelPath();
  15. string modelImage = UserSettings.GetImagePath();
  16. const int maxTokens = 1024;
  17. var prompt = $"{{{modelImage}}}\nUSER:\nProvide a full description of the image.\nASSISTANT:\n";
  18. var parameters = new ModelParams(modelPath);
  19. using var model = LLamaWeights.LoadFromFile(parameters);
  20. using var context = model.CreateContext(parameters);
  21. // Llava Init
  22. using var clipModel = LLavaWeights.LoadFromFile(multiModalProj);
  23. var ex = new InteractiveExecutor(context, clipModel );
  24. Console.ForegroundColor = ConsoleColor.Yellow;
  25. Console.WriteLine("The executor has been enabled. In this example, the prompt is printed, the maximum tokens is set to {0} and the context size is {1}.", maxTokens, parameters.ContextSize );
  26. Console.WriteLine("To send an image, enter its filename in curly braces, like this {c:/image.jpg}.");
  27. var inferenceParams = new InferenceParams() { Temperature = 0.1f, AntiPrompts = new List<string> { "\nUSER:" }, MaxTokens = maxTokens };
  28. do
  29. {
  30. // Evaluate if we have images
  31. //
  32. var imageMatches = Regex.Matches(prompt, "{([^}]*)}").Select(m => m.Value);
  33. var imageCount = imageMatches.Count();
  34. var hasImages = imageCount > 0;
  35. if (hasImages)
  36. {
  37. var imagePathsWithCurlyBraces = Regex.Matches(prompt, "{([^}]*)}").Select(m => m.Value);
  38. var imagePaths = Regex.Matches(prompt, "{([^}]*)}").Select(m => m.Groups[1].Value).ToList();
  39. List<byte[]> imageBytes;
  40. try
  41. {
  42. imageBytes = imagePaths.Select(File.ReadAllBytes).ToList();
  43. }
  44. catch (IOException exception)
  45. {
  46. Console.ForegroundColor = ConsoleColor.Red;
  47. Console.Write(
  48. $"Could not load your {(imageCount == 1 ? "image" : "images")}:");
  49. Console.Write($"{exception.Message}");
  50. Console.ForegroundColor = ConsoleColor.Yellow;
  51. Console.WriteLine("Please try again.");
  52. break;
  53. }
  54. // Each prompt with images we clear cache
  55. // When the prompt contains images we clear KV_CACHE to restart conversation
  56. // See:
  57. // https://github.com/ggerganov/llama.cpp/discussions/3620
  58. ex.Context.NativeHandle.KvCacheRemove( LLamaSeqId.Zero, -1, -1 );
  59. int index = 0;
  60. foreach (var path in imagePathsWithCurlyBraces)
  61. {
  62. // First image replace to tag <image, the rest of the images delete the tag
  63. prompt = prompt.Replace(path, index++ == 0 ? "<image>" : "");
  64. }
  65. Console.ForegroundColor = ConsoleColor.Yellow;
  66. Console.WriteLine($"Here are the images, that are sent to the chat model in addition to your message.");
  67. Console.WriteLine();
  68. foreach (var consoleImage in imageBytes?.Select(bytes => new CanvasImage(bytes)))
  69. {
  70. consoleImage.MaxWidth = 50;
  71. AnsiConsole.Write(consoleImage);
  72. }
  73. Console.WriteLine();
  74. Console.ForegroundColor = ConsoleColor.Yellow;
  75. Console.WriteLine($"The images were scaled down for the console only, the model gets full versions.");
  76. Console.WriteLine($"Write /exit or press Ctrl+c to return to main menu.");
  77. Console.WriteLine();
  78. // Initilize Images in executor
  79. //
  80. foreach (var image in imagePaths)
  81. {
  82. ex.Images.Add(await File.ReadAllBytesAsync(image));
  83. }
  84. }
  85. Console.ForegroundColor = Color.White;
  86. await foreach (var text in ex.InferAsync(prompt, inferenceParams))
  87. {
  88. Console.Write(text);
  89. }
  90. Console.Write(" ");
  91. Console.ForegroundColor = ConsoleColor.Green;
  92. prompt = Console.ReadLine();
  93. Console.WriteLine();
  94. // let the user finish with exit
  95. //
  96. if (prompt != null && prompt.Equals("/exit", StringComparison.OrdinalIgnoreCase))
  97. break;
  98. }
  99. while(true);
  100. }
  101. }
  102. }