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.5 kB

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

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