This repository has been archived by the owner on Apr 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
bot.py
150 lines (115 loc) · 4.68 KB
/
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
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
import os
import re
import json
from slack_bolt import App
from slack_bolt.adapter.socket_mode import SocketModeHandler
import dotenv
import requests
from apis.openai import generate_embeddings, chat
SYSTEM_PROMPT = """
You are an assistant for our company. Our employees will ask you questions.
You will have access to some context,
and your goal is to provide the best answer possible given the context.
The context can come from one of three places:
Notion, Slack, or Linear. All context pieces will have:
- url: link to the context
- timestamp: date of last modification
- title: title of the context
- body (optional): body of the context
- platform: linear, slack, or notion. Shows you which one of the three it is
- member: name of the person that created the context
- attributes: these are platform-specific, but important for each case
## Notion
It has internal documents and notes.
## Slack
It has our internal communication and messages.
## Linear
Our issue tracking software.
Attributes:
- status: if something is done or not. Can be Done (finished),
In Review (finished but not deployed),
In progress (being done), Todo (planned) and Backlog (not planned or started)
- priority: how important the issue is
- labels: can be either Feature, Bug, Improvement, Design, or empty.
- asignee: person in charge of doing the issue. Can be empty
You can use these attributes to anser about the state of an issue if you need.
## Your instructions
- Give an answer as specific as possible, and try to
derive aconclusion from the context.
- Always reference back to the URLs of the contexts,
we should be able to find information from your reply.
- Use attributes, especially for Linear, to know the
status of tasks.
- Use timestamps to know the timeline of things.
- There will probably me more than one context related to a topic.
Use them all, giving more weight to the most recent ones.
- If there are multiple relevant contexts, use them all,
but give more weight to the most recent ones.
## Formatting
- Write using Markdown
- Display links like this: <http://www.example.com|This message is a link>
- Referencing messages depends on the platform:
- for Slack and Notion, provide it like this: "
<http://www.example.com|(source)>"
- for linear, always add the title as a link.
For example: <https://linear.app/linear/issue/XXX|Issue name>.
`
"""
app = App(token=os.environ["SLACK_BOT_TOKEN"])
def process_question(body, say):
channel = body["channel"]
event_ts = body["ts"]
question = body["text"].strip().replace("<@(.*?)>", "", re.IGNORECASE)
app.client.reactions_add(channel=channel, name="brain", timestamp=event_ts)
try:
embeddings = generate_embeddings(question)
# Get answers from qdrant
qdrant_data = {
"vector": embeddings,
"limit": 5,
"params": {"hnsw_ef": 128, "exact": False},
"with_vectors": False,
"with_payload": True,
}
qdrant_headers = {
"Content-Type": "application/json",
"api-key": os.environ["QDRANT_API_KEY"],
}
url = f"{os.environ['QDRANT_HOST']}/collections/{os.environ['QDRANT_COLLECTION']}/points/search"
qdrant_response = requests.post(url, json=qdrant_data, headers=qdrant_headers)
qdrant_result = qdrant_response.json()["result"]
if len(qdrant_result) == 0:
app.client.chat_postMessage(
channel=channel,
text=":x: There are no results for your question. Please try different one.",
thread_ts=event_ts,
)
return
# Format qdrant_results
context_data = [entry["payload"] for entry in qdrant_result]
response = chat(
SYSTEM_PROMPT, f"Question:\n{question}\n Context:\n{context_data}"
)
# Remove reaction
app.client.reactions_remove(channel=channel, name="brain", timestamp=event_ts)
app.client.chat_postMessage(channel=channel, text=response, thread_ts=event_ts)
except Exception as err:
print(f"Error: {err}")
app.client.chat_postMessage(
channel=channel,
text=":x: An error occurred while fetching the answer. Please try again later.",
thread_ts=event_ts,
)
@app.event("message")
def handle_message(body, say):
from pprint import pprint as pp
if body["event"]["channel_type"] != "im":
return
process_question(body["event"], say)
@app.event("app_mention")
def handle_app_mention(body, say):
process_question(body, say)
if __name__ == "__main__":
handler = SocketModeHandler(app, os.environ["SLACK_APP_TOKEN"])
handler.start()
print("⚡️ Crowd assistant app is running!")