using System.Collections.Generic; using System.IO; using System.Text.Json; using System.Text.Json.Serialization; namespace LLama.Common { /// /// Role of the message author, e.g. user/assistant/system /// public enum AuthorRole { /// /// Role is unknown /// Unknown = -1, /// /// Message comes from a "system" prompt, not written by a user or language model /// System = 0, /// /// Message comes from the user /// User = 1, /// /// Messages was generated by the language model /// Assistant = 2, } // copy from semantic-kernel /// /// The chat history class /// public class ChatHistory { /// /// Chat message representation /// public class Message { /// /// Role of the message author, e.g. user/assistant/system /// [JsonConverter(typeof(JsonStringEnumConverter))] [JsonPropertyName("author_role")] public AuthorRole AuthorRole { get; set; } /// /// Message content /// [JsonPropertyName("content")] public string Content { get; set; } /// /// Create a new instance /// /// Role of message author /// Message content public Message(AuthorRole authorRole, string content) { this.AuthorRole = authorRole; this.Content = content; } } /// /// List of messages in the chat /// [JsonPropertyName("messages")] public List Messages { get; set; } = new(); /// /// Create a new instance of the chat content class /// [JsonConstructor] public ChatHistory() { } /// /// Add a message to the chat history /// /// Role of the message author /// Message content public void AddMessage(AuthorRole authorRole, string content) { this.Messages.Add(new Message(authorRole, content)); } /// /// Serialize the chat history to JSON /// /// public string ToJson() { return JsonSerializer.Serialize( this, new JsonSerializerOptions() { WriteIndented = true }); } /// /// Deserialize a chat history from JSON /// /// /// public static ChatHistory? FromJson(string json) { return JsonSerializer.Deserialize(json); } } }