feat: Make poe bot run as an app instead of server. (#550)
This commit is contained in:
@@ -7,42 +7,43 @@ title: '🔮 Poe Bot'
|
|||||||
1. Install embedchain python package:
|
1. Install embedchain python package:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
pip install embedchain[poe]
|
pip install "embedchain[poe]"
|
||||||
```
|
```
|
||||||
|
|
||||||
2. Create a free account on [Poe](https://www.poe.com?utm_source=embedchain).
|
2. Create a free account on [Poe](https://www.poe.com?utm_source=embedchain).
|
||||||
3. Click "Create Bot" button on top left
|
3. Click "Create Bot" button on top left.
|
||||||
4. Give it a handle and an optional description.
|
4. Give it a handle and an optional description.
|
||||||
5. Select `Use API`.
|
5. Select `Use API`.
|
||||||
6. Under `API URL` enter your server or ngrok address. You can use your machine's public IP or DNS. Otherwise, employ a proxy server like [ngrok](https://ngrok.com/) to make your local bot accessible.
|
6. Under `API URL` enter your server or ngrok address. You can use your machine's public IP or DNS. Otherwise, employ a proxy server like [ngrok](https://ngrok.com/) to make your local bot accessible.
|
||||||
7. Copy your api key and paste it in `.env` as `POE_API_KEY`.
|
7. Copy your api key and paste it in `.env` as `POE_API_KEY`.
|
||||||
8. Start the bot.
|
8. Now create your bot using the following code snippet
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
python -m embedchain.bots.poe
|
from embedchain import PoeBot
|
||||||
|
|
||||||
|
poe_bot = PoeBot()
|
||||||
|
|
||||||
|
# add as many data sources as you want
|
||||||
|
poe_bot.add("https://en.wikipedia.org/wiki/Adam_D%27Angelo")
|
||||||
|
poe_bot.add("https://www.youtube.com/watch?v=pJQVAqmKua8")
|
||||||
|
|
||||||
|
# start the bot
|
||||||
|
# this start the poe bot server on port 8080 by default
|
||||||
|
poe_bot.start()
|
||||||
```
|
```
|
||||||
|
|
||||||
If you want to run the bot on another port, you can pass `--port option` like
|
9. You can refer the [Supported Data formats](https://docs.embedchain.ai/advanced/data_types) section to refer the supported data types in embedchain.
|
||||||
|
|
||||||
```bash
|
10. Click `Run check` to make sure your machine can be reached.
|
||||||
python -m embedchain.bots.poe --port 5000
|
11. Make sure your bot is private if that's what you want.
|
||||||
```
|
12. Click `Create bot` at the bottom to finally create the bot
|
||||||
|
13. Now you bot is created.
|
||||||
9. Click `Run check` to make sure your machine can be reached.
|
|
||||||
10. Make sure your bot is private if that's what you want.
|
|
||||||
11. Click `Create bot` at the bottom to finally create the bot
|
|
||||||
12. Now you bot is created.
|
|
||||||
|
|
||||||
### 💬 How to use
|
### 💬 How to use
|
||||||
|
|
||||||
- To include data sources, use this command:
|
- To ask the bot questions, just type your query in the Poe interface:
|
||||||
```text
|
|
||||||
/add <url_or_text>
|
|
||||||
```
|
|
||||||
|
|
||||||
- You can refer the [Supported Data formats](https://docs.embedchain.ai/advanced/data_types) section to refer the supported data types in embedchain.
|
|
||||||
|
|
||||||
- To ask the bot questions, just type your query:
|
|
||||||
```text
|
```text
|
||||||
<your-question-here>
|
<your-question-here>
|
||||||
```
|
```
|
||||||
|
|
||||||
|
- If you wish to add more data source to the bot, simply update your script and add as many `.add` as you like. You need to restart the server.
|
||||||
|
|||||||
@@ -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
|
||||||
@@ -11,8 +11,27 @@ from embedchain.helper_classes.json_serializable import register_deserializable
|
|||||||
from .base import BaseBot
|
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
|
@register_deserializable
|
||||||
class EcPoeBot(BaseBot, PoeBot):
|
class PoeBot(BaseBot, PoeBot):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.history_length = 5
|
self.history_length = 5
|
||||||
super().__init__()
|
super().__init__()
|
||||||
@@ -38,15 +57,15 @@ class EcPoeBot(BaseBot, PoeBot):
|
|||||||
response = self.ask_bot(message, history)
|
response = self.ask_bot(message, history)
|
||||||
return response
|
return response
|
||||||
|
|
||||||
def add_data(self, message):
|
# def add_data(self, message):
|
||||||
data = message.split(" ")[-1]
|
# data = message.split(" ")[-1]
|
||||||
try:
|
# try:
|
||||||
self.add(data)
|
# self.add(data)
|
||||||
response = f"Added data from: {data}"
|
# response = f"Added data from: {data}"
|
||||||
except Exception:
|
# except Exception:
|
||||||
logging.exception(f"Failed to add data {data}.")
|
# logging.exception(f"Failed to add data {data}.")
|
||||||
response = "Some error occurred while adding data."
|
# response = "Some error occurred while adding data."
|
||||||
return response
|
# return response
|
||||||
|
|
||||||
def ask_bot(self, message, history: List[str]):
|
def ask_bot(self, message, history: List[str]):
|
||||||
try:
|
try:
|
||||||
@@ -57,24 +76,8 @@ class EcPoeBot(BaseBot, PoeBot):
|
|||||||
response = "An error occurred. Please try again!"
|
response = "An error occurred. Please try again!"
|
||||||
return response
|
return response
|
||||||
|
|
||||||
|
def start(self):
|
||||||
def start_command():
|
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"))
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
@@ -3,9 +3,6 @@ import logging
|
|||||||
import signal
|
import signal
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
from flask import Flask, request
|
|
||||||
from twilio.twiml.messaging_response import MessagingResponse
|
|
||||||
|
|
||||||
from embedchain.helper_classes.json_serializable import register_deserializable
|
from embedchain.helper_classes.json_serializable import register_deserializable
|
||||||
|
|
||||||
from .base import BaseBot
|
from .base import BaseBot
|
||||||
@@ -14,6 +11,8 @@ from .base import BaseBot
|
|||||||
@register_deserializable
|
@register_deserializable
|
||||||
class WhatsAppBot(BaseBot):
|
class WhatsAppBot(BaseBot):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
from flask import Flask, request
|
||||||
|
from twilio.twiml.messaging_response import MessagingResponse
|
||||||
super().__init__()
|
super().__init__()
|
||||||
|
|
||||||
def handle_message(self, message):
|
def handle_message(self, message):
|
||||||
|
|||||||
Reference in New Issue
Block a user