-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompass.js
166 lines (140 loc) · 4.82 KB
/
compass.js
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
/**
* Compass is the Assistant all users interact with in threads.
*
* @see https://tools.slack.dev/bolt-js/reference#agents--assistants
*/
const { Assistant } = require("@slack/bolt");
const blockKitBuilder = require("./classes/utilities/BlockKitBuilder");
const intentionHandler = require("./classes/utilities/IntentionHandler");
const openai = require("./classes/apis/OpenAI");
const pinecone = require("./classes/apis/Pinecone");
const {
COMPASS_BRIEFING,
FIRST_SUGGESTED_PROMPTS,
GENERATE_TITLE_PROMPT,
SUMMARIZE_CHAT_PROMPT,
} = require("./prompts");
/**
* Initiate the Assistant.
*/
const compass = new Assistant({
/**
* The `threadStarted` event is triggered when a new thread is created.
*/
threadStarted: async ({ say, setSuggestedPrompts }) => {
await say("Ahoy, waar kan ik je mee helpen?");
await setSuggestedPrompts({
prompts: FIRST_SUGGESTED_PROMPTS,
title: "Hier wat suggesties:",
});
},
/**
* The `userMessage` event is triggered when a user sends a message in a thread.
*/
userMessage: async ({
client,
logger,
message,
say,
setStatus,
setTitle,
getThreadContext,
}) => {
try {
const { text, channel, thread_ts } = message;
const userMessage = { role: "user", content: text };
let threadContext;
let prompts;
await setStatus("leest je vraag...");
const intention = await intentionHandler.analyseIntent(text);
logger.info(
`Received message with intention: ${JSON.stringify(intention)}`
);
logger.info("Preparing channel history for OpenAI completion.");
if (intention.intent === "summarize_chat") {
await setStatus("is het actieve kanaal aan het lezen...");
threadContext = await getThreadContext();
try {
channelHistory = await client.conversations.history({
channel: threadContext.channel_id,
limit: intention.limit || 50,
});
} catch (error) {
// If the bot is not in the channel, join it and try again.
if (e.data.error === "not_in_channel") {
await client.conversations.join({
channel: threadContext.channel_id,
});
channelHistory = await client.conversations.history({
channel: threadContext.channel_id,
limit: intention.limit || 50,
});
} else {
console.error(`Error fetching channel history: ${error}`);
throw error;
}
}
let prompt = `${SUMMARIZE_CHAT_PROMPT} <#${threadContext.channel_id}>:`;
for (const m of channelHistory.messages.reverse()) {
if (m.user) prompt += `\n<@${m.user}> says: ${m.text}`;
}
prompts = [{ role: "user", content: prompt }];
} else {
channelHistory = await client.conversations.replies({
channel,
ts: thread_ts,
oldest: thread_ts,
});
prompts = channelHistory.messages.map((message) => ({
role: message.bot_id ? "assistant" : "user",
content: message.text,
}));
prompts.push(userMessage);
// const embeddingResponse = await openai.createEmbedding({
// text: text,
// });
// const index = await pinecone.getIndex("kompas-index");
// const searchResults = await index.query({
// vector: embeddingResponse,
// topK: 4,
// includeMetadata: true,
// });
// const pineconeResults = searchResults.matches;
// if (pineconeResults.length > 0) {
// context = "Hier is wat ik heb gevonden:\n";
// pineconeResults.forEach((result, index) => {
// context += `*Resultaat ${index + 1}:*\nTitle: ${
// result.metadata.title
// }\nContent: ${result.metadata.content}\n\n`;
// });
// }
// prompts.push({ role: "system", content: context });
}
const messages = [
{ role: "system", content: COMPASS_BRIEFING },
...prompts,
];
await setStatus("is aan het nadenken...");
const completion = await openai.createCompletion({ messages });
logger.info("Sending back OpenAI completion.");
await setStatus("is aan het typen...");
await say({
blocks: [blockKitBuilder.addSection({ text: completion })],
text: completion,
});
const titlePrompt = [
{ role: "system", content: `${GENERATE_TITLE_PROMPT} ${completion}.` },
];
const generatedTitle = await openai.createCompletion({
messages: titlePrompt,
});
return await setTitle(generatedTitle);
} catch (e) {
logger.error(`Error processing message: ${e}`);
await say("Er is iets misgegaan bij het verwerken van je verzoek.");
}
},
});
module.exports = {
compass,
};