-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathindex.ts
38 lines (30 loc) · 1022 Bytes
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import express,{Application, Request, Response} from 'express'
import cors from 'cors'
import OpenAI from "openai";
import dotenv from 'dotenv';
dotenv.config(); // Load environment variables from .env file
const PORT:number=8000
const app:Application=express()
app.use(cors())
app.use(express.json())
// const API_KEY:string="YOUR_OPENAI_API_KEY"
const API_KEY = process.env.VITE_OPENAI_API_KEY
const openai= new OpenAI({
apiKey:API_KEY
})
app.post("/completions", async(req:Request,res:Response)=> {
try{
const completion = await openai.chat.completions.create({
model:"gpt-3.5-turbo",
temperature: 0,
max_tokens: 200,
messages:[{role:"user",
content:"refactor this code:\n"+req.body.message}]
})
res.send(completion.choices[0].message)
}catch(error){
console.error(error)
res.status(500).send("Server error")
}
})
app.listen(PORT,()=> console.log(`Your server is running on PORT: ${PORT}`))