dsdanielpark / Bard-API

The unofficial python package that returns response of Google Bard through cookie value.

Home Page:https://pypi.org/project/bardapi/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

BardAsync with continuous conversation

ahmadkeren opened this issue · comments

bard = BardAsync(token=tokenBard, session=session, timeout = 60)
jawaban = await bard.get_answer(q)
return jawaban
#res: init() got an unexpected keyword argument 'session

I'm currently working on this, maybe around 3 man day. Anyone can contribute to this issue; it's not that difficult.

still patiently waiting, hehe :)

ahmadkeren

Did you have a joyful Christmas? Wishing you a happy conclusion to the year-end as well~~

The feature to accept a session object as an argument will be completed by today. Please refer to the GitHub dev version for usage.

However, if your goal is to maintain other contexts, achieving that may be challenging. Please refer to FAQ #07 for more details.

In fact, I'm using a small LLM developed locally, so I only use bard when I really need to show something experimental. Therefore, I haven't looked into async as another contributor was working on it for learning purposes, but it seems there's still a lot to be fixed.

Due to lack of time for QA, I can't release an official version... Please use the GitHub dev version for now.

$ pip install git+https://github.com/dsdanielpark/Bard-API.git

Overall, additional work such as organizing the call order is needed, but for now, I've fixed the major issues.

Please reuse the resuable client object as I wrote in the FAQ. As mentioned, it might remember about one of my previous questions.

As noted in the FAQ, keeping a chatbot consistently having the same personality and persona is a high-cost, time-consuming task even in the LLM field. Therefore, if you're trying to maintain consistent context in that sense, it might not be very helpful.

Please try using the examples above, and if you encounter any further errors, I would appreciate it if you let me know. (Currently, regular objects are more optimized, so using a reusable session might be faster. I have too much work to review and optimize all async classes right now. Sorry.)

Let me know if there are any errors. Hope you have a happy end of the year and a hopeful new year. Thank you~~

from httpx import AsyncClient
from bardapi import BardAsync
import os

# Uncomment and set your API key as needed
# os.environ['_BARD_API_KEY'] = 'xxxxxxx'

token = 'xxxxxxx'  # Replace with your actual token

# Define SESSION_HEADERS
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/",
}

# Set timeout and proxies as needed
timeout = 30  # Example timeout
proxies = {}  # Replace with your proxies if needed

client = AsyncClient(
    http2=True,
    headers=SESSION_HEADERS,
    cookies={"__Secure-1PSID": token},
    timeout=timeout,
    proxies=proxies,
)

bard_async = BardAsync(token=token, client=client)

# Example usage (ensure you are in an async context or use asyncio.run for testing)
# response = await bard_async.get_answer("Tell me about NewJeans, popular among my peers")
# print(response['content'])

https://github.com/dsdanielpark/Bard-API?tab=readme-ov-file#reusable-session-object

In fact, I'm using a small LLM developed locally, so I only use bard when I really need to show something experimental. Therefore, I haven't looked into async as another contributor was working on it for learning purposes, but it seems there's still a lot to be fixed.

Due to lack of time for QA, I can't release an official version... Please use the GitHub dev version for now.

$ pip install git+https://github.com/dsdanielpark/Bard-API.git

Overall, additional work such as organizing the call order is needed, but for now, I've fixed the major issues.

Please reuse the resuable client object as I wrote in the FAQ. As mentioned, it might remember about one of my previous questions.

As noted in the FAQ, keeping a chatbot consistently having the same personality and persona is a high-cost, time-consuming task even in the LLM field. Therefore, if you're trying to maintain consistent context in that sense, it might not be very helpful.

Please try using the examples above, and if you encounter any further errors, I would appreciate it if you let me know. (Currently, regular objects are more optimized, so using a reusable session might be faster. I have too much work to review and optimize all async classes right now. Sorry.)

Let me know if there are any errors. Hope you have a happy end of the year and a hopeful new year. Thank you~~

from httpx import AsyncClient
from bardapi import BardAsync
import os

# Uncomment and set your API key as needed
# os.environ['_BARD_API_KEY'] = 'xxxxxxx'

token = 'xxxxxxx'  # Replace with your actual token

# Define SESSION_HEADERS
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/",
}

# Set timeout and proxies as needed
timeout = 30  # Example timeout
proxies = {}  # Replace with your proxies if needed

client = AsyncClient(
    http2=True,
    headers=SESSION_HEADERS,
    cookies={"__Secure-1PSID": token},
    timeout=timeout,
    proxies=proxies,
)

bard_async = BardAsync(token=token, client=client)

# Example usage (ensure you are in an async context or use asyncio.run for testing)
# response = await bard_async.get_answer("Tell me about NewJeans, popular among my peers")
# print(response['content'])

https://github.com/dsdanielpark/Bard-API?tab=readme-ov-file#reusable-session-object

thank you so much and happy end of the year :)

Sorry, did you forget to input the client parameters into init? or is there something wrong with my code?

I get error: init() got an unexpected keyword argument 'client'
image

My code:
image
image

ahmadkeren

I initially intended to unify the argument names under 'session', but have since revised this to use 'client' as the argument name.

In other words, the synchronous bard object uses session, while the asynchronous bard object uses client.

This information has been promptly updated.