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.

ChatHistory.cs 3.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using System.Text.Json;
  4. using System.Text.Json.Serialization;
  5. namespace LLama.Common
  6. {
  7. /// <summary>
  8. /// Role of the message author, e.g. user/assistant/system
  9. /// </summary>
  10. public enum AuthorRole
  11. {
  12. /// <summary>
  13. /// Role is unknown
  14. /// </summary>
  15. Unknown = -1,
  16. /// <summary>
  17. /// Message comes from a "system" prompt, not written by a user or language model
  18. /// </summary>
  19. System = 0,
  20. /// <summary>
  21. /// Message comes from the user
  22. /// </summary>
  23. User = 1,
  24. /// <summary>
  25. /// Messages was generated by the language model
  26. /// </summary>
  27. Assistant = 2,
  28. }
  29. // copy from semantic-kernel
  30. /// <summary>
  31. /// The chat history class
  32. /// </summary>
  33. public class ChatHistory
  34. {
  35. /// <summary>
  36. /// Chat message representation
  37. /// </summary>
  38. public class Message
  39. {
  40. /// <summary>
  41. /// Role of the message author, e.g. user/assistant/system
  42. /// </summary>
  43. [JsonConverter(typeof(JsonStringEnumConverter))]
  44. [JsonPropertyName("author_role")]
  45. public AuthorRole AuthorRole { get; set; }
  46. /// <summary>
  47. /// Message content
  48. /// </summary>
  49. [JsonPropertyName("content")]
  50. public string Content { get; set; }
  51. /// <summary>
  52. /// Create a new instance
  53. /// </summary>
  54. /// <param name="authorRole">Role of message author</param>
  55. /// <param name="content">Message content</param>
  56. public Message(AuthorRole authorRole, string content)
  57. {
  58. this.AuthorRole = authorRole;
  59. this.Content = content;
  60. }
  61. }
  62. /// <summary>
  63. /// List of messages in the chat
  64. /// </summary>
  65. [JsonPropertyName("messages")]
  66. public List<Message> Messages { get; set; } = new();
  67. /// <summary>
  68. /// Create a new instance of the chat content class
  69. /// </summary>
  70. [JsonConstructor]
  71. public ChatHistory() { }
  72. /// <summary>
  73. /// Add a message to the chat history
  74. /// </summary>
  75. /// <param name="authorRole">Role of the message author</param>
  76. /// <param name="content">Message content</param>
  77. public void AddMessage(AuthorRole authorRole, string content)
  78. {
  79. this.Messages.Add(new Message(authorRole, content));
  80. }
  81. /// <summary>
  82. /// Serialize the chat history to JSON
  83. /// </summary>
  84. /// <returns></returns>
  85. public string ToJson()
  86. {
  87. return JsonSerializer.Serialize(
  88. this,
  89. new JsonSerializerOptions()
  90. {
  91. WriteIndented = true
  92. });
  93. }
  94. /// <summary>
  95. /// Deserialize a chat history from JSON
  96. /// </summary>
  97. /// <param name="json"></param>
  98. /// <returns></returns>
  99. public static ChatHistory? FromJson(string json)
  100. {
  101. return JsonSerializer.Deserialize<ChatHistory>(json);
  102. }
  103. }
  104. }