added working code

This commit is contained in:
BHAGTANIDEEPAK 2025-04-14 17:40:01 +05:30
parent 5186a99297
commit b8cc02672a
7 changed files with 2645 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
.env
node_modules/

View File

@ -0,0 +1,65 @@
// import axios from 'axios';
// import Document from '../models/Document.js';
// const MISTRAL_API_KEY = process.env.MISTRAL_API_KEY;
// const MISTRAL_URL = "https://api.mistral.ai/chat/completions";
// export const chatWithAI = async (req, res) => {
// try {
// const { department, message } = req.body;
// // Fetch relevant documents
// const docs = await Document.find({ department });
// const context = docs.map(doc => doc.content).join("\n");
// // Call Mistral API
// const response = await axios.post(
// MISTRAL_URL,
// {
// messages: [{ role: "system", content: `You are an AI assistant for ${department} department.` },
// { role: "user", content: `Based on these documents: ${context} \n User Query: ${message}` }]
// },
// { headers: { Authorization: `Bearer ${MISTRAL_API_KEY}` } }
// );
// res.json({ response: response.data.choices[0].message.content });
// } catch (error) {
// console.error(error);
// res.status(500).json({ error: "Error processing the request" });
// }
// };
import { Mistral } from '@mistralai/mistralai';
import Document from '../models/Document.js';
import dotenv from 'dotenv';
dotenv.config();
const MISTRAL_API_KEY = process.env.MISTRAL_API_KEY;
console.log(MISTRAL_API_KEY);
const client = new Mistral({ apiKey: MISTRAL_API_KEY });
export const chatWithAI = async (req, res) => {
try {
const { department, message } = req.body;
// Fetch relevant documents
const docs = await Document.find({ department });
const context = docs.map(doc => doc.content).join("\n");
// Call Mistral API
const chatResponse = await client.chat.complete({
model: "mistral-large-latest",
messages: [
{ role: "system", content: `You are an AI assistant for ${department} department.` },
{ role: "user", content: `Based on these documents: ${context}\nUser Query: ${message} if you do not have exact information you can give general response of 1 to 1.5 line ` }
]
});
res.json({ response: chatResponse.choices[0].message.content });
} catch (error) {
console.error(error);
res.status(500).json({ error: "Error processing the request" });
}
};

9
models/Document.js Normal file
View File

@ -0,0 +1,9 @@
import mongoose from 'mongoose';
const documentSchema = new mongoose.Schema({
department: String,
type: String,
content: String
});
export default mongoose.model('Document', documentSchema);

2514
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

25
package.json Normal file
View File

@ -0,0 +1,25 @@
{
"name": "backend",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@mistralai/mistralai": "^1.5.0",
"axios": "^1.8.1",
"body-parser": "^1.20.3",
"cors": "^2.8.5",
"dotenv": "^16.4.7",
"express": "^4.21.2",
"mongoose": "^8.12.1"
},
"devDependencies": {
"nodemon": "^3.1.9"
}
}

7
routes/chatRoutes.js Normal file
View File

@ -0,0 +1,7 @@
import express from 'express';
import { chatWithAI } from '../controllers/chatController.js';
const router = express.Router();
router.post('/', chatWithAI);
export default router;

23
server.js Normal file
View File

@ -0,0 +1,23 @@
import express from 'express';
import cors from 'cors';
import dotenv from 'dotenv';
import mongoose from 'mongoose';
import chatRoutes from './routes/chatRoutes.js';
dotenv.config();
const app = express();
app.use(express.json());
app.use(cors());
// Routes
app.use('/api/chat', chatRoutes);
app.get('/', (req, res) => {
res.send('Server is running');
});
console.log(process.env.PORT)
const PORT = process.env.PORT || 5000;
mongoose.connect(process.env.MONGO_URI, { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => app.listen(PORT, () => console.log(`Server running on port ${PORT}`)))
.catch(err => console.error(err));