feat: Make poe bot run as an app instead of server. (#550)

This commit is contained in:
Taranjeet Singh
2023-09-04 20:29:41 -07:00
committed by GitHub
parent 60d5daaaf5
commit d4e6462e4d
4 changed files with 59 additions and 52 deletions

View File

@@ -0,0 +1,4 @@
from embedchain.bots.poe import PoeBot
from embedchain.bots.whatsapp import WhatsAppBot
# TODO: fix discord import
# from embedchain.bots.discord import DiscordBot

View File

@@ -11,8 +11,27 @@ from embedchain.helper_classes.json_serializable import register_deserializable
from .base import BaseBot
def start_command():
parser = argparse.ArgumentParser(description="EmbedChain PoeBot command line interface")
# parser.add_argument("--host", default="0.0.0.0", help="Host IP to bind")
parser.add_argument("--port", default=8080, type=int, help="Port to bind")
parser.add_argument("--api-key", type=str, help="Poe API key")
# parser.add_argument(
# "--history-length",
# default=5,
# type=int,
# help="Set the max size of the chat history. Multiplies cost, but improves conversation awareness.",
# )
args = parser.parse_args()
# FIXME: Arguments are automatically loaded by Poebot's ArgumentParser which causes it to fail.
# the port argument here is also just for show, it actually works because poe has the same argument.
run(PoeBot(), api_key=args.api_key or os.environ.get("POE_API_KEY"))
@register_deserializable
class EcPoeBot(BaseBot, PoeBot):
class PoeBot(BaseBot, PoeBot):
def __init__(self):
self.history_length = 5
super().__init__()
@@ -38,15 +57,15 @@ class EcPoeBot(BaseBot, PoeBot):
response = self.ask_bot(message, history)
return response
def add_data(self, message):
data = message.split(" ")[-1]
try:
self.add(data)
response = f"Added data from: {data}"
except Exception:
logging.exception(f"Failed to add data {data}.")
response = "Some error occurred while adding data."
return response
# def add_data(self, message):
# data = message.split(" ")[-1]
# try:
# self.add(data)
# response = f"Added data from: {data}"
# except Exception:
# logging.exception(f"Failed to add data {data}.")
# response = "Some error occurred while adding data."
# return response
def ask_bot(self, message, history: List[str]):
try:
@@ -57,24 +76,8 @@ class EcPoeBot(BaseBot, PoeBot):
response = "An error occurred. Please try again!"
return response
def start_command():
parser = argparse.ArgumentParser(description="EmbedChain PoeBot command line interface")
# parser.add_argument("--host", default="0.0.0.0", help="Host IP to bind")
parser.add_argument("--port", default=8080, type=int, help="Port to bind")
parser.add_argument("--api-key", type=str, help="Poe API key")
# parser.add_argument(
# "--history-length",
# default=5,
# type=int,
# help="Set the max size of the chat history. Multiplies cost, but improves conversation awareness.",
# )
args = parser.parse_args()
# FIXME: Arguments are automatically loaded by Poebot's ArgumentParser which causes it to fail.
# the port argument here is also just for show, it actually works because poe has the same argument.
run(EcPoeBot(), api_key=args.api_key or os.environ.get("POE_API_KEY"))
def start(self):
start_command()
if __name__ == "__main__":

View File

@@ -3,9 +3,6 @@ import logging
import signal
import sys
from flask import Flask, request
from twilio.twiml.messaging_response import MessagingResponse
from embedchain.helper_classes.json_serializable import register_deserializable
from .base import BaseBot
@@ -14,6 +11,8 @@ from .base import BaseBot
@register_deserializable
class WhatsAppBot(BaseBot):
def __init__(self):
from flask import Flask, request
from twilio.twiml.messaging_response import MessagingResponse
super().__init__()
def handle_message(self, message):