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.md 3.0 kB

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