-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.ts
81 lines (61 loc) · 2.59 KB
/
test.ts
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
import { GoogleGenerativeAI } from '@google/generative-ai';
import dotenv from 'dotenv';
dotenv.config();
const gemini: GoogleGenerativeAI = new GoogleGenerativeAI(process.env.GEMINI_API_KEY!);
const geminiModel = gemini.getGenerativeModel({
model: "gemini-pro"
})
async function categorizeEmail(emailBody: string): Promise<string> {
if (!emailBody || emailBody === "") {
console.log("Email body is empty")
return "NULL";
}
try {
const result = await geminiModel.generateContent(
`Categorize the following email content:\n\n"${emailBody}"\n\nCategory: Interested, Not-Interested, More information, Neutral`
)
const response: string = result.response.text()
console.log("Response: ", response);
if (response.includes("Not-Interested")) {
return "NOT INTERESTED";
} else if (response.includes("Interested")) {
return "INTERESTED";
} else if (response.includes("More information")) {
return "MORE INFO NEEDED";
} else {
return "NEUTRAL";
}
} catch (error) {
console.log("Error: ", error);
return "NULL";
}
}
async function generateResponseEmail(category: string, content: string, sender: string): Promise<string> {
if (!category || !content) {
console.log("Category or content is empty")
return "No response generated";
}
let prompt: string = "";
switch (category) {
case "Interested":
prompt = `Generate a positive and engaging response email for someone who is interested based on the following email content:\n\n"${content}"\nsender: ${sender}`;
break;
case "Not Interested":
prompt = `Generate a polite and respectful response email for someone who is not interested based on the following email content:\n\n"${content}"\nsender: ${sender}`;
break;
case "More information":
prompt = `Generate an informative response email providing more details based on the following email content:\n\n"${content}"\n\nResponse:`;
break;
default:
prompt = `Generate a neutral response email based on the following email content:\n\n"${content}"\n\nResponse:`;
break;
}
console.log("Prompt: ", prompt)
const response: string = (await geminiModel.generateContent(prompt)).response.text();
console.log("Response: ", response);
if (response) {
return response;
}
return "No response generated";
}
export { categorizeEmail, generateResponseEmail }