Skip to content

Commit

Permalink
use revchatgpt and .env
Browse files Browse the repository at this point in the history
  • Loading branch information
n3d1117 committed Dec 5, 2022
1 parent 7ffffdc commit e4ba2c9
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 146 deletions.
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
OPENAI_EMAIL="<YOUR_OPENAI_EMAIL>"
OPENAI_PASSWORD="YOUR_OPENAI_PASSWORD"
TELEGRAM_BOT_TOKEN="YOUR_TELEGRAM_BOT_TOKEN"
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
/__pycache__
/.idea
*.json
.env
2 changes: 2 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ name = "pypi"
[packages]
requests = "*"
python-telegram-bot = "==20.0a6"
revchatgpt = "==0.0.23"
python-dotenv = "*"

[dev-packages]

Expand Down
46 changes: 45 additions & 1 deletion Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

98 changes: 0 additions & 98 deletions chat_gpt3_bot.py

This file was deleted.

4 changes: 0 additions & 4 deletions config.json.example

This file was deleted.

21 changes: 14 additions & 7 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
import json
import logging
import os

from chat_gpt3_bot import ChatGPT3Bot
from dotenv import load_dotenv
from revChatGPT.revChatGPT import Chatbot as ChatGPT3Bot
from telegram_bot import ChatGPT3TelegramBot


def main():
load_dotenv()

logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO
)

with open("config.json", "r") as f:
config = json.load(f)
chatgpt_config = {
'email': os.environ['OPENAI_EMAIL'],
'password': os.environ['OPENAI_PASSWORD'],
}
telegram_config = {
'telegram_bot_token': os.environ['TELEGRAM_BOT_TOKEN']
}

gpt3_bot = ChatGPT3Bot(config=config)
telegram_bot = ChatGPT3TelegramBot(config=config, gpt3_bot=gpt3_bot)
gpt3_bot = ChatGPT3Bot(config=chatgpt_config)
telegram_bot = ChatGPT3TelegramBot(config=telegram_config, gpt3_bot=gpt3_bot)
telegram_bot.run()


Expand Down
32 changes: 0 additions & 32 deletions openai_extract_session_token.py

This file was deleted.

17 changes: 14 additions & 3 deletions telegram_bot.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import logging

import telegram.constants
Expand All @@ -22,21 +23,31 @@ async def start(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
# Reset the conversation
async def reset(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
logging.info('Resetting the conversation...')
self.gpt3_bot.reset()
self.gpt3_bot.reset_chat()
await context.bot.send_message(chat_id=update.effective_chat.id, text="Done!")

# React to messages
async def prompt(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
logging.info('New message received')
await context.bot.send_chat_action(chat_id=update.effective_chat.id, action=telegram.constants.ChatAction.TYPING)
response = self.gpt3_bot.get_chat_response(update.message.text)
response = self.get_chatgpt_response(update.message.text)
await context.bot.send_message(
chat_id=update.effective_chat.id,
reply_to_message_id=update.message.message_id,
text=response["message"],
parse_mode=telegram.constants.ParseMode.MARKDOWN
)
logging.info('Sent response')

def get_chatgpt_response(self, message, retry=False) -> dict:
response = self.gpt3_bot.get_chat_response(message)
if isinstance(response, dict):
return response
else:
if not retry:
self.gpt3_bot.refresh_session()
return self.get_chatgpt_response(message, retry=True)
else:
return {"message": "I'm having some trouble talking to you, please try again later."}

def run(self):
application = ApplicationBuilder().token(self.config['telegram_bot_token']).build()
Expand Down

0 comments on commit e4ba2c9

Please sign in to comment.