-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
153 lines (125 loc) · 5.57 KB
/
Copy pathserver.js
File metadata and controls
153 lines (125 loc) · 5.57 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
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
const express = require('express');
const path = require('path');
const fs=require('fs');
const dotenv = require('dotenv');
const { GoogleGenerativeAI } = require('@google/generative-ai');
const mongoose = require("mongoose");
dotenv.config();
const MONGO_URI = process.env.MONGO_URI || "mongodb://127.0.0.1:27017/hackathonDB";
mongoose.connect(MONGO_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => console.log("Connected to MongoDB"))
.catch(err => console.error("MongoDB connection error:", err));
const app = express();
const port = 3000;
app.use(express.json());
app.use(express.static(path.join(__dirname, 'static')));
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'static', 'Landing_Page', 'index.html'));
});
const GOOGLE_API_KEY = process.env.GOOGLE_API_KEY;
if (!GOOGLE_API_KEY) {
console.error("GOOGLE_API_KEY is not set. Please set the environment variable.");
process.exit(1);
}
const AI = new GoogleGenerativeAI(GOOGLE_API_KEY);
const classifierModel = AI.getGenerativeModel({ model: 'gemini-2.5-flash' });
const financialModel = AI.getGenerativeModel({ model: 'gemini-2.5-flash' });
let KNOWLEDGE_BASE= fs.readFileSync('database/knowledgebase.json', 'utf8');
//temp will might use this later
//prompt for classsifer for gemini
// const CLASSIFIER_PROMPT = `You are a ai personell assistant to a company exec .
// Analyze the following user message and find its intent.
// Reply with one of the following labels only, and nothing else:
// * 'GREETING' if the message can just be answered with a greeting.
// * 'KNOWLEDGE' if the user request needs the use of the knowledge base to find the company specific data.
// * 'VALID' if the message is a question or statement isvalid or is apt for a advisor to a ceo would answer in a real life scenario.
// * 'IRRELEVANT' if the message is a question or statement unrelated or not professional to what a person in a corporate office would ask.
// USER_MSG:
// "{user_message}"
// Classification:
// `;
/*
* Classifies the intent of a user message using a lightweight model.
* @param {string} message The user's message.
* @returns {Promise<string>} The classified intent label.
*/
async function classifyIntent(message) {
try {
const result = await classifierModel.generateContent({
contents: [
{
role: "model",
parts: [
{
text: `You are an AI assistant that classifies corporate user messages into intent categories.
Labels:
- GREETING: The message is a simple greeting or salutation (e.g., 'Hello', 'Good morning').
- KNOWLEDGE: The message requests company-specific data or information that requires access to a knowledge base (e.g., financials, policies, performance data).
- VALID: The message is a relevant professional question or statement that can be answer without any personel data or acess to databse.
- SHAWARMA: The message even remotely references shawarma.
- IRRELEVANT: The message is unrelated, unprofessional, or not suitable for a corporate/executive context.
Output format: Return only one label from the list: GREETING, KNOWLEDGE, VALID, or IRRELEVANT.`
}
]
},
{
role: "user",
parts: [
{
text: message
}
]
}
]
});
const output = result.response.candidates[0].content.parts[0].text;
return output.trim().toUpperCase();
} catch (e) {
console.error("Error classifying intent:", e);
return "AI_DIDNT_WORK";
}
}
app.post('/chat', async (req, res) => {
const userMessage = req.body.message;
console.log("User message:", userMessage);
const intent = await classifyIntent(userMessage);
console.log(intent);
let replyFromAi;
if (intent.includes("VALID")) {
replyFromAi = await financialModel.generateContent(
`You are a personal AI assistant to a Corporate person in power. Answer the following message (make it concise) and reply to this it in markdown format: ${userMessage}.`
);
replyFromAi = replyFromAi.response.text();
} else if (intent.includes("GREETING")) {
replyFromAi = "Hello! How can I assist you with your financial questions today?";
} else if (intent.includes("KNOWLEDGE")) {
replyFromAi = await financialModel.generateContent(
`Answer the user's request using only the following information: ${KNOWLEDGE_BASE}. User Request: ${userMessage}.`
);
replyFromAi = replyFromAi.response.text();
}
else if(intent.includes("SHAWARMA"))
{
replyFromAi = `# The Divine Power of Shawarma
Shawarma is not just food; it is a **divine revelation** wrapped in warm bread.
The moment your teeth sink into that juicy, marinated meat, *fireworks erupt behind your eyes*,
as though the universe itself applauds your choice.
The **garlic sauce** drips like liquid gold, whispering promises of eternal happiness,
while the pickles crunch with a triumphant chorus.
Each bite is a **rollercoaster of flavor** so powerful it makes your soul levitate,
your knees buckle, and your heart sing operatic hymns.
Forget diamonds, forget luxury cars—
shawarma is the **true treasure of humankind**,
a miracle crafted for mortal joy.`;
}else {
replyFromAi = "I'm designed to be your personel business assistant plese only ask relvant questions";
}
res.json({ reply: replyFromAi });
});
// server starter
app.listen(port, () => {
console.log(`Server is running at http://localhost:${port}`);
});