-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
117 lines (95 loc) · 3.3 KB
/
Copy pathserver.js
File metadata and controls
117 lines (95 loc) · 3.3 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
const express = require('express');
const cors = require('cors');
const dotenv = require('dotenv');
const path = require('path');
dotenv.config();
const app = express();
const PORT = process.env.PORT || 3000;
// Middleware
app.use(cors());
app.use(express.json());
app.use(express.static(path.join(__dirname, 'public')));
// In-memory storage for chat history (in production, use a database)
const chatHistory = [];
// DeepSeek API configuration
const DEEPSEEK_API_KEY = process.env.DEEPSEEK_API_KEY || 'sk-e789f2f01936430b843d08ea81c13c6d';
const DEEPSEEK_API_URL = 'https://api.deepseek.com/chat/completions';
// Language options
const LANGUAGES = {
english: 'English',
pidgin: 'Nigerian Pidgin',
yoruba: 'Yoruba',
igbo: 'Igbo',
hausa: 'Hausa'
};
// System prompt for the Qwen model
const SYSTEM_PROMPT = `You are Compatriot, a helpful assistant that explains constitutional and legal rights in Nigeria.
You should respond in the language selected by the user. The available languages are:
- English
- Nigerian Pidgin
- Yoruba
- Igbo
- Hausa
Focus on providing accurate information about Nigerian constitutional and legal rights. If you're unsure about specific legal details,
acknowledge the limitation and suggest consulting with a legal professional. Always be respectful and helpful.`;
// Chat endpoint
app.post('/api/chat', async (req, res) => {
try {
const { message, language } = req.body;
// Validate language
if (!LANGUAGES[language]) {
return res.status(400).json({ error: 'Invalid language selected' });
}
// Add user message to history
chatHistory.push({ role: 'user', content: message, language });
// Prepare the prompt with language context
const prompt = `Respond in ${LANGUAGES[language]}: ${message}`;
// Call DeepSeek API
const response = await fetch(DEEPSEEK_API_URL, {
method: 'POST',
headers: {
'Authorization': `Bearer ${DEEPSEEK_API_KEY}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
model: 'deepseek-chat',
messages: [
{ role: 'system', content: SYSTEM_PROMPT },
{ role: 'user', content: prompt }
],
temperature: 0.7
})
});
if (!response.ok) {
const errorData = await response.json();
throw new Error(`DeepSeek API error: ${errorData.error?.message || response.statusText}`);
}
const data = await response.json();
const botResponse = data.choices[0].message.content;
// Add bot response to history
chatHistory.push({ role: 'assistant', content: botResponse, language });
res.json({ response: botResponse });
} catch (error) {
console.error('Chat error:', error);
res.status(500).json({ error: 'Failed to get response from AI model' });
}
});
// Get supported languages
app.get('/api/languages', (req, res) => {
res.json(LANGUAGES);
});
// Get chat history
app.get('/api/history', (req, res) => {
res.json(chatHistory);
});
// Serve the frontend
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
// Serve all other routes to support React Router
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});