Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge in latest changes #1

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# JetBrains IDE
.idea/
*.iml
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ FROM python
WORKDIR /app
RUN curl https://sh.rustup.rs -sSf | bash -s -- -y
ENV PATH="/root/.cargo/bin:${PATH}"
COPY app.py /app
COPY requirements.txt /app

RUN pip install -r requirements.txt

COPY app.py /app

ENTRYPOINT ["python"]
CMD ["app.py"]
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ The `revChatGPT` package is used to handle authentication and communication with

## Usage

To use the bot, you will need to install the dependencies and set the `CHATGPT_EMAIL` and `CHATGPT_PASSWORD` environment variables with your OpenAI credentials. You also need to set `SLACK_SIGNING_SECRET` and `SLACK_BOT_TOKEN` environment variables after creating the Slack App. You can then run the `app.py` script to start the bot.
To use the bot, you will need to install the dependencies and set the `OPENAI_API_KEY` environment variable that you can get from https://platform.openai.com/account/api-keys. You also need to set `SLACK_SIGNING_SECRET` and `SLACK_BOT_TOKEN` environment variables after creating the Slack App. You can then run the `app.py` script to start the bot.

```python
```bash
pip install -r requirements.txt
export SLACK_SIGNING_SECRET=slack_signing_secret
export SLACK_BOT_TOKEN=slack_bot_token
Expand All @@ -30,7 +30,7 @@ Once the bot is running, you can mention it in a Slack channel to send it a mess

## ChatGPT Configuration

The `ChatGPTConfig` dictionary at the top of the `app.py` script contains the configuration for the `revChatGPT` package. It specifies the email and password for the OpenAI account that the bot will use to authenticate with the GPT-3 API. These values are read from the `CHATGPT_EMAIL` and `CHATGPT_PASSWORD` environment variables.
The `ChatGPTConfig` dictionary at the top of the `app.py` script contains the configuration for the `revChatGPT` package. It specifies the API token for the OpenAI account that the bot will use to authenticate with the GPT-3 API. The value is read from the `OPENAI_API_KEY` environment variable.

The `conversation_id` parameter in the `Chatbot` constructor is used to specify the ID of the conversation that the bot should use. If this parameter is not provided, the `revChatGPT` package will generate a new conversation ID for each message that the bot receives.

Expand All @@ -45,13 +45,15 @@ For this bot, the required Scopes in `OAuth & Permissions` are:
* chat:write
* im:write

For direct message required & config :
* `Enable bot direct messages` go to : Features -> App Home -> (Enable) Messages Tab
* `OAuth & Permissions` im:history and message:im

In the `Event Subscriptions`, you need to subscribe to the `app_mention` event.

## Limitations

The bot uses a pre-trained GPT-3 model, which means that its responses are limited to the information that is contained in the model. It may not be able to respond accurately to messages that are outside of the scope of the pre-trained model.

Additionally, the bot is only designed to handle `app_mention` events, so it will not respond to other types of messages. This can be easily extended by adding more event handlers to the `app.py` script.
The bot uses a pre-trained GPT-3 model, which means that its responses are limited to the information that is contained in the model. It may not be able to respond accurately to messages that are outside the scope of the pre-trained model.

## Notes

Expand Down
57 changes: 39 additions & 18 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,47 +1,68 @@
import os
import re
import sys
import time
from threading import Thread

from revChatGPT.V3 import Chatbot
from slack_bolt import App

ChatGPTConfig = {
"api_key": os.getenv("OPENAI_API_KEY"),
}
"api_key": os.getenv("OPENAI_API_KEY"),
}

if os.getenv("OPENAI_ENGINE"):
ChatGPTConfig["engine"] = os.getenv("OPENAI_ENGINE")

app = App()
chatbot = Chatbot(**ChatGPTConfig)

# Listen for an event from the Events API
@app.event("app_mention")
def event_test(event, say):
prompt = re.sub('(?:\s)<@[^, ]*|(?:^)<@[^, ]*', '', event['text'])

def handle_event(event, say, is_mention):
prompt = re.sub("\\s<@[^, ]*|^<@[^, ]*", "", event["text"])

# Each thread should be a separate conversation
convo_id = event.get("thread_ts") or event.get("ts") or ""

try:
response = chatbot.ask(prompt)
user = event['user']
user = f"<@{user}> you asked:"
asked = ['>',prompt]
asked = "".join(asked)
send = [user,asked,response]
send = "\n".join(send)
response = chatbot.ask(prompt, convo_id=convo_id)
user = event["user"]

if is_mention:
send = f"<@{user}> {response}"
else:
send = response
except Exception as e:
print(e)
send = "We're experiencing exceptionally high demand. Please, try again."
print(e, file=sys.stderr)
send = "We are experiencing exceptionally high demand. Please, try again."

# Get the `ts` value of the original message
original_message_ts = event["ts"]
if is_mention:
# Get the `ts` value of the original message
original_message_ts = event["ts"]
else:
original_message_ts = None

# Use the `app.event` method to send a reply to the message thread
# Use the `app.event` method to send a message
say(send, thread_ts=original_message_ts)


@app.event("app_mention")
def handle_mention(event, say):
handle_event(event, say, is_mention=True)


@app.event("message")
def handle_message(event, say):
handle_event(event, say, is_mention=False)


def chatgpt_refresh():
while True:
time.sleep(60)


if __name__ == "__main__":
print("Bot Started!", file=sys.stderr)
thread = Thread(target=chatgpt_refresh)
thread.start()
app.start(4000) # POST http://localhost:4000/slack/events
4 changes: 2 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ version: "3.9"
services:
chatgptslackbot:
build:
context: ./
context: .
image: chatgptslackbot
restart: always
container_name: chatgptslackbot
env_file:
- ./variables.env
ports:
- 4000:4000
- "4000:4000"
5 changes: 3 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
slack_bolt
revChatGPT>=3.0.0
slack_bolt==1.18.0
revChatGPT==5.0.0
flask==2.3.2
3 changes: 1 addition & 2 deletions variables.env
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
SLACK_SIGNING_SECRET=
SLACK_BOT_TOKEN=
CHATGPT_EMAIL=
CHATGPT_PASSWORD=
OPENAI_API_KEY=