-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
84 lines (69 loc) · 2.9 KB
/
server.js
File metadata and controls
84 lines (69 loc) · 2.9 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
const express = require("express");
const axios = require("axios");
const cors = require("cors");
require("dotenv").config();
const path = require("path");
const app = express();
app.use(express.json());
// Configure CORS to allow requests from http://localhost:3030 and https://docs.google.com
app.use(cors({
origin: ["http://localhost:3030", "https://docs.google.com"],
methods: ["GET", "POST"],
allowedHeaders: ["Content-Type", "Authorization"],
}));
app.use(express.static(path.join(__dirname)));
// API Key and URL for the AI service
const GROQ_API_KEY = process.env.GROQ_API_KEY; // Replace with your API key in environment variables
const GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions";
console.log("GROQ_API_KEY:", process.env.GROQ_API_KEY);
// API endpoint for AI queries
app.post("/api/ask", async (req, res) => {
try {
const { messages } = req.body;
if (!messages || !Array.isArray(messages) || messages.length === 0) {
return res.status(400).json({ error: "Bad Request", message: "Messages array is required and cannot be empty" });
}
// Add a system prompt to guide the AI's behavior
const systemPrompt = {
role: "system",
content: "You are a helpful and knowledgeable academic assistant. Provide concise and accurate answers in a humanized way, as you are a student an answering a question. Avoid unnecessary formatting, symbols like stars (*), or irrelevant details , do not include ** type thing you use to make it bold during answering the question. Focus on clarity and brevity.And if anything requires to be solve just solve and write the answer not explaination."
};
// Prepend the system prompt to the messages array
const updatedMessages = [systemPrompt, ...messages];
// Make a request to the GROQ API
const response = await axios.post(
GROQ_API_URL,
{
model: "gemma2-9b-it", // Replace with the correct model
messages: updatedMessages,
max_tokens: 150,
temperature: 0.5
},
{
headers: {
Authorization: `Bearer ${GROQ_API_KEY}`,
"Content-Type": "application/json",
},
}
);
// Send the AI response back to the client
res.json(response.data);
} catch (error) {
console.error("Error processing request:", error.response?.data || error.message);
res.status(500).json({
error: "Internal Server Error",
message: error.response?.data?.error?.message || "AI request failed",
});
}
});
// Health check endpoint
app.get("/api/health", (req, res) => {
res.json({ status: "ok", message: "Server is running" });
});
// Start the server
const PORT = process.env.PORT || 8000; // Backend runs on port 8000
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
console.log(`Health check: http://localhost:${PORT}/api/health`);
console.log(`AI endpoint: http://localhost:${PORT}/api/ask`);
});