Skip to content

Commit

Permalink
Merge pull request #808 from sp9324/main
Browse files Browse the repository at this point in the history
added gemini chatbot final
  • Loading branch information
SUGAM-ARORA authored Oct 11, 2024
2 parents 90570d9 + 77c475f commit 336e35c
Show file tree
Hide file tree
Showing 10 changed files with 603 additions and 23 deletions.
44 changes: 39 additions & 5 deletions BACKEND/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,26 @@ const express = require('express');
const mongoose = require('mongoose');
const cookieParser = require('cookie-parser');
const cors = require('cors');
const { GoogleGenerativeAI } = require("@google/generative-ai");

// CHATBOT
const genAI = new GoogleGenerativeAI(process.env.GOOGLE_API_KEY);
const model = genAI.getGenerativeModel({ model: "gemini-pro"});
const chat = model.startChat({
history: [
{
role: "user",
parts: [{ text: "Hello, I'm the user. Please answer my general queries related to development and open source." }],
},
{
role: "model",
parts: [{ text: "Hello, I'm your helpful Tech Assistant. How can I assist you? Some info about Unicollab: UniCollab is more than just a platform; it's a gateway to a world where students from different universities/colleges converge to collaborate, innovate, and elevate their projects. Imagine a space where ideas flow freely, where knowledge is shared effortlessly, and where learning transcends classrooms. That's UniCollab for you." }],
},
],
generationConfig: {
maxOutputTokens: 500,
},
});

const app = express();
const port = process.env.PORT || 5000;
Expand All @@ -16,13 +36,27 @@ mongoose.connect(process.env.MONGODB_URI, { useNewUrlParser: true, useUnifiedTop
.then(() => console.log('MongoDB connected'))
.catch(err => console.error('MongoDB connection error:', err));

const authRoutes = require('./routes/auth');
const authMiddleware = require('./middleware/auth');
app.post('/chatbot', async (req, res) => {
const userMessage = req.body.message;
console.log(userMessage);
// Check if userMessage is valid and not empty
if (!userMessage || !userMessage.trim()) {
return res.status(400).json({ message: 'Message must not be empty.' });
}

app.use('/auth', authRoutes);
try {
const result = await chat.sendMessage(userMessage);
console.log("result: ", result);
const response = await result.response;
console.log("response: ", response);
const text = response.text();
console.log("response.text: ", text);

app.get('/protected', authMiddleware, (req, res) => {
res.send(`Hello ${req.user.username}, this is a protected route.`);
res.json({ message: text });
} catch (error) {
console.error(error);
res.status(500).send("Server Error");
}
});

app.listen(port, () => {
Expand Down
1 change: 1 addition & 0 deletions BACKEND/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"@google/generative-ai": "^0.21.0",
"bcryptjs": "^2.4.3",
"body-parser": "^1.20.2",
"cookie-parser": "^1.4.7",
Expand Down
Loading

0 comments on commit 336e35c

Please sign in to comment.