-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
37 lines (30 loc) · 919 Bytes
/
server.js
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
import express from "express";
import bodyParser from "body-parser";
import cors from "cors";
import { Configuration, OpenAIApi } from 'openai';
import dotenv from "dotenv-safe";
dotenv.config();
// OpenAI API init
const configuration = new Configuration({
organization: process.env.OPEN_AI_ORG,
apiKey: process.env.OPEN_AI_API_KEY,
});
const openai = new OpenAIApi(configuration);
const app = express().use(cors()).use(bodyParser.json());
const model = "gpt-3.5-turbo";
const port = 3000;
app.post("/", async (req, res) => {
const response = await openai.createChatCompletion({
model: model,
messages: [
{role: "user", content: req.body.prompt}
],
max_tokens: 1000,
});
const reply = response.data.choices[0].message.content;
console.log(reply);
res.json({"reply": reply.trimStart()});
});
app.listen(port, () => {
console.log(`ChromeGPT API listening on port ${port}`)
})