added working code
This commit is contained in:
parent
5186a99297
commit
b8cc02672a
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
.env
|
||||||
|
node_modules/
|
65
controllers/chatController.js
Normal file
65
controllers/chatController.js
Normal 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
9
models/Document.js
Normal 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
2514
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
25
package.json
Normal file
25
package.json
Normal 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
7
routes/chatRoutes.js
Normal 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
23
server.js
Normal 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));
|
Loading…
x
Reference in New Issue
Block a user