Advanced
Importing/Exporting Chats

Importing and Exporting Chats

Sometimes, when you need to continue a Chat, its necessary to export your chat into some form of a file, or localStorage, and then later import it again. bard-ai has the ability to do this built in.

Example

Let's start by creating a new chat.

import Bard from "bard-ai";
 
let myBard = new Bard(COOKIE);
 
let myChat = myBard.createChat();

Now, let's ask a question.

console.log(await myChat.ask("What is 1+1?"));

We then call the export function.

myChat.export();

You should get an object similar to this:

{
    "conversationID": string,
    "responseID": string,
    "choiceID": string,
    "_reqID": string
}

And now import our conversation:

import Bard from "bard-ai";
 
let myBard = new Bard(COOKIE);
 
let myChatContinued = myBard.createChat({
  conversationID: string,
  responseID: string,
  choiceID: string,
  _reqID: string,
});

Finally, we ask Bard a continuation of the original question ("What is 1+1?"):

console.log(await myChatContinued.ask("Add 1 to that"));

Bard should be able to catch on, and say 3!

Usage

💡
export will be first explained, then how to re-import.

Chat.export()

Takes no parameters, and returns an object in this format:

{
    "conversationID": string,
    "responseID": string,
    "choiceID": string,
    "_reqID": string
}
️🚫

Make sure to not edit this JSON at all when importing. A tiny change will render it useless.

These IDs keep track of the entire conversation, which message you are on, and more.

bard-ai does not handle the actual saving of the conversation, so you'll have to supply your own file control and/or localStorage control. This is because of 2 things, that are somewhat intertwined:

createChat(IDs)

createChat() takes in one argument, the ID object you obtained from the export above. The chat will simply be initialized with the IDs (and remember previous messages), and can then be used as normal.

Learn more about createChat here.