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

Bard AI native integration with textbase. #118

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Coming soon:
- [x] [PyPI package](https://pypi.org/project/textbase-client/)
- [x] Easy web deployment via [textbase deploy](/docs/deployment/deploy-from-cli)
- [ ] SMS integration
- [ ] Native integration of other models (Claude, Llama, ...)
- [X] Native integration of other models (Claude, Llama, ...), Bard AI added.

![Demo Deploy GIF](assets/textbase-deploy.gif)

Expand Down
42 changes: 42 additions & 0 deletions examples/bardAi-bot/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Bard AI Bot

A bot using bardAi developed using a textbase framework.

<br />

## Installation
---
Make sure you have `python version >=3.9.0`, it's always good to follow the [docs](https://docs.textbase.ai/) 👈🏻
### 1. Follow the installation steps in the [documentation of the textbase](https://docs.textbase.ai/get-started/installation)👈🏻 or alternatively just 1pip install -r requirements.txt` but make sure you get your virtual env running before installing from the file.

### 2. Give the `main.py` of this bardAi while running the project.

### 3. If you are using the `textbase-bot` repo for the front end make sure to replace the API URL as mentioned in the `textbase-bot`repository.

<br />

## Usage
---
Make sure you have Bard AI opened in the browser
### 1. BardAi doesn't provide API key publically i need to fetch it from developer tools of the cookie section of the browser.
### 9. There is __Secure-1PSID under Bard cookies. need to delete cookies and relogin if the backend is giving errors just like we are having.


Refer to this [video link](https://drive.google.com/file/d/1ZDqygsWxe2S_aA60u1ZrxIZhL09uGKjx/view?usp=sharing) for more ideas from configuring the API key to deploying the bot to textbase website I have recorded them all.


There are known corrections and future modifications to this project feel free to contribute if you want to.
- [X] Native integration of other models (BardAi)
- [ ] Need to display the responses as Google Brad displays with images and links on images as well.

<br />

## Demo
---
I'm sure this will not work because the API key must have expired when you are watching this repo but here is a link.
https://bot.textbase.ai/pspofficial29/bardai


### Note
---
I know that this api key regenerating is a tedioud task but what should we do? we can't control how third party works! Here is a hope https://www.googlecloudcommunity.com/gc/AI-ML/Google-Bard-API/m-p/538517.
31 changes: 31 additions & 0 deletions examples/bardAi-bot/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from textbase import bot, Message
from typing import List
from textbase.models import BardAI

@bot()
def on_message(message_history: List[Message], state: dict = None):

bot_response = BardAI.generate(message_history=message_history)

response = {
"data": {
"messages": [
{
"data_type": "STRING",
"value": bot_response
}
],
"state": state
},
"errors": [
{
"message": ""
}
]
}


return {
"status_code": 200,
"response": response
}
108 changes: 108 additions & 0 deletions requirements.txt
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why all these packages?

Copy link
Author

@Psp29onetwo Psp29onetwo Sep 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just auto generated the requirements.txt should i only keep the required ones?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok letme correct this

Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
aiohttp==3.8.5
aiosignal==1.3.1
altair==5.1.1
annotated-types==0.5.0
anyio==4.0.0
async-timeout==4.0.3
attrs==23.1.0
bardapi==0.1.33
beautifulsoup4==4.12.2
blinker==1.6.2
browser-cookie3==0.19.1
cachetools==5.3.1
certifi==2023.7.22
charset-normalizer==3.2.0
click==8.1.7
cloudevents==1.9.0
colorama==0.4.6
deep-translator==1.11.4
deprecation==2.1.0
EasyProcess==1.1
entrypoint2==1.1
Flask==2.3.3
frozenlist==1.4.0
functions-framework==3.4.0
gitdb==4.0.10
GitPython==3.1.34
google-api-core==2.11.1
google-auth==2.22.0
google-cloud-core==2.3.3
google-cloud-translate==3.12.0
googleapis-common-protos==1.60.0
grpcio==1.57.0
grpcio-status==1.57.0
h11==0.14.0
h2==4.1.0
hpack==4.0.0
httpcore==0.17.3
httpx==0.24.1
hyperframe==6.0.1
idna==3.4
importlib-metadata==6.8.0
itsdangerous==2.1.2
Jinja2==3.1.2
jsonschema==4.19.0
jsonschema-specifications==2023.7.1
langdetect==1.0.9
lz4==4.3.2
markdown-it-py==3.0.0
MarkupSafe==2.1.3
mdurl==0.1.2
MouseInfo==0.1.3
mss==9.0.1
multidict==6.0.4
numpy==1.25.2
openai==0.27.10
packaging==23.1
pandas==2.1.0
Pillow==9.5.0
proto-plus==1.22.3
protobuf==4.24.2
pyarrow==13.0.0
pyasn1==0.5.0
pyasn1-modules==0.3.0
PyAutoGUI==0.9.54
pycryptodomex==3.18.0
pydantic==2.3.0
pydantic_core==2.6.3
pydeck==0.8.0
PyGetWindow==0.0.9
Pygments==2.16.1
Pympler==1.0.1
PyMsgBox==1.0.9
pyperclip==1.8.2
PyRect==0.2.0
pyscreenshot==3.1
PyScreeze==0.1.29
python-dateutil==2.8.2
python-dotenv==1.0.0
pytweening==1.0.7
pytz==2023.3
pytz-deprecation-shim==0.1.0.post0
referencing==0.30.2
requests==2.31.0
rich==13.5.2
rpds-py==0.10.0
rsa==4.9
six==1.16.0
smmap==5.0.0
sniffio==1.3.0
soupsieve==2.5
tabulate==0.9.0
tenacity==8.2.3
termcolor==2.3.0
toml==0.10.2
toolz==0.12.0
tornado==6.3.3
tqdm==4.66.1
typing_extensions==4.7.1
tzdata==2023.3
tzlocal==4.3.1
urllib3==1.26.16
validators==0.22.0
watchdog==3.0.0
Werkzeug==2.3.7
wikipedia==1.4.0
yarl==1.9.2
yaspin==3.0.0
zipp==3.16.2
24 changes: 23 additions & 1 deletion textbase/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

from textbase import Message

import os
from bardapi import Bard
# Return list of values of content.
def get_contents(message: Message, data_type: str):
return [
Expand Down Expand Up @@ -143,4 +145,24 @@ def generate(
data = json.loads(response.text) # parse the JSON data into a dictionary
message = data['message']

return message
return message


class BardAI:

# Search for __Secure-1PSID under the bard cookies make sure to copy the api key with the . at the end
api_key = "BARD_AI_API_KEY"

@classmethod
def generate(
cls,
message_history: list[Message]
):
assert cls.api_key is not None, "BardAI API key is not set."
os.environ["_BARD_API_KEY"] = cls.api_key

most_recent_message = get_contents(message_history[-1], "STRING")

response = Bard().get_answer(str(most_recent_message))

return response["content"]