Spaces:
Sleeping
Sleeping
File size: 7,929 Bytes
645ad9d 75db0a2 645ad9d 9baa557 645ad9d 9baa557 645ad9d 9baa557 645ad9d 9baa557 645ad9d 9baa557 645ad9d 9baa557 645ad9d 9baa557 645ad9d 9baa557 645ad9d 9baa557 645ad9d 9baa557 645ad9d 9baa557 645ad9d |
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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
import { HfInference } from "@huggingface/inference";
import { NextApiRequest, NextApiResponse } from "next";
import { createConnection, executeQuery } from "@/utils/database";
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
if (req.method !== "POST") {
return res.status(405).json({ message: "Method not allowed" });
}
const { dbUri, userPrompt } = req.body;
if (!dbUri || !userPrompt) {
return res.status(400).json({
message: "Missing required fields",
details: {
dbUri: !dbUri ? "Database URI is required" : null,
userPrompt: !userPrompt ? "Query prompt is required" : null
}
});
}
try {
const apiKey = process.env.API_TOKEN;
if (!apiKey) {
return res.status(500).json({
message: "Server configuration error",
details: "API key is not configured"
});
}
let hf;
try {
hf = new HfInference(apiKey);
} catch (error: any) {
return res.status(500).json({
message: "Failed to initialize AI model",
details: error.message
});
}
let response;
const prompt = `You are a SQL expert. Convert the following text to a SQL query.
Rules:
- Return a JSON object with exactly this format: {"query": "YOUR SQL QUERY HERE", "chartType": "CHART TYPE HERE"}
- For chartType use one of: "bar", "pie", "line", "doughnut", or null
- The query should be safe and only return the requested data
- Keep table names exactly as provided
- Do not include any explanations or comments
Example input: "Show me sales data as a pie chart"
Example output: {"query": "SELECT * FROM sales LIMIT 10", "chartType": "pie"}
Text: ${userPrompt}`;
try {
response = await hf.chatCompletion({
model: "nvidia/Llama-3.1-Nemotron-70B-Instruct-HF",
messages: [{ role: "user", content: prompt }],
temperature: 0.1,
max_tokens: 500,
});
console.log(response);
} catch (error: any) {
console.log(error);
return res.status(500).json({
message: "AI model error",
details: error.message || "Failed to generate SQL query"
});
}
let sqlQuery = '';
let requestedChartType = null;
try {
const content = response?.choices?.[0]?.message?.content?.trim() || '';
const jsonMatch = content.match(/\{[\s\S]*\}/);
if (jsonMatch) {
const parsedResponse = JSON.parse(jsonMatch[0]);
sqlQuery = parsedResponse.query?.trim();
requestedChartType = parsedResponse.chartType;
} else {
sqlQuery = content
.replace(/```sql/gi, '')
.replace(/```/gi, '')
.replace(/sql query:?\s*/gi, '')
.replace(/query:?\s*/gi, '')
.trim();
}
if (!sqlQuery) {
throw new Error('No valid SQL query found in response');
}
} catch (error: any) {
return res.status(500).json({
message: "Failed to parse AI response",
details: error.message
});
}
let connection;
try {
connection = await createConnection(dbUri);
} catch (error: any) {
return res.status(500).json({
message: "Database connection error",
details: error.message
});
}
try {
const results = await executeQuery(connection, sqlQuery || '');
let visualization = null;
if (Array.isArray(results) && results.length > 0) {
const firstRow = results[0];
const columns = Object.keys(firstRow);
const dataAnalysis = {
totalColumns: columns.length,
numericColumns: columns.filter((col: string) =>
typeof (firstRow as any)[col] === 'number' &&
!col.toLowerCase().includes('id') &&
!col.toLowerCase().includes('_id')
),
dateColumns: columns.filter((col: string) => (firstRow as any)[col] instanceof Date),
stringColumns: columns.filter((col: string) =>
typeof (firstRow as any)[col] === 'string' ||
col.toLowerCase().includes('name') ||
col.toLowerCase().includes('title')
),
rowCount: results.length
};
if (requestedChartType === 'pie' || requestedChartType === 'doughnut') {
const preferredNumericColumns = dataAnalysis.numericColumns.filter(col =>
col.toLowerCase().includes('status') ||
col.toLowerCase().includes('count') ||
col.toLowerCase().includes('amount') ||
col.toLowerCase().includes('total')
);
if (preferredNumericColumns.length > 0) {
dataAnalysis.numericColumns = preferredNumericColumns;
}
}
if (requestedChartType) {
switch (requestedChartType) {
case 'pie':
case 'doughnut':
if (dataAnalysis.numericColumns.length > 0) {
visualization = {
type: requestedChartType,
config: {
labels: results.map((row: any) =>
dataAnalysis.stringColumns[0]
? String(row[dataAnalysis.stringColumns[0]])
: `Row ${results.indexOf(row) + 1}`
),
datasets: [{
data: results.map((row: any) => row[dataAnalysis.numericColumns[0]]),
backgroundColor: results.map(() =>
`hsla(${Math.random() * 360}, 70%, 50%, 0.6)`
)
}]
}
};
}
break;
case 'line':
if (dataAnalysis.dateColumns.length > 0 || dataAnalysis.numericColumns.length > 0) {
visualization = {
type: 'line',
config: {
labels: dataAnalysis.dateColumns.length > 0
? results.map((row: any) => new Date(row[dataAnalysis.dateColumns[0]]).toLocaleDateString())
: results.map((_, idx) => `Point ${idx + 1}`),
datasets: dataAnalysis.numericColumns.map((col: string) => ({
label: col,
data: results.map((row: any) => row[col]),
borderColor: `hsl(${Math.random() * 360}, 70%, 50%)`,
tension: 0.1
}))
}
};
}
break;
case 'bar':
default:
visualization = {
type: 'bar',
config: {
labels: dataAnalysis.stringColumns.length > 0
? results.map((row: any) => String(row[dataAnalysis.stringColumns[0]]))
: results.map((_, idx) => `Row ${idx + 1}`),
datasets: dataAnalysis.numericColumns.map((col: string) => ({
label: col,
data: results.map((row: any) => row[col]),
backgroundColor: `hsla(${Math.random() * 360}, 70%, 50%, 0.6)`,
borderColor: `hsl(${Math.random() * 360}, 70%, 50%)`,
borderWidth: 1
}))
}
};
break;
}
}
}
await connection.end();
return res.status(200).json({
results,
query: sqlQuery,
visualization
});
} catch (error: any) {
await connection?.end();
return res.status(500).json({
message: "Query execution error",
details: error.message
});
}
} catch (error: any) {
console.error('Unexpected API Error:', error);
return res.status(500).json({
message: "Unexpected error occurred",
details: error.message || "Unknown error"
});
}
}
|