File tree Expand file tree Collapse file tree 5 files changed +344
-11
lines changed Expand file tree Collapse file tree 5 files changed +344
-11
lines changed Original file line number Diff line number Diff line change 1- import { Configuration , OpenAIApi } from 'openai' ;
1+ import OpenAI from 'openai' ;
22import dotenv from 'dotenv' ;
33
44dotenv . config ( ) ;
@@ -8,15 +8,11 @@ const openaiApiKey = process.env.OPENAI_API_KEY;
88if ( ! 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.
Original file line number Diff line number Diff line change 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 ( / ` ` ` s q l | ` ` ` / 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 ;
Original file line number Diff line number Diff line change 11import express from 'express' ;
22import cors from 'cors' ;
3+ import generate from './generate.js' ;
4+
35
46const app = express ( ) ;
57
8+ app . use ( express . json ( ) ) ; // Middleware to parse JSON request bodies
9+ // Middleware to enable CORS (Cross-Origin Resource Sharing)
610app . use ( cors ( ) ) ;
711
812const port = process . env . PORT || 3005 ;
913
14+
1015// endpoint to test the server
1116app . 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
You can’t perform that action at this time.
0 commit comments