@@ -3,6 +3,19 @@ import { getSession } from "next-auth/react"
33import type { NextApiRequest , NextApiResponse } from "next"
44import rateLimit from "../../../utils/rate-limit"
55import { env } from "process"
6+ import { MongoClient } from "mongodb"
7+ 8+ const options = {
9+ useUnifiedTopology : true ,
10+ useNewUrlParser : true ,
11+ }
12+ const client = new MongoClient ( process . env . MONGO_URI ! )
13+ 14+ interface Userpromt {
15+ input : string
16+ output : string
17+ createdAt : string
18+ }
619
720const limiter = rateLimit ( {
821 interval : 60 * 1000 , // 60 seconds
@@ -11,20 +24,22 @@ const limiter = rateLimit({
1124
1225const { Configuration, OpenAIApi } = require ( "openai" )
1326
14- const configuration = new Configuration ( {
15- apiKey : env . OPENAI_API_KEY ,
16- } )
17- const openai = new OpenAIApi ( configuration )
18- 1927export default async ( req : NextApiRequest , res : NextApiResponse ) => {
20- await limiter . check ( res , 4 , "CACHE_TOKEN" ) // 8 requests per minute
28+ await limiter . check ( res , 20 , "CACHE_TOKEN" ) // 20 requests per minute
29+ 30+ let configuration = new Configuration ( {
31+ apiKey : process . env . OPENAI_API_KEY ,
32+ } )
33+ let openai = new OpenAIApi ( configuration )
2134
2235 const session = await getSession ( { req } )
2336
2437 //console.log(req.body)
2538 //console.log(req.body.textup)
2639 //console.log(req.body.selectedOption.value)
2740
41+ console . log ( session )
42+ 2843 console . log ( "content length" , req . body . textup . length )
2944 if ( req . body . textup . length > 1000 ) {
3045 res . status ( 400 ) . json ( {
@@ -37,8 +52,8 @@ export default async (req: NextApiRequest, res: NextApiResponse) => {
3752 const { user } = session
3853
3954 openai
40- . createCompletion ( "content-filter-alpha" , {
41- //text-davinci-002 ,
55+ . createCompletion ( {
56+ model : "content-filter-alpha" ,
4257 prompt : "<|endoftext|>" + req . body . textup + "\n--\nLabel:" ,
4358 temperature : 0 ,
4459 max_tokens : 1 ,
@@ -52,24 +67,30 @@ export default async (req: NextApiRequest, res: NextApiResponse) => {
5267
5368 console . log ( "usermail:" , user ?. email )
5469
55- // add sending user id to the request
70+ configuration = new Configuration ( {
71+ apiKey : process . env . OPENAI_API_KEY_CODEX ,
72+ } )
73+ openai = new OpenAIApi ( configuration )
5674
5775 openai
58- . createCompletion ( "text-davinci-002" , {
76+ . createCompletion ( {
77+ model : "code-davinci-002" ,
5978 prompt :
60- "Add typings to the following code:: " +
79+ "Added strong typing to code:\n\n " +
6180 req . body . textup +
62- "\n\n\n\n\n\n" ,
63- temperature : 0.7 ,
81+ "\n\n Start of fixed code:\n" ,
82+ suffix : "\ End of fixed code." ,
83+ temperature : 0.6 ,
6484 max_tokens : 250 ,
6585 top_p : 1 ,
6686 frequency_penalty : 0 ,
67- presence_penalty : 0 ,
87+ presence_penalty : 0.9 ,
6888 user : user ?. email ,
6989 } )
70- . then ( ( response : any ) => {
90+ . then ( async ( response : any ) => {
7191 console . log ( response . data . choices [ 0 ] . text )
7292 //res.status(200).json(response.data)
93+ console . log ( "Response:" , response . data . choices [ 0 ] )
7394 try {
7495 res . status ( 200 ) . json ( { data : response . data . choices [ 0 ] . text } )
7596 } catch ( err ) {
0 commit comments