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.

Program.cs 3.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using LLama;
  2. using LLama.Examples;
  3. Console.WriteLine("================LLamaSharp Examples==================\n");
  4. Console.WriteLine("Please input a number to choose an example to run:");
  5. Console.WriteLine("0: Run a chat session.");
  6. Console.WriteLine("1: Run a LLamaModel to chat.");
  7. Console.WriteLine("2: Quantize a model.");
  8. Console.WriteLine("3: Get the embeddings of a message.");
  9. Console.WriteLine("4: Run a LLamaModel with instruct mode.");
  10. Console.WriteLine("5: Load and save state of LLamaModel.");
  11. while (true)
  12. {
  13. Console.Write("\nYour choice: ");
  14. int choice = int.Parse(Console.ReadLine());
  15. if (choice == 0)
  16. {
  17. Console.Write("Please input your model path: ");
  18. var modelPath = Console.ReadLine();
  19. ChatSession chat = new(modelPath, "Assets/chat-with-bob.txt", new string[] { "User:" });
  20. chat.Run();
  21. }
  22. else if (choice == 1)
  23. {
  24. Console.Write("Please input your model path: ");
  25. var modelPath = Console.ReadLine();
  26. ChatWithLLamaModel chat = new(modelPath, "Assets/chat-with-bob.txt", new string[] { "User:" });
  27. chat.Run();
  28. }
  29. else if (choice == 2) // quantization
  30. {
  31. Console.Write("Please input your original model path: ");
  32. var inputPath = Console.ReadLine();
  33. Console.Write("Please input your output model path: ");
  34. var outputPath = Console.ReadLine();
  35. Console.Write("Please input the quantize type (one of q4_0, q4_1, q5_0, q5_1, q8_0): ");
  36. var quantizeType = Console.ReadLine();
  37. Quantize q = new Quantize();
  38. q.Run(inputPath, outputPath, quantizeType);
  39. }
  40. else if (choice == 3) // get the embeddings only
  41. {
  42. Console.Write("Please input your model path: ");
  43. var modelPath = Console.ReadLine();
  44. GetEmbeddings em = new GetEmbeddings(modelPath);
  45. Console.Write("Please input the text: ");
  46. var text = Console.ReadLine();
  47. em.Run(text);
  48. }
  49. else if (choice == 4) // instruct mode
  50. {
  51. Console.Write("Please input your model path: ");
  52. var modelPath = Console.ReadLine();
  53. InstructMode im = new InstructMode(modelPath, "Assets/alpaca.txt");
  54. Console.WriteLine("Here's a simple example for using instruct mode. You can input some words and let AI " +
  55. "complete it for you. For example: Write a story about a fox that wants to make friend with human. No less than 200 words.");
  56. im.Run();
  57. }
  58. else if (choice == 5) // load and save state
  59. {
  60. Console.Write("Please input your model path: ");
  61. var modelPath = Console.ReadLine();
  62. Console.Write("Please input your state file path: ");
  63. var statePath = Console.ReadLine();
  64. SaveAndLoadState sals = new(modelPath, File.ReadAllText(@"D:\development\llama\llama.cpp\prompts\alpaca.txt"));
  65. sals.Run("Write a story about a fox that wants to make friend with human. No less than 200 words.");
  66. sals.SaveState(statePath);
  67. sals.Dispose();
  68. GC.Collect();
  69. GC.WaitForPendingFinalizers();
  70. // create a new model to load the state.
  71. SaveAndLoadState sals2 = new(modelPath, "");
  72. sals2.LoadState(statePath);
  73. sals2.Run("Tell me more things about the fox in the story you told me.");
  74. }
  75. else
  76. {
  77. Console.WriteLine("Cannot parse your choice. Please select again.");
  78. continue;
  79. }
  80. break;
  81. }

C#/.NET上易用的LLM高性能推理框架,支持LLaMA和LLaVA系列模型。