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.

TalkToYourself.md 2.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. # Talk to yourself
  2. ```cs
  3. using System.Text;
  4. using LLama.Abstractions;
  5. using LLama.Common;
  6. namespace LLama.Examples.Examples
  7. {
  8. // Let two bots chat with each other.
  9. public class TalkToYourself
  10. {
  11. public static async Task Run()
  12. {
  13. string modelPath = UserSettings.GetModelPath();
  14. // Load weights into memory
  15. var @params = new ModelParams(modelPath);
  16. using var weights = LLamaWeights.LoadFromFile(@params);
  17. // Create 2 contexts sharing the same weights
  18. using var aliceCtx = weights.CreateContext(@params);
  19. var alice = new InteractiveExecutor(aliceCtx);
  20. using var bobCtx = weights.CreateContext(@params);
  21. var bob = new InteractiveExecutor(bobCtx);
  22. // Initial alice prompt
  23. var alicePrompt = "Transcript of a dialog, where the Alice interacts a person named Bob. Alice is friendly, kind, honest and good at writing.\nAlice: Hello";
  24. var aliceResponse = await Prompt(alice, ConsoleColor.Green, alicePrompt, false, false);
  25. // Initial bob prompt
  26. var bobPrompt = $"Transcript of a dialog, where the Bob interacts a person named Alice. Bob is smart, intellectual and good at writing.\nAlice: Hello{aliceResponse}";
  27. var bobResponse = await Prompt(bob, ConsoleColor.Red, bobPrompt, true, true);
  28. // swap back and forth from Alice to Bob
  29. while (true)
  30. {
  31. aliceResponse = await Prompt(alice, ConsoleColor.Green, bobResponse, false, true);
  32. bobResponse = await Prompt(bob, ConsoleColor.Red, aliceResponse, false, true);
  33. if (Console.KeyAvailable)
  34. break;
  35. }
  36. }
  37. private static async Task<string> Prompt(ILLamaExecutor executor, ConsoleColor color, string prompt, bool showPrompt, bool showResponse)
  38. {
  39. var inferenceParams = new InferenceParams
  40. {
  41. Temperature = 0.9f,
  42. AntiPrompts = new List<string> { "Alice:", "Bob:", "User:" },
  43. MaxTokens = 128,
  44. Mirostat = MirostatType.Mirostat2,
  45. MirostatTau = 10,
  46. };
  47. Console.ForegroundColor = ConsoleColor.White;
  48. if (showPrompt)
  49. Console.Write(prompt);
  50. Console.ForegroundColor = color;
  51. var builder = new StringBuilder();
  52. await foreach (var text in executor.InferAsync(prompt, inferenceParams))
  53. {
  54. builder.Append(text);
  55. if (showResponse)
  56. Console.Write(text);
  57. }
  58. return builder.ToString();
  59. }
  60. }
  61. }
  62. ```