forked from seratch/ChatGPT-in-Slack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
258 lines (227 loc) · 8.16 KB
/
app.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import logging
import os
from openai.error import Timeout
from slack_bolt import App, Ack, BoltContext
from typing import Dict
from slack_sdk.web import WebClient
from slack_sdk.http_retry.builtin_handlers import RateLimitErrorRetryHandler
from internals import (
post_wip_message,
call_openai,
format_assistant_reply,
update_wip_message,
format_openai_message_content,
)
#
# Listener functions
#
def just_ack(ack: Ack):
ack()
DEFAULT_SYSTEM_TEXT = """
You are a bot in a slack chat room. You might receive messages from multiple people.
Each message has the author id prepended, like this: "<@U1234> message text".
"""
SYSTEM_TEXT = os.environ.get("SYSTEM_TEXT", DEFAULT_SYSTEM_TEXT)
TIMEOUT_ERROR_MESSAGE = "Sorry! It looks like OpenAI didn't respond within 25 seconds. Please try again later. :bow:"
def start_convo(
context: BoltContext, payload: dict, client: WebClient, logger: logging.Logger
):
wip_reply = None
try:
if payload.get("thread_ts") is not None:
return
openai_api_key = context.get("OPENAI_API_KEY")
if openai_api_key is None:
client.chat_postMessage(
channel=context.channel_id,
text="To use this app, please configure your OpenAI API key first",
)
return
messages = [
{"role": "system", "content": SYSTEM_TEXT},
{"role": "user", "content": format_openai_message_content(payload["text"])},
]
wip_reply = post_wip_message(
client=client,
channel=context.channel_id,
thread_ts=payload["ts"],
messages=messages,
user=context.user_id,
)
response = call_openai(
api_key=openai_api_key,
messages=messages,
user=context.user_id,
logger=logger,
)
assistant_reply: Dict[str, str] = response["choices"][0]["message"]
assistant_reply_text = format_assistant_reply(assistant_reply["content"])
messages.append(
{"content": assistant_reply_text, "role": assistant_reply["role"]}
)
update_wip_message(
client=client,
channel=context.channel_id,
ts=wip_reply["message"]["ts"],
text=assistant_reply_text,
messages=messages,
user=context.user_id,
)
except Timeout:
if wip_reply is not None:
client.chat_update(
channel=context.channel_id,
ts=wip_reply["message"]["ts"],
text=TIMEOUT_ERROR_MESSAGE,
)
except Exception as e:
text = f"Failed to start a conversation with ChatGPT: {e}"
logger.exception(text, e)
if wip_reply is not None:
client.chat_update(
channel=context.channel_id,
ts=wip_reply["message"]["ts"],
text=text,
)
def reply_if_necessary(
context: BoltContext,
payload: dict,
client: WebClient,
logger: logging.Logger,
):
wip_reply = None
try:
thread_ts = payload.get("thread_ts")
if thread_ts is None:
return
if (
payload.get("bot_id") is not None
and payload.get("bot_id") != context.bot_id
):
# Skip a new message by a different app
return
openai_api_key = context.get("OPENAI_API_KEY")
if openai_api_key is None:
return
replies = client.conversations_replies(
channel=context.channel_id,
ts=thread_ts,
include_all_metadata=True,
limit=1000,
)
messages = []
user_id = context.user_id
last_assistant_idx = -1
reply_messages = replies.get("messages", [])
indices_to_remove = []
for idx, reply in enumerate(reply_messages):
maybe_event_type = reply.get("metadata", {}).get("event_type")
if maybe_event_type == "chat-gpt-convo":
if context.bot_id != reply.get("bot_id"):
# Remove messages by a different app
indices_to_remove.append(idx)
continue
maybe_new_messages = (
reply.get("metadata", {}).get("event_payload", {}).get("messages")
)
if maybe_new_messages is not None:
if len(messages) == 0 or user_id is None:
new_user_id = (
reply.get("metadata", {})
.get("event_payload", {})
.get("user")
)
if new_user_id is not None:
user_id = new_user_id
messages = maybe_new_messages
last_assistant_idx = idx
if last_assistant_idx == -1:
return
filtered_reply_messages = []
for idx, reply in enumerate(reply_messages):
if idx not in indices_to_remove:
filtered_reply_messages.append(reply)
if len(filtered_reply_messages) == last_assistant_idx + 1:
return
start_idx = last_assistant_idx + 1
for reply in filtered_reply_messages[start_idx:]:
messages.append(
{
"content": format_openai_message_content(reply.get("text")),
"role": "user",
}
)
wip_reply = post_wip_message(
client=client,
channel=context.channel_id,
thread_ts=payload["ts"],
messages=messages,
user=user_id,
)
response = call_openai(
api_key=openai_api_key,
messages=messages,
user=user_id,
logger=logger,
)
latest_replies = client.conversations_replies(
channel=context.channel_id,
ts=thread_ts,
include_all_metadata=True,
limit=1000,
)
if latest_replies.get("messages", [])[-1]["ts"] != wip_reply["message"]["ts"]:
# Since a new reply will come soon, this app abandons this reply
client.chat_delete(
channel=context.channel_id,
ts=wip_reply["message"]["ts"],
)
return
assistant_reply: Dict[str, str] = response["choices"][0]["message"]
assistant_reply_text = format_assistant_reply(assistant_reply["content"])
messages.append(
{"content": assistant_reply_text, "role": assistant_reply["role"]}
)
update_wip_message(
client=client,
channel=context.channel_id,
ts=wip_reply["message"]["ts"],
text=assistant_reply_text,
messages=messages,
user=user_id,
)
except Timeout:
if wip_reply is not None:
client.chat_update(
channel=context.channel_id,
ts=wip_reply["message"]["ts"],
text=TIMEOUT_ERROR_MESSAGE,
)
except Exception as e:
text = f"Failed to reply in the conversation with ChatGPT: {e}"
logger.exception(text, e)
if wip_reply is not None:
client.chat_update(
channel=context.channel_id,
ts=wip_reply["message"]["ts"],
text=text,
)
def register_listeners(app: App):
app.event("app_mention")(ack=just_ack, lazy=[start_convo])
app.event("message")(ack=just_ack, lazy=[reply_if_necessary])
if __name__ == "__main__":
#
# Local development
#
from slack_bolt.adapter.socket_mode import SocketModeHandler
logging.basicConfig(level=logging.DEBUG)
client = WebClient(token=os.environ["SLACK_BOT_TOKEN"])
client.retry_handlers.append(RateLimitErrorRetryHandler(max_retry_count=2))
app = App(client=client, process_before_response=True)
register_listeners(app)
@app.middleware
def set_openai_api_key(context: BoltContext, next_):
context["OPENAI_API_KEY"] = os.environ["OPENAI_API_KEY"]
next_()
handler = SocketModeHandler(app, os.environ["SLACK_APP_TOKEN"])
handler.start()