-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
126 lines (111 loc) · 3.81 KB
/
Copy pathserver.js
File metadata and controls
126 lines (111 loc) · 3.81 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
118
119
120
121
122
123
124
125
126
import express from 'express';
import cors from 'cors';
import 'dotenv/config';
const app = express();
app.use(cors());
app.use(express.json());
const GROQ_API_KEY = process.env.GROQ_API_KEY;
const GROQ_API_URL = 'https://api.groq.com/openai/v1/chat/completions';
app.get('/health', (req, res) => {
res.json({ status: 'ok', timestamp: new Date().toISOString() });
});
const SYSTEM_PROMPT = `You are an expert database architect. When the user describes an application or system, generate a complete database schema.
Respond ONLY with valid JSON in this exact format:
{
"tables": [
{
"name": "table_name",
"columns": [
{
"name": "column_name",
"dataType": "INT|VARCHAR|TEXT|BOOLEAN|DATE|TIMESTAMP|DECIMAL|FLOAT|DOUBLE|UUID|BIGINT|JSON",
"length": 255,
"isPrimaryKey": true,
"isForeignKey": false,
"autoIncrement": true,
"unique": false,
"notNull": true,
"defaultValue": null
}
],
"description": "What this table stores"
}
],
"relationships": [
{
"sourceTable": "table_with_foreign_key",
"targetTable": "referenced_table",
"sourceColumn": "fk_column",
"targetColumn": "pk_column",
"type": "ONE_TO_ONE|ONE_TO_MANY|MANY_TO_MANY"
}
]
}
Rules:
- Always include id (INT, PK, autoIncrement) in every table
- Use appropriate data types for each column
- Add created_at (TIMESTAMP) to all tables
- Create junction tables for MANY_TO_MANY relationships
- Add meaningful indexes
- Keep response concise, only JSON no extra text
- Do not wrap in markdown code blocks, return raw JSON only`;
app.post('/api/schema', async (req, res) => {
try {
const { prompt, history } = req.body;
const messages = [
...(history || []).slice(-10).map((m) => ({ role: m.role, content: m.content })),
{ role: 'user', content: prompt },
];
const response = await fetch(GROQ_API_URL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${GROQ_API_KEY}`,
},
body: JSON.stringify({
model: 'llama-3.3-70b-versatile',
messages: [{ role: 'system', content: SYSTEM_PROMPT }, ...messages],
temperature: 0.3,
max_tokens: 4096,
}),
});
if (!response.ok) {
const error = await response.json().catch(() => ({}));
return res.status(response.status).json({ error: error.error?.message || 'Groq API error' });
}
const data = await response.json();
res.json({ content: data.choices[0]?.message?.content || '' });
} catch (err) {
res.status(500).json({ error: 'Server error: ' + err.message });
}
});
app.post('/api/chat', async (req, res) => {
try {
const { messages } = req.body;
const response = await fetch(GROQ_API_URL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${GROQ_API_KEY}`,
},
body: JSON.stringify({
model: 'llama-3.3-70b-versatile',
messages: [{ role: 'system', content: 'You are a helpful database design assistant. Answer questions about database design, suggest improvements, and help with schema planning. Be concise and technical.' }, ...messages],
temperature: 0.5,
max_tokens: 2048,
}),
});
if (!response.ok) {
const error = await response.json().catch(() => ({}));
return res.status(response.status).json({ error: error.error?.message || 'Groq API error' });
}
const data = await response.json();
res.json({ content: data.choices[0]?.message?.content || '' });
} catch (err) {
res.status(500).json({ error: 'Server error: ' + err.message });
}
});
const PORT = 3001;
app.listen(PORT, () => {
console.log(`AI Proxy server running on http://127.0.0.1:${PORT}`);
});