Skip to content

Commit

Permalink
Adding google search baby! Let's got open source!
Browse files Browse the repository at this point in the history
  • Loading branch information
altryne committed Dec 4, 2022
1 parent 4c63e9b commit 2c9d3dd
Show file tree
Hide file tree
Showing 6 changed files with 534 additions and 10 deletions.
4 changes: 3 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
TELEGRAM_API_KEY=
TELEGRAM_USER_ID= #Use this with your user ID to restrict usage only to your account

STABILITY_API_KEY= #use this if you want the bot to draw things with stability AI as well
STABILITY_API_KEY= #use this if you want the bot to draw things with stability AI as well

SERP_API_KEY= #add this from serpapi if you want to enable the google search feature
20 changes: 15 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# ChatGPT Telegram Bot
# ChatGPT Telegram Bot - @altryne
### see [announcement thread on twitter](https://twitter.com/altryne/status/1598822052760195072)

- Uses your local browser using Playwright to run chatGPT in chromium
Expand All @@ -7,12 +7,21 @@
- has a `/draw` command in telegram to draw pictures using stable diffusion!
- more stuff coming after I rest

## Features
- [ ] Chat with chatGPT from your Telegram! On the go! 📱
- [ ] `/draw` pictures using stable diffusion! `0.0.2`
- Just add STABILITY_API_KEY in .env
- [ ] `/browse` give chatGPT google access!! `0.0.3`
- Just add SERP_API_KEY in .env



![CleanShot 2022-12-02 at 16 08 27](https://user-images.githubusercontent.com/463317/205404516-56ea908e-dd31-4c53-acb7-15f9f6ed379f.gif)


# How to install

* Make sure that python and virual environment is installed.
* Make sure that python and miniconda are installed.

* Create a conda environment with `conda env create -f environment.yml`

Expand All @@ -21,8 +30,9 @@
playwright install
```

You need to setup your telegram bot token [how to](https://core.telegram.org/bots/tutorial#obtain-your-bot-token) and [user id](https://bigone.zendesk.com/hc/en-us/articles/360008014894-How-to-get-the-Telegram-user-ID-) in `.env` file.
Edit the .env.example file and rename it to .env and place your values in there.
- You need to setup your telegram bot token [how to](https://core.telegram.org/bots/tutorial#obtain-your-bot-token) and [user id](https://bigone.zendesk.com/hc/en-us/articles/360008014894-How-to-get-the-Telegram-user-ID-) in `.env` file.

- Edit the .env.example file and rename it to .env and place your values in there.


* Now run the server
Expand All @@ -34,5 +44,5 @@ python server.py
Then find your bot in telegram (you should have already created it with @botfather) and start chatting.

# Credit

* Creator [@Altryne](https://twitter.com/altryne/status/1598902799625961472) on Twitter
* Got started with this using [Daniel Gross's whatsapp gpt](https://github.com/danielgross/whatsapp-gpt) package.
3 changes: 2 additions & 1 deletion environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ dependencies:
- playwright
- python-dotenv
- python-telegram-bot==20.0a6
- requests
- requests
- google-search-results
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
playwright~=1.28.0
python-dotenv~=0.21.0
requests~=2.28.1
requests~=2.28.1
google-search-results
44 changes: 42 additions & 2 deletions server.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Make some requests to OpenAI's chatbot"""
import json
import time
import os

Expand All @@ -8,6 +9,8 @@

import dotenv
import nest_asyncio

from utils.googleSearch import googleSearch
from utils.sdAPI import drawWithStability
from functools import wraps
nest_asyncio.apply()
Expand Down Expand Up @@ -162,6 +165,42 @@ async def respond_with_image(update, response):
parse_mode=telegram.constants.ParseMode.MARKDOWN_V2)


@auth(USER_ID)
async def browse(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
message = update.message.text.replace('/browse','')
await application.bot.send_chat_action(update.effective_chat.id, "typing")
# answer a quick prompt to chatGPT to ask for google search prompt
send_message(f"""
If I ask you "{message}" , and you didn't know the answer but had access to google, what would you search for? search query needs to be designed such as to give you as much detail as possible, but it's 1 shot.
Answer with
query: x
only, where x is the google search string that would let you help me answer the question
I want you to only reply with the output inside and nothing else. Do no write explanations.
""")
await check_loading(update)
response = get_last_message()
# extract prompt from this format [prompt: x]
response.replace('query: ', '')
print(f'Clean response from chatGPT {response}')
results = googleSearch(message)
prompt = f"""
Pretend I was able to run a google search for "{message}" instead of you and I got the following results:
\"\"\"
{results}
\"\"\"
Provide a summary of the new facts in a code block, in markdown format
Then in another code block, answer the question {message} with the new facts you just learned
"""
send_message(prompt)
await check_loading(update)
response = get_last_message()
if "\[prompt:" in response:
await respond_with_image(update, response, parse_mode=telegram.constants.ParseMode.MARKDOWN_V2)
else:
await update.message.reply_text(response, parse_mode=telegram.constants.ParseMode.MARKDOWN_V2)

@auth(USER_ID)
async def echo(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
"""Echo the user message."""
Expand All @@ -175,13 +214,13 @@ async def echo(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
await update.message.reply_text(response, parse_mode=telegram.constants.ParseMode.MARKDOWN_V2)

async def check_loading(update):
# with a timeout of 45 seconds, created a while loop that checks if loading is done
# with a timeout of 90 seconds, created a while loop that checks if loading is done
loading = PAGE.query_selector_all("button[class^='PromptTextarea__PositionSubmit']>.text-2xl")
#keep checking len(loading) until it's empty or 45 seconds have passed
await application.bot.send_chat_action(update.effective_chat.id, "typing")
start_time = time.time()
while len(loading) > 0:
if time.time() - start_time > 45:
if time.time() - start_time > 90:
break
time.sleep(0.5)
loading = PAGE.query_selector_all("button[class^='PromptTextarea__PositionSubmit']>.text-2xl")
Expand All @@ -201,6 +240,7 @@ def start_browser():
application.add_handler(CommandHandler("reload", reload))
application.add_handler(CommandHandler("help", help_command))
application.add_handler(CommandHandler("draw", draw))
application.add_handler(CommandHandler("browse", browse))

# on non command i.e message - echo the message on Telegram
application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, echo))
Expand Down
Loading

0 comments on commit 2c9d3dd

Please sign in to comment.