Bard-API
Bard-API copied to clipboard
Want to hold a conversation
I would like to retain the session data after the session is disconnected and use that data to retain the conversation when I re-instantiate the session. It seems possible if I can retain the conversation_id and reuse it.
Please check readme. https://github.com/dsdanielpark/Bard-API#reusable-session-object
Since there have been repeated instances of duplicate questions, this issue will remain open and unresolved.
Reusable session object
You can continue the conversation using a reusable session.
from bardapi import Bard
import os
import requests
os.environ['_BARD_API_KEY'] = 'xxxxxxxxxxx'
# token='xxxxxxxxxxx'
session = requests.Session()
session.headers = {
"Host": "bard.google.com",
"X-Same-Domain": "1",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36",
"Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
"Origin": "https://bard.google.com",
"Referer": "https://bard.google.com/",
}
session.cookies.set("__Secure-1PSID", os.getenv("_BARD_API_KEY"))
# session.cookies.set("__Secure-1PSID", token)
bard = Bard(session=session, timeout=30)
bard.get_answer("나와 내 동년배들이 좋아하는 뉴진스에 대해서 알려줘")['content']
# Continued conversation without set new session
bard.get_answer("What is my last prompt??")['content']
What's the actual significance of timeout arg in the Bard class?
What's the actual significance of timeout arg in the Bard class?
You can adjust the session response timeout in seconds. In the service layer, you can handle situations where the response time exceeds the timeout and take alternative actions. However, it may be used differently depending on the user type.
I'm using the reusable session object you refer to in the code above, but I don't seem to be getting continuous conversational context. I confirmed by comparing the responses to two subsequent related questions to the Google site, then asking the same two questions with the same reusable session object through the API, got different results, and then reproduced those different results by resetting the chat on the Google site between the two questions. Long story short, the chat is still reset between calls even with the reusable session. After reviewing the API code, it looks like what we are actually needing to feed subsequent calls in the same context is the conversation_id value, not just the session. Am I crazy?
This would maybe be my edit to the init function on the Bard class? I will try it locally to confirm it does the trick before I submit a PR.
def __init__(
self,
token: str = None,
timeout: int = 20,
proxies: dict = None,
session: requests.Session = None,
**conversation_id = None,**
language: str = None,
run_code: bool = False,
):
"""
Initialize the Bard instance.
Args:
token (str): Bard API token.
timeout (int): Request timeout in seconds.
proxies (dict): Proxy configuration for requests.
**conversation_id: Value for identifying context?**
session (requests.Session): Requests session object.
language (str): Language code for translation (e.g., "en", "ko", "ja").
"""
self.token = token or os.getenv("_BARD_API_KEY")
self.proxies = proxies
self.timeout = timeout
self._reqid = int("".join(random.choices(string.digits, k=4)))
# Add conversation_id as parameter option
self.conversation_id = ""
self.response_id = ""
self.choice_id = ""
# Set session
**if conversation_id != None:
self.conversation_id = conversation_id**
if session is None:
self.session = requests.Session()
self.session.headers = SESSION_HEADERS
self.session.cookies.set("__Secure-1PSID", self.token)
else:
self.session = session
self.SNlM0e = self._get_snim0e()
self.language = language or os.getenv("_BARD_API_LANG")
self.run_code = run_code or False
Totally worked! I got the same response from the query pair with these changes as I got from the Google site! I'll go ahead and submit the PR.
It would be a good idea to check for follow-up issues regarding related content. While some persistence is possible through context IDs and sessions, a complete implementation is not feasible. Therefore, consider options such as creating a database or sending multiple conversation queries simultaneously. Thank you.