File tree 1 file changed +40
-0
lines changed
1 file changed +40
-0
lines changed Original file line number Diff line number Diff line change
1
+ const { Configuration, OpenAIApi } = require ( 'openai' ) ;
2
+
3
+ const configuration = new Configuration ( {
4
+ apiKey : process . env . OPENAI_API_KEY ,
5
+ } ) ;
6
+ const openai = new OpenAIApi ( configuration ) ;
7
+
8
+ const generateSQLQuery = async ( req , res ) => {
9
+ const { prompt } = req . body ;
10
+
11
+ try {
12
+ const response = await openai . createCompletion ( {
13
+ model : "text-davinci-003" ,
14
+ max_tokens : 2048 ,
15
+ prompt,
16
+ temperature : 0.2 ,
17
+ } ) ;
18
+
19
+ const query = response . data . choices [ 0 ] . text ;
20
+
21
+ res . status ( 200 ) . json ( {
22
+ success : true ,
23
+ result : query ,
24
+ } ) ;
25
+ } catch ( error ) {
26
+ if ( error . response ) {
27
+ console . log ( error . response . status ) ;
28
+ console . log ( error . response . data ) ;
29
+ } else {
30
+ console . log ( error . message ) ;
31
+ }
32
+
33
+ res . status ( 400 ) . json ( {
34
+ success : false ,
35
+ error : 'The query could not be generated' ,
36
+ } ) ;
37
+ }
38
+ } ;
39
+
40
+ module . exports = { generateSQLQuery } ;
You can’t perform that action at this time.
0 commit comments