Add backend server code

This commit is contained in:
Tobias Wasner 2023-01-08 00:03:39 +01:00
parent 535a33f9f1
commit 2538a70049
3 changed files with 73 additions and 0 deletions

2
backend/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
.DS_Store
node_modules

18
backend/package.json Normal file
View File

@ -0,0 +1,18 @@
{
"name": "nodejs",
"version": "1.0.0",
"description": "Backend of ComplAI",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"repository": "https://github.com/gosticks/complai",
"dependencies": {
"body-parser": "^1.20.1",
"cors": "^2.8.5",
"express": "^4.18.2",
"openai": "^3.1.0"
}
}

53
backend/server.js Normal file
View File

@ -0,0 +1,53 @@
const express = require('express');
const cors = require("cors");
bodyParser = require('body-parser');
const app = express();
app.use(cors({
origin: 'https://complai.de/'
}));
// support parsing of application/json type post data
app.use(bodyParser.json());
//support parsing of application/x-www-form-urlencoded post data
app.use(bodyParser.urlencoded({ extended: true }));
app.route("/interpretLaw").post((req, res) => {
const {lawtext} = req.body;
getResponse(lawtext, res);
});
app.listen(8543, ()=>{
console.log('server is runing at port 8543')
});
async function getResponse(lawtext, res) {
const { Configuration, OpenAIApi } = require("openai");
const configuration = new Configuration({
apiKey: process.env.OPEN_AI_API_KEY,
});
const openai = new OpenAIApi(configuration);
const prompt = "Können Sie zuerst in 1 Wort angeben, welche von diesen Dienstleistungen: [Beratung, Finanzierung, Logistik, Outsourcing, Design, Buchhaltung, Recht, IT, Marketing, Verkauf, Training, Forschung, Wartung, Produktion, Consulting, PR, Support, Recruiting, Coaching, Lieferung.] das folgende Gesetzt betrifft? Dann geben Sie in der nächsten Zeile ausführliche Empfehlungen an Unternehmen, die diese Dienste anbieten, zum Umgang mit diesem Gesetz:\n" + lawtext;
console.log("sending to openai: " + prompt);
try {
const response = await openai.createCompletion({
model: "text-davinci-003",
prompt: prompt + "\n",
temperature: 0.2,
max_tokens: 1000,
top_p: 1,
frequency_penalty: 0,
presence_penalty: 0
});
console.log("got from openai: " + JSON.stringify(response.data));
res.status(200).end(history + response.data.choices[0].text);
} catch(error) {
console.error(error.response.status, error.response.data);
res.status(error.response.status).json(error.response.data);
}
}