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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using System.Collections.Generic;
  2. using System.Linq;
  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. private static readonly JsonSerializerOptions _jsonOptions = new() { WriteIndented = true };
  36. /// <summary>
  37. /// Chat message representation
  38. /// </summary>
  39. public class Message
  40. {
  41. /// <summary>
  42. /// Role of the message author, e.g. user/assistant/system
  43. /// </summary>
  44. [JsonConverter(typeof(JsonStringEnumConverter))]
  45. [JsonPropertyName("author_role")]
  46. public AuthorRole AuthorRole { get; set; }
  47. /// <summary>
  48. /// Message content
  49. /// </summary>
  50. [JsonPropertyName("content")]
  51. public string Content { get; set; }
  52. /// <summary>
  53. /// Create a new instance
  54. /// </summary>
  55. /// <param name="authorRole">Role of message author</param>
  56. /// <param name="content">Message content</param>
  57. public Message(AuthorRole authorRole, string content)
  58. {
  59. this.AuthorRole = authorRole;
  60. this.Content = content;
  61. }
  62. }
  63. /// <summary>
  64. /// List of messages in the chat
  65. /// </summary>
  66. [JsonPropertyName("messages")]
  67. public List<Message> Messages { get; set; } = new();
  68. /// <summary>
  69. /// Create a new instance of the chat content class
  70. /// </summary>
  71. [JsonConstructor]
  72. public ChatHistory() { }
  73. /// <summary>
  74. /// Create a new instance of the chat history from array of messages
  75. /// </summary>
  76. /// <param name="messageHistory"></param>
  77. public ChatHistory(Message[] messageHistory)
  78. {
  79. this.Messages = messageHistory.ToList();
  80. }
  81. /// <summary>
  82. /// Add a message to the chat history
  83. /// </summary>
  84. /// <param name="authorRole">Role of the message author</param>
  85. /// <param name="content">Message content</param>
  86. public void AddMessage(AuthorRole authorRole, string content)
  87. {
  88. this.Messages.Add(new Message(authorRole, content));
  89. }
  90. /// <summary>
  91. /// Serialize the chat history to JSON
  92. /// </summary>
  93. /// <returns></returns>
  94. public string ToJson()
  95. {
  96. return JsonSerializer.Serialize(this, _jsonOptions);
  97. }
  98. /// <summary>
  99. /// Deserialize a chat history from JSON
  100. /// </summary>
  101. /// <param name="json"></param>
  102. /// <returns></returns>
  103. public static ChatHistory? FromJson(string json)
  104. {
  105. return JsonSerializer.Deserialize<ChatHistory>(json);
  106. }
  107. }
  108. }