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

Can't generate response when using old chat export only works with new chat #24

Closed
Nessie-Dev opened this issue Jul 10, 2023 · 26 comments
Closed
Labels
bug Something isn't working

Comments

@Nessie-Dev
Copy link

when i use old chat export it only returns "Google Bard encountered an error )]}'

39
[["wrb.fr",null,null,null,null,[13]]]
58
[["di",8769],["af.httprm",8768,"6620395296999733811",0]]
25
[["e",4,null,null,134]]
.
at queryBard (file:///home/runner/XaviaBot/node_modules/bard-ai/index.js:88:15)
at processTicksAndRejections (node:internal/process/task_queues:96:5)"here's my code:import Bard from "bard-ai";
import sqlite3 from "sqlite3";
import fetch from 'node-fetch'
global.fetch = fetch;

Bard.init("YAgdpqonMlUB0ELzDB_hFNjqXtv-4iXn29DaSsr-abQGyVy8rvehwyE8Q0EfyvBGbOCmQw.");

const db = new sqlite3.Database("database.db");

db.run(CREATE TABLE IF NOT EXISTS users ( uid TEXT PRIMARY KEY, conversationID TEXT, responseID TEXT, choiceID TEXT, _reqID TEXT ));

async function generateResponse(prompt, uid) {
prompt = decodeURIComponent(prompt);

if (!prompt) {
throw new Error("Please provide a prompt.");
}

if (!uid) {
const response = await Bard.askAI(prompt, { useJSON: true });
const images = response.images ? response.images.map((image) => image.url) : [];
const contentWithoutImages = response.content?.replace(/![.?]([^)]+)\n/g, "") || response.content;
const modifiedResponse = {
content: contentWithoutImages,
...(images.length > 0 && { images }),
};

return modifiedResponse;

}

try {
const userData = await getUserData(uid);
console.log(userData)

if (userData) {
  const { conversationID, responseID, choiceID, _reqID } = userData;
  const userChat = new Bard.Chat({ conversationID, responseID, choiceID, _reqID });
  const response = await userChat.ask(prompt, { useJSON: true });
  const ID = userChat.export();
  const images = response.images ? response.images.map((image) => image.url) : [];
  const contentWithoutImages = response.content?.replace(/\!\[.*?\]\([^)]+\)\n*/g, "") || response.content;
  const modifiedResponse = {
    content: contentWithoutImages,
    ...(images.length > 0 && { images }),
  };

  await updateUserData(uid, {
    conversationID: ID.conversationID,
    responseID: ID.responseID,
    choiceID: ID.ids.choiceID,
    _reqID: ID._reqID,
  });

  return modifiedResponse;
} else {
  const newChat = new Bard.Chat();
  const response = await newChat.ask(prompt, { useJSON: true });
  const ID = newChat.export();
  const images = response.images ? response.images.map((image) => image.url) : [];
  const contentWithoutImages = response.content?.replace(/\!\[.*?\]\([^)]+\)\n*/g, "") || response.content;
  const newUser = {
    uid: uid,
    conversationID: ID.conversationID,
    responseID: ID.responseID,
    choiceID: ID._reqIDchoiceID,
    _reqID: ID._reqID,
  };

  await createUserData(newUser);

  const modifiedResponse = {
    content: contentWithoutImages,
    ...(images.length > 0 && { images }),
  };

  return modifiedResponse;
}

} catch (error) {
console.error("Failed to generate response:", error);
throw new Error("Failed to generate response.");
}
}

async function getUserData(uid) {
return new Promise((resolve, reject) => {
db.get("SELECT * FROM users WHERE uid = ?", uid, (err, row) => {
if (err) {
console.error("Failed to retrieve user data:", err);
reject(err);
} else {
resolve(row);
}
});
});
}

async function createUserData(userData) {
return new Promise((resolve, reject) => {
db.run(
"INSERT INTO users (uid, conversationID, responseID, choiceID, _reqID) VALUES (?, ?, ?, ?, ?)",
[userData.uid, userData.conversationID, userData.responseID, userData.choiceID, userData._reqID],
(err) => {
if (err) {
console.error("Failed to create user data:", err);
reject(err);
} else {
resolve();
}
}
);
});
}

async function updateUserData(uid, userData) {
return new Promise((resolve, reject) => {
db.run(
"UPDATE users SET conversationID = ?, responseID = ?, choiceID = ?, _reqID = ? WHERE uid = ?",
[userData.conversationID, userData.responseID, userData.choiceID, userData._reqID, uid],
(err) => {
if (err) {
console.error("Failed to update user data:", err);
reject(err);
} else {
resolve();
}
}
);
});
}

async function resetChat(uid) {
try {
await deleteUserData(uid);
} catch (error) {
console.error("Failed to reset chat:", error);
throw new Error("Failed to reset chat.");
}
}

async function deleteUserData(uid) {
return new Promise((resolve, reject) => {
db.run("DELETE FROM users WHERE uid = ?", uid, (err) => {
if (err) {
console.error("Failed to delete user data:", err);
reject(err);
} else {
resolve();
}
});
});
}

export { generateResponse, resetChat };

@ThatXliner
Copy link
Collaborator

Please provide an MRE

@EvanZhouDev
Copy link
Owner

Closing otherwise. We respond to issues regarding bard-ai, we are not necessarily responsible for how you use it specifically unless it's a clear issue on our part.

@Nessie-Dev
Copy link
Author

Nessie-Dev commented Jul 11, 2023

It's related to bard-ai when i export chat and then import it it to get responses from bard-ai I get is, means I can't use the ID's of the chat to continue an old chat "Google Bard encountered an error )]}'

39
[["wrb.fr",null,null,null,null,[13]]]
58
[["di",8769],["af.httprm",8768,"6620395296999733811",0]]
25
[["e",4,null,null,134]]
.
at queryBard (file:///home/runner/XaviaBot/node_modules/bard-ai/index.js:88:15)
at processTicksAndRejections (node:internal/process/task_queues:96:5)"

@EvanZhouDev
Copy link
Owner

It works for me?
Ensure you are giving Chat a JSON (not stringified) when importing. It will not work if your input is stringified.

@Nessie-Dev
Copy link
Author

It worked before, but does not works after the update last update came.

@EvanZhouDev
Copy link
Owner

Go ahead and purge cookies, then log in again to your Google account.
Get the new PSID, and it should be all fixed!

@Nessie-Dev
Copy link
Author

did but same error brother, can you give me an example again I tried same as shown as on the docs

@EvanZhouDev
Copy link
Owner

It works? Are you sure you are using __Secure-PSID1?

@Nessie-Dev
Copy link
Author

Yes, it works when i create new chat everytime for response, but not works with the chat export and import IDs

@EvanZhouDev
Copy link
Owner

Can you show me what you are exporting and importing? Make sure its raw JSON and not stringified or changed in any way shape or form.

@Nessie-Dev
Copy link
Author

here's my code:const userData = await getUserData(uid);

if (userData) {
  const { conversationID, responseID, choiceID, _reqID } = userData;
  const userChat = new Bard.Chat({
    conversationID: conversationID,
    responseID: responseID,
    choiceID: choiceID,
    _reqID: _reqID,
  });
  const response = await userChat.ask(prompt, { useJSON: true });
  const images = response.images ? response.images.map((image) => image.url) : [];
  const contentWithoutImages = response.content?.replace(/\!\[.*?\]\([^)]+\)\n*/g, "") || response.content;
  const modifiedResponse = {
    content: contentWithoutImages,
    ...(images.length > 0 && { images }),
  };

  await updateUserData(uid, {
    conversationID: response.ids.conversationID,
    responseID: response.ids.responseID,
    choiceID: response.ids.choiceID,
    _reqID: response.ids._reqID,
  });

  return modifiedResponse;
} else {
  const newChat = new Bard.Chat();
  const response = await newChat.ask(prompt, { useJSON: true });
  const images = response.images ? response.images.map((image) => image.url) : [];
  const contentWithoutImages = response.content?.replace(/\!\[.*?\]\([^)]+\)\n*/g, "") || response.content;
  const newUser = {
    uid: uid,
    conversationID: response.ids.conversationID,
    responseID: response.ids.responseID,
    choiceID: response.ids.choiceID,
    _reqID: response.ids._reqID,
  };

  await createUserData(newUser);

  const modifiedResponse = {
    content: contentWithoutImages,
    ...(images.length > 0 && { images }),
  };

  return modifiedResponse;
}

@EvanZhouDev
Copy link
Owner

If you just use the most basic example, does it work?
(Please join Slack, its a lot easier to talk there 😄)

@EvanZhouDev
Copy link
Owner

Any updates? Closing otherwise...

@EvanZhouDev
Copy link
Owner

Closing for now, feel free to reopen if you deem necessary.

@therealsujitk
Copy link

I'm facing the same issue, can't figure out why.

@ThatXliner ThatXliner added the bug Something isn't working label Jul 26, 2023
@ThatXliner
Copy link
Collaborator

ThatXliner commented Jul 26, 2023

I'm facing the same issue, can't figure out why.

could you show us an MRE of your code?

@EvanZhouDev i suspect it’s an issue with cookies

@ThatXliner ThatXliner reopened this Jul 26, 2023
@therealsujitk
Copy link

@ThatXliner It seems to work fine when I write an example and run it in the terminal. I'm trying to using this package in a chrome extension which gives this error (It works the first time when result.bardConversationIds is undefined).

Bard.init(bardKey)
  .then(() => chrome?.storage?.local?.get('bardConversationIds'))
  .then((result) => new Bard.Chat(result.bardConversationIds))
  .then(async (conversation) => {
    const response = await conversation.ask("Hello world");
    chrome?.storage?.local?.set({bardConversationIds: conversation.export()});
    return response;
  })
  .then((response) => console.log(response));
  • I logged the stored ids object and it seems like it's being stored and retrieved correctly.
    Screenshot 2023-08-01 at 01 46 21
  • I hard-coded the ids object in the constructor but it still gave the same error.
    .then((result) => new Bard.Chat({
      conversationID: 'c_e29fe6192981cfe1',
      responseID: 'r_e29fe6192981c15a',
      choiceID: 'rc_e29fe6192981c8b7',
      _reqID: 100000
    }))

@ThatXliner
Copy link
Collaborator

Why console.log(response) giving an object? IIRC it should’ve been a string unless the json config was set

@therealsujitk
Copy link

The object is logged when I do console.log(result.bardConversationIds). The response string gets logged properly when result.bardConversationIds is undefined, in this case it never gets to the response.

@therealsujitk
Copy link

Could it be browser cookies from bard.google.com interfering with this? I noticed even when I don't specify a bardKey, it still works since I'm signed in to bard in my browser.

Note: I'm using this package with a chrome extension.

@Nessie-Dev
Copy link
Author

the issue hasn't been fixed yet, also now i changed my cookies more than 10 times by logging the Gmail, and then logging in it says my cookie isn't valid.

@ThatXliner
Copy link
Collaborator

the issue hasn't been fixed yet, also now i changed my cookies more than 10 times by logging the Gmail, and then logging in it says my cookie isn't valid.

On "change cookie," did you copy+paste the new cookie into your code?

@EvanZhouDev
Copy link
Owner

Also, you need to make sure you clear your cookies, not just logging in and out of Gmail... not sure if that was implied?

@EvanZhouDev
Copy link
Owner

Have you tried using v2? It should be better.

Let me know if you have any updates, I will close if not...

@therealsujitk
Copy link

I get an error while trying to build my project. This error doesn't show up in v.1.5.4.

Module not found: Error: Can't resolve 'fs' in '/Users/sujit/Documents/My Projects/chrome-ext-litcode-solver/node_modules/bard-ai'

@ThatXliner
Copy link
Collaborator

I get an error while trying to build my project. This error doesn't show up in v.1.5.4.

Module not found: Error: Can't resolve 'fs' in '/Users/sujit/Documents/My Projects/chrome-ext-litcode-solver/node_modules/bard-ai'

Please create a new issue. also, this is probably because this project is currently uses the nodejs fs module

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants