Skip to content
This repository has been archived by the owner on Apr 24, 2024. It is now read-only.

Commit

Permalink
doc: update async reusable client code on readme
Browse files Browse the repository at this point in the history
  • Loading branch information
dsdanielpark authored Dec 27, 2023
1 parent 7d39c77 commit d3c81b9
Showing 1 changed file with 50 additions and 3 deletions.
53 changes: 50 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,14 +187,16 @@ bard = Bard(token='xxxxxxx', proxies=proxies, timeout=30)
bard.get_answer("나와 내 동년배들이 좋아하는 뉴진스에 대해서 알려줘")['content']
```

To make the async bard code collapsible in Markdown, you can use HTML details and summary tags. This allows the code to be hidden under a summary title, which can be clicked to expand and view the full code block. Here's how you can modify your Markdown content:

### Reusable session object
You can continue the conversation using a reusable session.
```python
from bardapi import Bard
import os
import requests
os.environ['_BARD_API_KEY'] = 'xxxxxxx'
# token='xxxxxxx'
# import os
# os.environ['_BARD_API_KEY'] = 'xxxxxxx'
token='xxxxxxx'

session = requests.Session()
session.headers = {
Expand All @@ -215,6 +217,51 @@ bard.get_answer("나와 내 동년배들이 좋아하는 뉴진스에 대해서
bard.get_answer("What is my last prompt??")['content']
```

<details>
<summary>Async Bard Code (Click to expand)</summary>

```python
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'])
```

</details>


### Auto Cookie Bard
Using [browser_cookie3](https://github.com/borisbabic/browser_cookie3) we extract the `__Secure-1PSID` cookie from all browsers, and then we can use the API without passing the token. However, there are still incomplete dependency packages and various variables, so please seek assistance in the following [GitHub Issues](https://github.com/borisbabic/browser_cookie3/issues) or adjust your browser's version.
- Visit https://bard.google.com/ in your browser and execute the following command while in the chat-enabled state. Refer to browser_cookie3 for details on how it works. If any issues arise, please restart the browser or log in to your Google account again.
Expand Down

0 comments on commit d3c81b9

Please sign in to comment.