-
Notifications
You must be signed in to change notification settings - Fork 18
/
slack_gpt_bot.py
74 lines (62 loc) · 2.48 KB
/
slack_gpt_bot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import openai
import os
from dotenv import load_dotenv
from slack_bolt import App
from slack_bolt.adapter.socket_mode import SocketModeHandler
load_dotenv()
SLACK_BOT_TOKEN = os.environ["SLACK_BOT_TOKEN"]
SLACK_APP_TOKEN = os.environ["SLACK_APP_TOKEN"]
OPENAI_API_KEY = os.environ["OPENAI_API_KEY"]
from utils import (N_CHUNKS_TO_CONCAT_BEFORE_UPDATING, OPENAI_API_KEY,
SLACK_APP_TOKEN, SLACK_BOT_TOKEN, WAIT_MESSAGE,
num_tokens_from_messages, process_conversation_history,
update_chat)
app = App(token=SLACK_BOT_TOKEN)
openai.api_key = OPENAI_API_KEY
def get_conversation_history(channel_id, thread_ts):
return app.client.conversations_replies(
channel=channel_id,
ts=thread_ts,
inclusive=True
)
@app.event("app_mention")
def command_handler(body, context):
try:
channel_id = body['event']['channel']
thread_ts = body['event'].get('thread_ts', body['event']['ts'])
bot_user_id = context['bot_user_id']
slack_resp = app.client.chat_postMessage(
channel=channel_id,
thread_ts=thread_ts,
text=WAIT_MESSAGE
)
reply_message_ts = slack_resp['message']['ts']
conversation_history = get_conversation_history(channel_id, thread_ts)
messages = process_conversation_history(conversation_history, bot_user_id)
num_tokens = num_tokens_from_messages(messages)
print(f"Number of tokens: {num_tokens}")
openai_response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=messages,
stream=True
)
response_text = ""
ii = 0
for chunk in openai_response:
if chunk.choices[0].delta.get('content'):
ii = ii + 1
response_text += chunk.choices[0].delta.content
if ii > N_CHUNKS_TO_CONCAT_BEFORE_UPDATING:
update_chat(app, channel_id, reply_message_ts, response_text)
ii = 0
elif chunk.choices[0].finish_reason == 'stop':
update_chat(app, channel_id, reply_message_ts, response_text)
except Exception as e:
print(f"Error: {e}")
app.client.chat_postMessage(
channel=channel_id,
thread_ts=thread_ts,
text=f"I can't provide a response. Encountered an error:\n`\n{e}\n`")
if __name__ == "__main__":
handler = SocketModeHandler(app, SLACK_APP_TOKEN)
handler.start()