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 4.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. namespace LLama.Examples.NewVersion
  2. {
  3. using LLama.Common;
  4. using System;
  5. using System.Reflection;
  6. internal class CodingAssistant
  7. {
  8. const string DefaultModelUri = "https://huggingface.co/TheBloke/CodeLlama-7B-Instruct-GGUF/resolve/main/codellama-7b-instruct.Q4_K_S.gguf";
  9. // Source paper with example prompts:
  10. // https://scontent-ham3-1.xx.fbcdn.net/v/t39.2365-6/369856151_1754812304950972_1159666448927483931_n.pdf?_nc_cat=107&ccb=1-7&_nc_sid=3c67a6&_nc_ohc=wURKmnWKaloAX9CL8rD&_nc_ht=scontent-ham3-1.xx&oh=00_AfBSvnWP6BkLgXzZ0OvLGkiDbkejxoM03Xg2ghVhn_InZQ&oe=64EEAC4F
  11. const string InstructionPrefix = "[INST]";
  12. const string InstructionSuffix = "[/INST]";
  13. const string SystemInstruction = "You're an intelligent, concise coding assistant. Wrap code in ``` for readability. Don't repeat yourself. Use best practice and good coding standards.";
  14. private static string ModelsDirectory = Path.Combine(Directory.GetParent(Assembly.GetExecutingAssembly().Location)!.FullName, "Models");
  15. public static async Task Run()
  16. {
  17. Console.Write("Please input your model path (if left empty, a default model will be downloaded for you): ");
  18. var modelPath = Console.ReadLine();
  19. if(string.IsNullOrWhiteSpace(modelPath) )
  20. {
  21. modelPath = await GetDefaultModel();
  22. }
  23. var parameters = new ModelParams(modelPath)
  24. {
  25. ContextSize = 4096
  26. };
  27. using var model = LLamaWeights.LoadFromFile(parameters);
  28. using var context = model.CreateContext(parameters);
  29. var executor = new InstructExecutor(context, InstructionPrefix, InstructionSuffix);
  30. Console.ForegroundColor = ConsoleColor.Yellow;
  31. Console.WriteLine("The executor has been enabled. In this example, the LLM will follow your instructions." +
  32. "It's a 7B Code Llama, so it's trained for programming tasks like \"Write a C# function reading a file name from a given URI\" or \"Write some programming interview questions\".");
  33. Console.ForegroundColor = ConsoleColor.White;
  34. var inferenceParams = new InferenceParams() {
  35. Temperature = 0.8f,
  36. MaxTokens = -1,
  37. };
  38. string instruction = $"{SystemInstruction}\n";
  39. await Console.Out.WriteAsync("Instruction: ");
  40. instruction += Console.ReadLine() ?? "Ask me for instructions.";
  41. while (true)
  42. {
  43. Console.ForegroundColor = ConsoleColor.Green;
  44. foreach (var text in executor.Infer(instruction+System.Environment.NewLine, inferenceParams))
  45. {
  46. Console.Write(text);
  47. }
  48. Console.ForegroundColor = ConsoleColor.White;
  49. await Console.Out.WriteAsync("Instruction: ");
  50. instruction = Console.ReadLine() ?? "Ask me for instructions.";
  51. }
  52. }
  53. private static async Task<string> GetDefaultModel()
  54. {
  55. var uri = new Uri(DefaultModelUri);
  56. var modelName = uri.Segments[^1];
  57. await Console.Out.WriteLineAsync($"The following model will be used: {modelName}");
  58. var modelPath = Path.Combine(ModelsDirectory, modelName);
  59. if(!Directory.Exists(ModelsDirectory))
  60. {
  61. Directory.CreateDirectory(ModelsDirectory);
  62. }
  63. if (File.Exists(modelPath))
  64. {
  65. await Console.Out.WriteLineAsync($"Existing model found, using {modelPath}");
  66. }
  67. else
  68. {
  69. await Console.Out.WriteLineAsync($"Model not found locally, downloading {DefaultModelUri}...");
  70. using var http = new HttpClient();
  71. await using var downloadStream = await http.GetStreamAsync(uri);
  72. await using var fileStream = new FileStream(modelPath, FileMode.Create, FileAccess.Write);
  73. await downloadStream.CopyToAsync(fileStream);
  74. await Console.Out.WriteLineAsync($"Model downloaded and saved to {modelPath}");
  75. }
  76. return modelPath;
  77. }
  78. }
  79. }