-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgenerate_reply_function.ts
More file actions
65 lines (63 loc) · 1.78 KB
/
generate_reply_function.ts
File metadata and controls
65 lines (63 loc) · 1.78 KB
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
import { DefineFunction, Schema, SlackFunction } from "deno-slack-sdk/mod.ts";
import { MessageType } from "../types/message_type.ts";
import { env } from "../../.env.ts";
export const GenerateReplyFunctionDefinition = DefineFunction({
callback_id: "generate_reply_function",
title: "Generate a reply",
source_file: "functions/generate_reply/generate_reply_function.ts",
input_parameters: {
properties: {
systemMessage: {
type: Schema.types.string,
},
latestMessages: {
type: Schema.types.array,
items: {
type: MessageType,
},
},
},
required: ["latestMessages"],
},
output_parameters: {
properties: {
reply: {
type: Schema.types.string,
},
},
required: ["reply"],
},
});
export default SlackFunction(
GenerateReplyFunctionDefinition,
async ({ inputs, env: slackEnv }) => {
const messages: { role: string; content: string }[] = [
{
role: "system",
content: inputs.systemMessage ?? env.INITIAL_SYSTEM_MESSAGE,
},
...inputs.latestMessages,
];
console.log(
`Payload to send to ChatGPT API: ${JSON.stringify(messages, null, 2)}`,
);
const response = await fetch("https://api.openai.com/v1/chat/completions", {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${slackEnv.OPENAI_API_KEY}`,
},
body: JSON.stringify({
model: "gpt-3.5-turbo-0301",
messages,
}),
});
const completion = await response.json();
console.log(
`ChatGPT API Response: ${JSON.stringify(completion, null, 2)}`,
);
const reply = completion.choices[0].message?.content ??
"Error: No response";
return { outputs: { reply } };
},
);