Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to save chat history in Json format, for later upload history from file? #201

Open
AzerQ opened this issue Mar 31, 2024 · 1 comment

Comments

@AzerQ
Copy link

AzerQ commented Mar 31, 2024

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?

@riiight
Copy link

riiight commented May 6, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants