Skip to content

Commit

Permalink
API
Browse files Browse the repository at this point in the history
  • Loading branch information
redji committed Apr 12, 2024
1 parent d729638 commit 047f891
Show file tree
Hide file tree
Showing 8 changed files with 359 additions and 5 deletions.
1 change: 1 addition & 0 deletions API/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
30 changes: 30 additions & 0 deletions API/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,36 @@
const express = require('express');
const app = express();
const PORT = process.env.PORT || 3000;
const bodyParser = require('body-parser');
require('dotenv').config();
const { chatCompletion, embedding, transcript } = require('./openAPI/index.js');

const APIKey = process.env['API_KEY'];

app.use(bodyParser.json());
app.post('/answer/', async (req, res) => {
// Access JSON data from the request body
const jsonData = req.body;

// Do something with the JSON data
//console.log('Received JSON data:', jsonData);

// Send a response
await chatCompletion({
messages: [
{
role: 'system',
content: ''
},
{
role: 'user',
content: jsonData.question
}],
model: 'gpt-3.5-turbo'
}).then(async (response) => {
res.json({ reply: response.choices[0].message.content });
});
});

app.get('/', (req, res) => {
res.send('Hello, Aiva!');
Expand Down
Empty file modified API/build_docker.sh
100644 → 100755
Empty file.
41 changes: 41 additions & 0 deletions API/openAPI/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const { config } = require('dotenv');
const OpenAI = require('openai');
const { makeRequestWithDelay } = require("../utils/makeRequest.js");

config();

const OpenAIKey = process.env.OPENAI_API_KEY;

const openai = new OpenAI({
apiKey: OpenAIKey // This is the default and can be omitted
});

async function chatCompletion(createObject) {
return await openai.chat.completions.create(createObject);
}

async function moderations(input) {
return await makeRequestWithDelay(`https://api.openai.com/v1/moderations`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${OpenAIKey}`
},
body: JSON.stringify({ input })
});
}

async function embedding(createObject) {
return await openai.embeddings.create(createObject);
}

async function transcript(createObject) {
return await openai.audio.transcriptions.create(createObject);
}

module.exports = {
moderations,
chatCompletion,
embedding,
transcript
};
Loading

0 comments on commit 047f891

Please sign in to comment.