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.

ChatSessionStripRoleName.md 2.1 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. # Use chat session and strip role names
  2. ```cs
  3. using LLama.Common;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. public class ChatSessionStripRoleName
  10. {
  11. public static void Run()
  12. {
  13. Console.Write("Please input your model path: ");
  14. var modelPath = Console.ReadLine();
  15. var parameters = new ModelParams(modelPath)
  16. {
  17. ContextSize = 1024,
  18. Seed = 1337,
  19. GpuLayerCount = 5
  20. };
  21. using var model = LLamaWeights.LoadFromFile(parameters);
  22. using var context = model.CreateContext(parameters);
  23. var executor = new InteractiveExecutor(context);
  24. var chatHistoryJson = File.ReadAllText("Assets/chat-with-bob.json");
  25. ChatHistory chatHistory = ChatHistory.FromJson(chatHistoryJson) ?? new ChatHistory();
  26. ChatSession session = new(executor, chatHistory);
  27. session.WithOutputTransform(new LLamaTransforms.KeywordTextOutputStreamTransform(
  28. new string[] { "User:", "Assistant:" },
  29. redundancyLength: 8));
  30. InferenceParams inferenceParams = new InferenceParams()
  31. {
  32. Temperature = 0.9f,
  33. AntiPrompts = new List<string> { "User:" }
  34. };
  35. Console.ForegroundColor = ConsoleColor.Yellow;
  36. Console.WriteLine("The chat session has started.");
  37. // show the prompt
  38. Console.ForegroundColor = ConsoleColor.Green;
  39. string userInput = Console.ReadLine() ?? "";
  40. while (userInput != "exit")
  41. {
  42. await foreach (
  43. var text
  44. in session.ChatAsync(
  45. new ChatHistory.Message(AuthorRole.User, userInput),
  46. inferenceParams))
  47. {
  48. Console.ForegroundColor = ConsoleColor.White;
  49. Console.Write(text);
  50. }
  51. Console.ForegroundColor = ConsoleColor.Green;
  52. userInput = Console.ReadLine() ?? "";
  53. Console.ForegroundColor = ConsoleColor.White;
  54. }
  55. }
  56. }
  57. ```