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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using System.Collections.Generic;
  2. using System.Text.Json;
  3. using System.Text.Json.Serialization;
  4. namespace LLama.Common
  5. {
  6. /// <summary>
  7. /// Role of the message author, e.g. user/assistant/system
  8. /// </summary>
  9. public enum AuthorRole
  10. {
  11. /// <summary>
  12. /// Role is unknown
  13. /// </summary>
  14. Unknown = -1,
  15. /// <summary>
  16. /// Message comes from a "system" prompt, not written by a user or language model
  17. /// </summary>
  18. System = 0,
  19. /// <summary>
  20. /// Message comes from the user
  21. /// </summary>
  22. User = 1,
  23. /// <summary>
  24. /// Messages was generated by the language model
  25. /// </summary>
  26. Assistant = 2,
  27. }
  28. // copy from semantic-kernel
  29. /// <summary>
  30. /// The chat history class
  31. /// </summary>
  32. public class ChatHistory
  33. {
  34. private static readonly JsonSerializerOptions _jsonOptions = new() { WriteIndented = true };
  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(this, _jsonOptions);
  88. }
  89. /// <summary>
  90. /// Deserialize a chat history from JSON
  91. /// </summary>
  92. /// <param name="json"></param>
  93. /// <returns></returns>
  94. public static ChatHistory? FromJson(string json)
  95. {
  96. return JsonSerializer.Deserialize<ChatHistory>(json);
  97. }
  98. }
  99. }