Is it possible to save chat history in Json format, for later upload history from file?
Hi, recently saw your library, it fits my needs perfectly. I am using the original Nuget package, I am now loading and saving data with the following code:
public static Conversation LoadChatMessagesFromFile(Conversation chat, string path)
{
string inputJson = File.ReadAllText(path);
var messages = JsonConvert.DeserializeObject<IList<ChatMessage>>(inputJson);
chat.Messages.Clear();
foreach (var chatMessage in messages)
{
chat.Messages.Add(chatMessage);
}
return chat;
}
public static void SaveChatMessagesToFile(Conversation chat, string path)
{
string jsonData = JsonConvert.SerializeObject(chat.Messages, Formatting.Indented);
File.WriteAllText(path, jsonData);
}
Serialization is done using Newtonsoft.Json:
[
{
"role": "user",
"content": [
{
"type": "text",
"text": "Hi, whats up?",
"image_url": null
}
],
"name": null
},
{
"role": "assistant",
"content": [
{
"type": "text",
"text": "Hello! I'm just here to help with any questions or tasks you may have. How can I assist you today?",
"image_url": null
}
],
"name": null
},
{
"role": "user",
"content": [
{
"type": "text",
"text": "Talk about yourself",
"image_url": null
}
],
"name": null
},
{
"role": "assistant",
"content": [
{
"type": "text",
"text": "I am a language model AI designed to assist with a wide range of tasks and provide information on various topics. I am constantly learning and improving to better serve users like you. Is there anything specific you would like to know or discuss?",
"image_url": null
}
],
"name": null
}
]
But an exception is thrown when loading in runtime: line:
var messages = JsonConvert.DeserializeObject<IList<ChatMessage>>(inputJson);
exception: Newtonsoft.Json.JsonSerializationException: "Error setting value to 'ContentItems' on 'OpenAI_API.Chat.ChatMessage'." innerException: NullReferenceException: Object reference not set to an instance of an object. Any advice on how this can be fixed?
Just wanted to say that I am also experiencing this. I didn't have problems serializing using an old version of the library. Just started happening when I upgraded today.