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.

StatelessModeExecute.cs 2.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. using LLama.Common;
  2. using System.Text;
  3. namespace LLama.Examples.NewVersion
  4. {
  5. public class StatelessModeExecute
  6. {
  7. public static void Run()
  8. {
  9. Console.Write("Please input your model path: ");
  10. var modelPath = Console.ReadLine();
  11. var parameters = new ModelParams(modelPath, contextSize: 1024, seed: 1337, gpuLayerCount: 5);
  12. using var model = LLamaWeights.LoadFromFile(parameters);
  13. using var context = model.CreateContext(parameters, Encoding.UTF8);
  14. var ex = new StatelessExecutor(context);
  15. Console.ForegroundColor = ConsoleColor.Yellow;
  16. Console.WriteLine("The executor has been enabled. In this example, the inference is an one-time job. That says, the previous input and response has " +
  17. "no impact on the current response. Now you can ask it questions. Note that in this example, no prompt was set for LLM and the maximum response tokens is 50. " +
  18. "It may not perform well because of lack of prompt. This is also an example that could indicate the improtance of prompt in LLM. To improve it, you can add " +
  19. "a prompt for it yourself!");
  20. Console.ForegroundColor = ConsoleColor.White;
  21. var inferenceParams = new InferenceParams() { Temperature = 0.6f, AntiPrompts = new List<string> { "Question:", "#", "Question: ", ".\n" }, MaxTokens = 50 };
  22. while (true)
  23. {
  24. Console.Write("\nQuestion: ");
  25. Console.ForegroundColor = ConsoleColor.Green;
  26. string prompt = Console.ReadLine();
  27. Console.ForegroundColor = ConsoleColor.White;
  28. Console.Write("Answer: ");
  29. prompt = $"Question: {prompt.Trim()} Answer: ";
  30. foreach (var text in ex.Infer(prompt, inferenceParams))
  31. {
  32. Console.Write(text);
  33. }
  34. }
  35. }
  36. }
  37. }