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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. namespace LLama.Examples.Examples
  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://doi.org/10.48550/arXiv.2308.12950
  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, null);
  30. Console.ForegroundColor = ConsoleColor.Yellow;
  31. Console.WriteLine("The executor has been enabled. In this example, the LLM will follow your instructions." +
  32. "\nIt'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. "\nWrite 'exit' to exit");
  34. Console.ForegroundColor = ConsoleColor.White;
  35. var inferenceParams = new InferenceParams() {
  36. Temperature = 0.8f,
  37. MaxTokens = -1,
  38. };
  39. string instruction = $"{SystemInstruction}\n\n";
  40. await Console.Out.WriteAsync("Instruction: ");
  41. instruction += Console.ReadLine() ?? "Ask me for instructions.";
  42. while (instruction != "exit")
  43. {
  44. Console.ForegroundColor = ConsoleColor.Green;
  45. await foreach (var text in executor.InferAsync(instruction + System.Environment.NewLine, inferenceParams))
  46. {
  47. Console.Write(text);
  48. }
  49. Console.ForegroundColor = ConsoleColor.White;
  50. await Console.Out.WriteAsync("Instruction: ");
  51. instruction = Console.ReadLine() ?? "Ask me for instructions.";
  52. }
  53. }
  54. private static async Task<string> GetDefaultModel()
  55. {
  56. var uri = new Uri(DefaultModelUri);
  57. var modelName = uri.Segments[^1];
  58. await Console.Out.WriteLineAsync($"The following model will be used: {modelName}");
  59. var modelPath = Path.Combine(ModelsDirectory, modelName);
  60. if(!Directory.Exists(ModelsDirectory))
  61. {
  62. Directory.CreateDirectory(ModelsDirectory);
  63. }
  64. if (File.Exists(modelPath))
  65. {
  66. await Console.Out.WriteLineAsync($"Existing model found, using {modelPath}");
  67. }
  68. else
  69. {
  70. await Console.Out.WriteLineAsync($"Model not found locally, downloading {DefaultModelUri}...");
  71. using var http = new HttpClient();
  72. await using var downloadStream = await http.GetStreamAsync(uri);
  73. await using var fileStream = new FileStream(modelPath, FileMode.Create, FileAccess.Write);
  74. await downloadStream.CopyToAsync(fileStream);
  75. await Console.Out.WriteLineAsync($"Model downloaded and saved to {modelPath}");
  76. }
  77. return modelPath;
  78. }
  79. }
  80. }