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

Commit

Permalink
doc: add async example code
Browse files Browse the repository at this point in the history
  • Loading branch information
dsdanielpark committed Jan 18, 2024
1 parent 721c1da commit 05652f0
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 19 deletions.
26 changes: 19 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,24 @@ I referred to this github repository([github.com/acheong08/Bard](https://github.
- [More features](#more-features)
- [Amazing Bard Prompts Is All You Need!](#amazing-bard-prompts-is-all-you-need)
- [The Python package hf-transllm](#the-python-package-hf-transllm)
- [Example code of hf-transllm](#example-code-of-hf-transllm)
- [Usage](#usage-1)
- [For LLM that use languages `other than English`](#for-llm-that-use-languages-other-than-english)
- [For LLM that use `English`](#for-llm-that-use-english)
- [What is Google Gemini?](#what-is-google-gemini)
- [Google AI Studio](#google-ai-studio)
- [Access to Gemini Pro in Bard API package](#access-to-gemini-pro-in-bard-api-package)
- [Google PaLM](#google-palm)
- [Quick Start](#quick-start)
- [Sponsor](#sponsor)
- [FAQ](#faq)
- [Scripts](#scripts)
- [Contributors](#contributors)
- [License](#license)
- [Shifting Service Policies: Bard and Google's Dynamics](#shifting-service-policies-bard-and-googles-dynamics)
- [Bugs and Issues](#bugs-and-issues)
- [Contacts](#contacts)
- [Reference](#reference)



Expand Down Expand Up @@ -214,10 +226,8 @@ 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",
Expand All @@ -226,8 +236,6 @@ SESSION_HEADERS = {
"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

Expand All @@ -241,9 +249,13 @@ client = AsyncClient(

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'])
# Asynchronous function to get the answer
async def get_bard_answer(question):
await bard_async.async_setup() # Ensure async setup is done
return await bard_async.get_answer(question)

response = await get_bard_answer("나와 내 동년배들이 좋아하는 뉴진스에 대해서 알려줘")
print(response['content'])
```

</details>
Expand Down
47 changes: 35 additions & 12 deletions documents/README_DEV.md
Original file line number Diff line number Diff line change
Expand Up @@ -287,20 +287,43 @@ bard.get_answer("code a pie chart in python for this data={'blue':25, 'red':30,
Using asynchronous implementation will be efficient when implementing ChatBots or something alone those lines.
BardAsync is not using requests library instead it is using httpx library and http2 protocol.

BardAsync is present in translate_to.core_async.BardAsync
```python
from bardapi import BardAsync

bard = BardAsync(token="xxxxxxxx")
await bard.get_answer("What is Metaverse?")
```
or
```python
import asyncio
from httpx import AsyncClient
from bardapi import BardAsync

bard = BardAsync(token="xxxxxxxx")
asyncio.run(bard.get_answer("What is Metaverse?"))
import os

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

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/",
}
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)

# Asynchronous function to get the answer
async def get_bard_answer(question):
await bard_async.async_setup() # Ensure async setup is done
return await bard_async.get_answer(question)

response = await get_bard_answer("나와 내 동년배들이 좋아하는 뉴진스에 대해서 알려줘")
print(response['content'])
```

<br>
Expand Down

0 comments on commit 05652f0

Please sign in to comment.