-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
124 lines (107 loc) · 3.22 KB
/
app.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
const { App } = require("@slack/bolt");
const { Configuration, OpenAIApi } = require("openai");
require("dotenv").config();
const app = new App({
signingSecret: process.env.SLACK_SIGNING_SECRET,
token: process.env.SLACK_BOT_TOKEN,
socketMode: true,
appToken: process.env.SLACK_APP_TOKEN,
port: process.env.PORT || 3000,
});
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
temperature: 0.5,
max_tokens: 500,
});
const openai = new OpenAIApi(configuration);
// Object to store conversation threads
const conversations = {};
const systemInitializationPrompt = {
role: "system",
content:
"You are Q, a cosmic entity from Star Trek. You will speak as Q and answer questions carefully step by step. You are witty, clever, and immensely intelligent",
};
// Function to generate AI response
async function generateResponse(message) {
const completion = await openai.createChatCompletion({
model: "gpt-3.5-turbo",
messages: conversations[message.thread_ts ? message.thread_ts : message.ts], // If message is in a thread, use thread_ts, otherwise use ts
});
let response;
if (!completion.data.choices || !completion.data.choices.length) {
console.log(
"Error generating response. No choices found in completion data."
);
response =
"My apologies, there was an error getting a response from the AI.";
} else {
response = completion.data.choices[0].message.content.trim();
// use the response
}
return response;
}
// Listen for messages containing bot mention
app.event("app_mention", async ({ event, say }) => {
console.log("Mention event", event);
// Check if conversation thread exists in conversations object
if (event.thread_ts in conversations) {
const messages = conversations[event.thread_ts];
messages.push({
role: "user",
content: event.text,
});
const response = await generateResponse(event);
messages.push({
role: "assistant",
content: response,
});
// Send response to user
say({
thread_ts: event.thread_ts,
text: response,
});
} else {
// If conversation thread does not exist, start new conversation
conversations[event.ts] = [
systemInitializationPrompt,
{ role: "user", content: event.text },
];
const response = await generateResponse(event);
conversations[event.ts].push({
role: "assistant",
content: response,
});
// Send response to user
say({
thread_ts: event.ts,
text: response,
});
}
});
// Listen for additional messages in existing conversation threads
app.message(async ({ event, say }) => {
console.log("Reply event: ", event);
// Check if conversation thread exists in conversations object
if (event.thread_ts in conversations) {
const messages = conversations[event.thread_ts];
messages.push({
role: "user",
content: event.text,
});
const response = await generateResponse(event);
messages.push({
role: "assistant",
content: response,
});
// Send response to user
say({
thread_ts: event.thread_ts,
text: response,
});
}
});
(async () => {
// Start the app
await app.start();
console.log("Bot is running!");
})();