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.

CodingAssistant.cs 2.9 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. namespace LLama.Examples.Examples
  2. {
  3. using LLama.Common;
  4. using System;
  5. internal class CodingAssistant
  6. {
  7. // Source paper with example prompts:
  8. // https://doi.org/10.48550/arXiv.2308.12950
  9. const string InstructionPrefix = "[INST]";
  10. const string InstructionSuffix = "[/INST]";
  11. const string SystemInstruction = "You're an intelligent, concise coding assistant. " +
  12. "Wrap code in ``` for readability. Don't repeat yourself. " +
  13. "Use best practice and good coding standards.";
  14. public static async Task Run()
  15. {
  16. string modelPath = UserSettings.GetModelPath();
  17. if (!modelPath.Contains("codellama", StringComparison.InvariantCultureIgnoreCase))
  18. {
  19. Console.ForegroundColor = ConsoleColor.Yellow;
  20. Console.WriteLine("WARNING: the model you selected is not a Code LLama model!");
  21. Console.WriteLine("For this example we specifically recommend 'codellama-7b-instruct.Q4_K_S.gguf'");
  22. Console.WriteLine("Press ENTER to continue...");
  23. Console.ReadLine();
  24. }
  25. var parameters = new ModelParams(modelPath)
  26. {
  27. ContextSize = 4096
  28. };
  29. using var model = await LLamaWeights.LoadFromFileAsync(parameters);
  30. using var context = model.CreateContext(parameters);
  31. var executor = new InstructExecutor(context, InstructionPrefix, InstructionSuffix, null);
  32. Console.ForegroundColor = ConsoleColor.Yellow;
  33. Console.WriteLine("The executor has been enabled. In this example, the LLM will follow your instructions." +
  34. "\nIt's a 7B Code Llama, so it's trained for programming tasks like \"Write a C# function reading " +
  35. "a file name from a given URI\" or \"Write some programming interview questions\"." +
  36. "\nWrite 'exit' to exit");
  37. Console.ForegroundColor = ConsoleColor.White;
  38. var inferenceParams = new InferenceParams()
  39. {
  40. Temperature = 0.8f,
  41. MaxTokens = -1,
  42. };
  43. string instruction = $"{SystemInstruction}\n\n";
  44. await Console.Out.WriteAsync("Instruction: ");
  45. instruction += Console.ReadLine() ?? "Ask me for instructions.";
  46. while (instruction != "exit")
  47. {
  48. Console.ForegroundColor = ConsoleColor.Green;
  49. await foreach (var text in executor.InferAsync(instruction + Environment.NewLine, inferenceParams))
  50. {
  51. Console.Write(text);
  52. }
  53. Console.ForegroundColor = ConsoleColor.White;
  54. await Console.Out.WriteAsync("Instruction: ");
  55. instruction = Console.ReadLine() ?? "Ask me for instructions.";
  56. }
  57. }
  58. }
  59. }