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";
 
await Bard.init(COOKIE_KEY);
 
let myConversation = new Bard.Chat();

Now, let's ask a question.

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

We then call the export function.

myConversation.export();

You should get a object similar to this:

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

Now, we create a new conversation, but we use the JSON we got to create it, like this:

import Bard from "bard-ai";
 
await Bard.init(COOKIE_KEY);
 
let myConversationContinued = new Bard.Chat({
	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 myConversationContinued.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.

Bard.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 to not work.

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

bard-ai does not handle saving 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:

  • The Single Responsibility Principle (SRP), that says each part of your code should do one thing.
  • Keeps the library size down, to make it lighter and faster to download.

Bard.Chat(IDs)

Bard.Chat() takes in one argument, the IDs you obtained from the export above. The chat will simply be initialized with the IDs, and can then be used as normal.

Learn more about Bard.Chat here.