Skip to content

Commit b5bf6bd

Browse files
committed
all back end final
1 parent f4e02dc commit b5bf6bd

File tree

5 files changed

+344
-11
lines changed

5 files changed

+344
-11
lines changed

server/api.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Configuration , OpenAIApi} from 'openai';
1+
import OpenAI from 'openai';
22
import dotenv from 'dotenv';
33

44
dotenv.config();
@@ -8,15 +8,11 @@ const openaiApiKey = process.env.OPENAI_API_KEY;
88
if (!openaiApiKey) {
99
console.error('OPENAI_API_KEY is not defined in the environment variables');
1010
throw new Error('OPENAI_API_KEY is not defined in the environment variables');
11-
// This will stop the execution of the script
12-
process.exit(1);
13-
1411
}
1512

16-
const configuration = new Configuration({
13+
const openai = new OpenAI({
1714
apiKey: openaiApiKey,
1815
});
19-
// below is the open ai api instance
20-
// The OpenAIApi class is used to interact with the OpenAI API
21-
const openai = new OpenAIApi(configuration);
22-
// Function to generate SQL query using OpenAI API
16+
17+
export default openai;
18+
// This code imports the OpenAI library and dotenv for environment variable management.

server/generate.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import openaiClient from './api.js';
2+
3+
const generate = async (queryDescription) => {
4+
const turbo = async (queryDescription) => {
5+
const messages = [
6+
{ role: "system", content: "You are a helpful assistant that converts natural language queries into SQL queries." },
7+
{ role: "user", content: "Convert the following natural language description into a SQL Query:\n\nShow all elements in the table users." },
8+
{ role: "assistant", content: "SELECT * FROM users;" },
9+
{ role: "user", content: `Convert the following natural language description into a SQL Query:\n\n${queryDescription}` },
10+
];
11+
12+
const response = await openaiClient.chat.completions.create({
13+
model: "gpt-3.5-turbo-0125",
14+
messages: messages,
15+
max_tokens: 100,
16+
temperature: 0.7,
17+
});
18+
19+
return response.choices[0].message.content.replace(/```sql|```/g, '').trim();
20+
};
21+
22+
try {
23+
return await turbo(queryDescription);
24+
} catch (err) {
25+
console.error("OpenAI API error:", err.response?.data || err.message || err);
26+
throw err;
27+
}
28+
};
29+
30+
export default generate;

server/index.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,42 @@
11
import express from 'express';
22
import cors from 'cors';
3+
import generate from './generate.js';
4+
35

46
const app = express();
57

8+
app.use(express.json()); // Middleware to parse JSON request bodies
9+
// Middleware to enable CORS (Cross-Origin Resource Sharing)
610
app.use(cors());
711

812
const port = process.env.PORT || 3005;
913

14+
1015
// endpoint to test the server
1116
app.get("/", (req, res) => {
1217
res.send("Server is running.Hello World from our API");
1318
});
1419

20+
//Routes to handle the SQL generation
21+
app.post("/generate", async (req, res) => {
22+
const queryDescription = req.body.queryDescription;
23+
console.log("Received Query Description:", queryDescription);
24+
25+
26+
try{
27+
const sqlQuery = await generate(queryDescription);
28+
res.json({ response: sqlQuery });
29+
console.log("Generated SQL Query:", sqlQuery);
30+
}
31+
catch (error) {
32+
console.error("Error generating SQL query:", error.response?.data || error.message || error);
33+
res.status(500).json({ error: "An error occurred while generating the SQL query." });
34+
return;
35+
}
36+
// Call the generate function to get the SQL query
37+
38+
})
39+
1540
// app.listen is a method that starts the server and listens for incoming requests on the specified port
1641
// The port is either taken from the environment variables or defaults to 3005
1742
// The callback function is executed when the server starts successfully

0 commit comments

Comments
 (0)