Spaces:
Sleeping
Sleeping
Update src/pages/api/sql.ts
Browse files- src/pages/api/sql.ts +15 -15
src/pages/api/sql.ts
CHANGED
|
@@ -23,7 +23,7 @@ export default async function handler(
|
|
| 23 |
}
|
| 24 |
|
| 25 |
try {
|
| 26 |
-
const apiKey = process.env.
|
| 27 |
if (!apiKey) {
|
| 28 |
return res.status(500).json({
|
| 29 |
message: "Server configuration error",
|
|
@@ -76,7 +76,7 @@ Text: ${userPrompt}`;
|
|
| 76 |
try {
|
| 77 |
const content = response?.choices?.[0]?.message?.content?.trim() || '';
|
| 78 |
|
| 79 |
-
const jsonMatch = content.match(/\{
|
| 80 |
if (jsonMatch) {
|
| 81 |
const parsedResponse = JSON.parse(jsonMatch[0]);
|
| 82 |
sqlQuery = parsedResponse.query?.trim();
|
|
@@ -120,14 +120,14 @@ Text: ${userPrompt}`;
|
|
| 120 |
|
| 121 |
const dataAnalysis = {
|
| 122 |
totalColumns: columns.length,
|
| 123 |
-
numericColumns: columns.filter(col =>
|
| 124 |
-
typeof firstRow[col] === 'number' &&
|
| 125 |
!col.toLowerCase().includes('id') &&
|
| 126 |
!col.toLowerCase().includes('_id')
|
| 127 |
),
|
| 128 |
-
dateColumns: columns.filter(col => firstRow[col] instanceof Date),
|
| 129 |
-
stringColumns: columns.filter(col =>
|
| 130 |
-
typeof firstRow[col] === 'string' ||
|
| 131 |
col.toLowerCase().includes('name') ||
|
| 132 |
col.toLowerCase().includes('title')
|
| 133 |
),
|
|
@@ -155,13 +155,13 @@ Text: ${userPrompt}`;
|
|
| 155 |
visualization = {
|
| 156 |
type: requestedChartType,
|
| 157 |
config: {
|
| 158 |
-
labels: results.map(row =>
|
| 159 |
dataAnalysis.stringColumns[0]
|
| 160 |
? String(row[dataAnalysis.stringColumns[0]])
|
| 161 |
: `Row ${results.indexOf(row) + 1}`
|
| 162 |
),
|
| 163 |
datasets: [{
|
| 164 |
-
data: results.map(row => row[dataAnalysis.numericColumns[0]]),
|
| 165 |
backgroundColor: results.map(() =>
|
| 166 |
`hsla(${Math.random() * 360}, 70%, 50%, 0.6)`
|
| 167 |
)
|
|
@@ -177,11 +177,11 @@ Text: ${userPrompt}`;
|
|
| 177 |
type: 'line',
|
| 178 |
config: {
|
| 179 |
labels: dataAnalysis.dateColumns.length > 0
|
| 180 |
-
? results.map(row => new Date(row[dataAnalysis.dateColumns[0]]).toLocaleDateString())
|
| 181 |
: results.map((_, idx) => `Point ${idx + 1}`),
|
| 182 |
-
datasets: dataAnalysis.numericColumns.map(col => ({
|
| 183 |
label: col,
|
| 184 |
-
data: results.map(row => row[col]),
|
| 185 |
borderColor: `hsl(${Math.random() * 360}, 70%, 50%)`,
|
| 186 |
tension: 0.1
|
| 187 |
}))
|
|
@@ -196,11 +196,11 @@ Text: ${userPrompt}`;
|
|
| 196 |
type: 'bar',
|
| 197 |
config: {
|
| 198 |
labels: dataAnalysis.stringColumns.length > 0
|
| 199 |
-
? results.map(row => String(row[dataAnalysis.stringColumns[0]]))
|
| 200 |
: results.map((_, idx) => `Row ${idx + 1}`),
|
| 201 |
-
datasets: dataAnalysis.numericColumns.map(col => ({
|
| 202 |
label: col,
|
| 203 |
-
data: results.map(row => row[col]),
|
| 204 |
backgroundColor: `hsla(${Math.random() * 360}, 70%, 50%, 0.6)`,
|
| 205 |
borderColor: `hsl(${Math.random() * 360}, 70%, 50%)`,
|
| 206 |
borderWidth: 1
|
|
|
|
| 23 |
}
|
| 24 |
|
| 25 |
try {
|
| 26 |
+
const apiKey = process.env.HUGGINGFACE_API_KEY;
|
| 27 |
if (!apiKey) {
|
| 28 |
return res.status(500).json({
|
| 29 |
message: "Server configuration error",
|
|
|
|
| 76 |
try {
|
| 77 |
const content = response?.choices?.[0]?.message?.content?.trim() || '';
|
| 78 |
|
| 79 |
+
const jsonMatch = content.match(/\{[\s\S]*\}/);
|
| 80 |
if (jsonMatch) {
|
| 81 |
const parsedResponse = JSON.parse(jsonMatch[0]);
|
| 82 |
sqlQuery = parsedResponse.query?.trim();
|
|
|
|
| 120 |
|
| 121 |
const dataAnalysis = {
|
| 122 |
totalColumns: columns.length,
|
| 123 |
+
numericColumns: columns.filter((col: string) =>
|
| 124 |
+
typeof (firstRow as any)[col] === 'number' &&
|
| 125 |
!col.toLowerCase().includes('id') &&
|
| 126 |
!col.toLowerCase().includes('_id')
|
| 127 |
),
|
| 128 |
+
dateColumns: columns.filter((col: string) => (firstRow as any)[col] instanceof Date),
|
| 129 |
+
stringColumns: columns.filter((col: string) =>
|
| 130 |
+
typeof (firstRow as any)[col] === 'string' ||
|
| 131 |
col.toLowerCase().includes('name') ||
|
| 132 |
col.toLowerCase().includes('title')
|
| 133 |
),
|
|
|
|
| 155 |
visualization = {
|
| 156 |
type: requestedChartType,
|
| 157 |
config: {
|
| 158 |
+
labels: results.map((row: any) =>
|
| 159 |
dataAnalysis.stringColumns[0]
|
| 160 |
? String(row[dataAnalysis.stringColumns[0]])
|
| 161 |
: `Row ${results.indexOf(row) + 1}`
|
| 162 |
),
|
| 163 |
datasets: [{
|
| 164 |
+
data: results.map((row: any) => row[dataAnalysis.numericColumns[0]]),
|
| 165 |
backgroundColor: results.map(() =>
|
| 166 |
`hsla(${Math.random() * 360}, 70%, 50%, 0.6)`
|
| 167 |
)
|
|
|
|
| 177 |
type: 'line',
|
| 178 |
config: {
|
| 179 |
labels: dataAnalysis.dateColumns.length > 0
|
| 180 |
+
? results.map((row: any) => new Date(row[dataAnalysis.dateColumns[0]]).toLocaleDateString())
|
| 181 |
: results.map((_, idx) => `Point ${idx + 1}`),
|
| 182 |
+
datasets: dataAnalysis.numericColumns.map((col: string) => ({
|
| 183 |
label: col,
|
| 184 |
+
data: results.map((row: any) => row[col]),
|
| 185 |
borderColor: `hsl(${Math.random() * 360}, 70%, 50%)`,
|
| 186 |
tension: 0.1
|
| 187 |
}))
|
|
|
|
| 196 |
type: 'bar',
|
| 197 |
config: {
|
| 198 |
labels: dataAnalysis.stringColumns.length > 0
|
| 199 |
+
? results.map((row: any) => String(row[dataAnalysis.stringColumns[0]]))
|
| 200 |
: results.map((_, idx) => `Row ${idx + 1}`),
|
| 201 |
+
datasets: dataAnalysis.numericColumns.map((col: string) => ({
|
| 202 |
label: col,
|
| 203 |
+
data: results.map((row: any) => row[col]),
|
| 204 |
backgroundColor: `hsla(${Math.random() * 360}, 70%, 50%, 0.6)`,
|
| 205 |
borderColor: `hsl(${Math.random() * 360}, 70%, 50%)`,
|
| 206 |
borderWidth: 1
|