Git Product home page Git Product logo

ra83205 / google-bard-api Goto Github PK

View Code? Open in Web Editor NEW
255.0 6.0 65.0 36 KB

This project provides a FastAPI wrapper for interacting with Google Bard, a conversational AI by Google. It allows users to send messages to Google Bard and receive responses through a simple API.

Python 87.41% Dockerfile 6.79% Shell 5.81%
chat-gpt fastapi google-bard ai-assistant chat-interface chatbot-development dialog-systems machine-learning natural-language-processing python

google-bard-api's Introduction

Google Bard API

Welcome to the Google Bard API project! This repository contains a FastAPI wrapper designed to seamlessly interact with 🤖 Google Bard, a remarkable conversational AI developed by Google. The project encompasses a comprehensive library for effortlessly dispatching requests to Google Bard, along with a streamlined API server that empowers you to interact with Bard through a REST API. This exceptional fusion of resources facilitates the integration of Bard's capabilities into your applications or services via a single, straightforward API endpoint.

Getting Started 🚀

Let's embark on a journey to set up and launch the project on your local machine for developmental and testing purposes.

Prerequisites 📋

Before we dive in, ensure you have the following prerequisites:

  • Python 3.9 or newer 🐍
  • Docker 🐳

Installation 🛠️

  1. Begin by cloning the repository:

    git clone https://github.com/ra83205/google-bard-api.git
    cd google-bard-api
  2. Create a virtual environment and install the necessary packages:

    python3 -m venv venv
    source venv/bin/activate
    pip install -r requirements.txt
  3. Launch the application using Uvicorn:

    uvicorn main:app --reload --port 8000

    Your application should now be live at http://localhost:8000.

Usage 📦

To utilize the power of the Google Bard API, follow these steps:

  1. Obtain the cookies as mentioned on bard.google.com from an authorized session. These cookies will be used to send POST requests to the /ask endpoint, accompanied by a message in a JSON payload. Make sure to include the session_id, which corresponds to the __Secure-1PSID cookie, in your request.

  2. Example request using cURL:

    curl -X POST "http://localhost:8000/ask" -H "accept: application/json" -H "Content-Type: application/json" -d '{"session_id":"your-session-id","message":"What is the meaning of life?"}'
  3. Example request using Postman:

    • Open Postman and choose the "POST" method

    • Enter the URL: http://localhost:8000/ask

    • In the "Headers" tab, add a new key-value pair: Content-Type: application/json

    • In the "Body" tab, select the "raw" option and input the following JSON payload:

      {
        "session_id": "your-session-id",
        "message": "What is the meaning of life?"
      }
    • Click "Send" to submit the request. You'll receive a JSON response from Google Bard containing the response message.

    Note: To secure the /ask endpoint with an authentication key, set the USER_AUTH_KEY environment variable to a string value of your choice. The authentication key will be cross-referenced against the Authorization header of incoming requests.

Deployment 🚢

To deploy this application using Docker, embark on the following steps:

  1. Construct the Docker image:

    docker build -t your-image-name .
  2. Initiate the Docker container:

    docker run -p 8000:80 your-image-name

    The application will be accessible at http://localhost:8000.

Collaboration 👥

Are you excited to contribute and expand the horizons of this project? Collaboration is key! Feel free to fork the repository and propose your changes through pull requests. Your contributions will be meticulously reviewed and seamlessly merged if they align with the project's objectives.

License 📜

The code presented in this repository is generously licensed for unrestricted use, devoid of limitations or warranties.

Acknowledgments 🙌

A special thanks to:

  • Google for creating the visionary Google Bard
  • FastAPI for providing an elegant and efficient web framework

Your participation and contributions are truly appreciated! Happy coding! 🎉

google-bard-api's People

Contributors

hack-r avatar ra83205 avatar rozhnev avatar shuaixiaoqiang avatar static-voiddd avatar tsahil01 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

google-bard-api's Issues

Failing to get response

When running it seems form me it's failing at this functionality.
def __get_snlm0e(self): resp = self.session.get(url="https://bard.google.com/", timeout=10) # Find "SNlM0e":"<ID>" if resp.status_code != 200: raise Exception("Could not get Google Bard") print(resp.text) exit(0) SNlM0e = re.search(r"SNlM0e\":\"(.*?)\"", resp.text).group(1) return SNlM0e
For some reason he cannot finds the SNlM0e in the response text

Google Bard encountered an error

google-bard-api doessn't work.

The only response message i got was {"content":"Google Bard encountered an error: b')]}\'\n\n38\n[["wrb.fr",null,null,null,null,[9]]]\n56\n[["di",184],["af.httprm",184,"7440470310890705770",2]]\n25\n[["e",4,null,null,131]]\n'."}

I add print(resp.text) below this line, and got something like this:

)]}'

38
[["wrb.fr",null,null,null,null,[8]]]
56
[["di",238],["af.httprm",238,"6391811025503487755",1]]
25
[["e",4,null,null,131]]


That's absolutely a bad result. I wonder who is to blame?

Error on hitting post request

PS C:\Users\ArchanaChinnaiah\Google_Bard\code\google-bard-api> uvicorn main:app --reload --port 8000
←[32mINFO←[0m: Will watch for changes in these directories: ['C:\Users\ArchanaChinnaiah\Google_Bard\code\google-bard-api']
←[32mINFO←[0m: Uvicorn running on ←[1mhttp://127.0.0.1:8000←[0m (Press CTRL+C to quit)
←[32mINFO←[0m: Started reloader process [←[36m←[1m17704←[0m] using ←[36m←[1mStatReload←[0m
←[32mINFO←[0m: Started server process [←[36m49560←[0m]
←[32mINFO←[0m: Waiting for application startup.
←[32mINFO←[0m: Application startup complete.
←[32mINFO←[0m: 127.0.0.1:55623 - "←[1mPOST /ask HTTP/1.1←[0m" ←[91m500 Internal Server Error←[0m
←[31mERROR←[0m: Exception in ASGI application
Traceback (most recent call last):
File "C:\Python310\lib\site-packages\uvicorn\protocols\http\h11_impl.py", line 428, in run_asgi
result = await app( # type: ignore[func-returns-value]
File "C:\Python310\lib\site-packages\uvicorn\middleware\proxy_headers.py", line 78, in call
return await self.app(scope, receive, send)
File "C:\Python310\lib\site-packages\fastapi\applications.py", line 276, in call
await super().call(scope, receive, send)
File "C:\Python310\lib\site-packages\starlette\applications.py", line 122, in call
await self.middleware_stack(scope, receive, send)
File "C:\Python310\lib\site-packages\starlette\middleware\errors.py", line 184, in call
raise exc
File "C:\Python310\lib\site-packages\starlette\middleware\errors.py", line 162, in call
await self.app(scope, receive, _send)
File "C:\Python310\lib\site-packages\starlette\middleware\exceptions.py", line 79, in call
raise exc
File "C:\Python310\lib\site-packages\starlette\middleware\exceptions.py", line 68, in call
await self.app(scope, receive, sender)
File "C:\Python310\lib\site-packages\fastapi\middleware\asyncexitstack.py", line 21, in call
raise e
File "C:\Python310\lib\site-packages\fastapi\middleware\asyncexitstack.py", line 18, in call
await self.app(scope, receive, send)
File "C:\Python310\lib\site-packages\starlette\routing.py", line 718, in call
await route.handle(scope, receive, send)
File "C:\Python310\lib\site-packages\starlette\routing.py", line 276, in handle
await self.app(scope, receive, send)
File "C:\Python310\lib\site-packages\starlette\routing.py", line 66, in app
response = await func(request)
File "C:\Python310\lib\site-packages\fastapi\routing.py", line 237, in app
raw_response = await run_endpoint_function(
File "C:\Python310\lib\site-packages\fastapi\routing.py", line 163, in run_endpoint_function
return await dependant.call(**values)
File "C:\Users\ArchanaChinnaiah\Google_Bard\code\google-bard-api\main.py", line 25, in ask
chatbot = Chatbot(message.session_id)
File "C:\Users\ArchanaChinnaiah\Google_Bard\code\google-bard-api\Bard.py", line 99, in init
self.SNlM0e = self.__get_snlm0e()
File "C:\Users\ArchanaChinnaiah\Google_Bard\code\google-bard-api\Bard.py", line 106, in __get_snlm0e
SNlM0e = re.search(r"SNlM0e":"(.*?)"", resp.text).group(1)
AttributeError: 'NoneType' object has no attribute 'group'
←[32mINFO←[0m: 127.0.0.1:60154 - "←[1mGET / HTTP/1.1←[0m" ←[31m404 Not Found←[0m

AttributeError: 'NoneType' object has no attribute 'group'

This popped up today. Did Google finally kill off outside access? I think somehow the response from bard has changed.

I have tried a few new sessions from the cookie as per the readme, however facing the same result.

INFO:     51.161.161.54:42734 - "POST /ask HTTP/1.1" 500 Internal Server Error
ERROR:    Exception in ASGI application
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/uvicorn/protocols/http/h11_impl.py", line 428, in run_asgi
    result = await app(  # type: ignore[func-returns-value]
  File "/usr/local/lib/python3.9/site-packages/uvicorn/middleware/proxy_headers.py", line 78, in __call__
    return await self.app(scope, receive, send)
  File "/usr/local/lib/python3.9/site-packages/fastapi/applications.py", line 276, in __call__
    await super().__call__(scope, receive, send)
  File "/usr/local/lib/python3.9/site-packages/starlette/applications.py", line 122, in __call__
    await self.middleware_stack(scope, receive, send)
  File "/usr/local/lib/python3.9/site-packages/starlette/middleware/errors.py", line 184, in __call__
    raise exc
  File "/usr/local/lib/python3.9/site-packages/starlette/middleware/errors.py", line 162, in __call__
    await self.app(scope, receive, _send)
  File "/usr/local/lib/python3.9/site-packages/starlette/middleware/exceptions.py", line 79, in __call__
    raise exc
  File "/usr/local/lib/python3.9/site-packages/starlette/middleware/exceptions.py", line 68, in __call__
    await self.app(scope, receive, sender)
  File "/usr/local/lib/python3.9/site-packages/fastapi/middleware/asyncexitstack.py", line 21, in __call__
    raise e
  File "/usr/local/lib/python3.9/site-packages/fastapi/middleware/asyncexitstack.py", line 18, in __call__
    await self.app(scope, receive, send)
  File "/usr/local/lib/python3.9/site-packages/starlette/routing.py", line 718, in __call__
    await route.handle(scope, receive, send)
  File "/usr/local/lib/python3.9/site-packages/starlette/routing.py", line 276, in handle
    await self.app(scope, receive, send)
  File "/usr/local/lib/python3.9/site-packages/starlette/routing.py", line 66, in app
    response = await func(request)
  File "/usr/local/lib/python3.9/site-packages/fastapi/routing.py", line 237, in app
    raw_response = await run_endpoint_function(
  File "/usr/local/lib/python3.9/site-packages/fastapi/routing.py", line 163, in run_endpoint_function
    return await dependant.call(**values)
  File "/app/main.py", line 25, in ask
    chatbot = Chatbot(message.session_id)
  File "/app/Bard.py", line 99, in __init__
    self.SNlM0e = self.__get_snlm0e()
  File "/app/Bard.py", line 106, in __get_snlm0e
    SNlM0e = re.search(r"SNlM0e\":\"(.*?)\"", resp.text).group(1)
AttributeError: 'NoneType' object has no attribute 'group'

404 Not Found

After installation

uvicorn main:app --reload --port 8000
INFO: Will watch for changes in these directories: ['/opt/google-bard-api']
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO: Started reloader process [1293] using StatReload
INFO: Started server process [1301]
INFO: Waiting for application startup.
INFO: Application startup complete.

for example when executing a request:
curl -X POST "http://localhost:8000/ask" -H "accept: application/json" -H "Content-Type: application/json" -d '{"session_id":"ZQjhVqyLaEkcPex9Wkq-HuIVd1DWsk24PVQVKHxrXrE3EkeKssgO4E4sdrzgzFXKjf05jA.","message":"What is HS code for asus vivo notebook?"}'

INFO: 127.0.0.1:59330 - "POST /ask HTTP/1.1" 500 Internal Server Error
ERROR: Exception in ASGI application
Traceback (most recent call last):
File "/opt/google-bard-api/venv/lib/python3.9/site-packages/uvicorn/protocols/http/h11_impl.py", line 408, in run_asgi
result = await app( # type: ignore[func-returns-value]
File "/opt/google-bard-api/venv/lib/python3.9/site-packages/uvicorn/middleware/proxy_headers.py", line 84, in call
return await self.app(scope, receive, send)
File "/opt/google-bard-api/venv/lib/python3.9/site-packages/fastapi/applications.py", line 292, in call
await super().call(scope, receive, send)
File "/opt/google-bard-api/venv/lib/python3.9/site-packages/starlette/applications.py", line 122, in call
await self.middleware_stack(scope, receive, send)
File "/opt/google-bard-api/venv/lib/python3.9/site-packages/starlette/middleware/errors.py", line 184, in call
raise exc
File "/opt/google-bard-api/venv/lib/python3.9/site-packages/starlette/middleware/errors.py", line 162, in call
await self.app(scope, receive, _send)
File "/opt/google-bard-api/venv/lib/python3.9/site-packages/starlette/middleware/exceptions.py", line 79, in call
raise exc
File "/opt/google-bard-api/venv/lib/python3.9/site-packages/starlette/middleware/exceptions.py", line 68, in call
await self.app(scope, receive, sender)
File "/opt/google-bard-api/venv/lib/python3.9/site-packages/fastapi/middleware/asyncexitstack.py", line 20, in call
raise e
File "/opt/google-bard-api/venv/lib/python3.9/site-packages/fastapi/middleware/asyncexitstack.py", line 17, in call
await self.app(scope, receive, send)
File "/opt/google-bard-api/venv/lib/python3.9/site-packages/starlette/routing.py", line 718, in call
await route.handle(scope, receive, send)
File "/opt/google-bard-api/venv/lib/python3.9/site-packages/starlette/routing.py", line 276, in handle
await self.app(scope, receive, send)
File "/opt/google-bard-api/venv/lib/python3.9/site-packages/starlette/routing.py", line 66, in app
response = await func(request)
File "/opt/google-bard-api/venv/lib/python3.9/site-packages/fastapi/routing.py", line 273, in app
raw_response = await run_endpoint_function(
File "/opt/google-bard-api/venv/lib/python3.9/site-packages/fastapi/routing.py", line 190, in run_endpoint_function
return await dependant.call(**values)
File "/opt/google-bard-api/main.py", line 25, in ask
chatbot = Chatbot(message.session_id)
File "/opt/google-bard-api/Bard.py", line 99, in init
self.SNlM0e = self.__get_snlm0e()
File "/opt/google-bard-api/Bard.py", line 105, in __get_snlm0e
raise Exception("Could not get Google Bard")
Exception: Could not get Google Bard

Please tell me what could be the reason?

CORS Issue

I am running a React frontend an when I try to post I get:
Access to XMLHttpRequest at 'http://localhost:8000/ask' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
I am new (very) to python so any help would be appreciated!

[Question]: Faster API Implementation for Bing

I've recently noticed that the API for Bing in the EdgeGPT repository is performing significantly slower than the local server method. Is it possible to implement a similar approach for Bing, leveraging the fastapi library to improve its performance?

I believe such an implementation would yield significant speed improvements and overall enhance the experience for users.

Maintain

Is this repo being updated?
I've got some error.
raise Exception("Could not get Google Bard")
Exception: Could not get Google Bard
I already updated the bardapi to the latest version

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.