Skip to content

Commit

Permalink
Merge pull request #580 from jay-m-dev/openai_connection
Browse files Browse the repository at this point in the history
Openai connection
  • Loading branch information
nickotto authored Feb 23, 2023
2 parents 3514df8 + d999d15 commit f6184ff
Show file tree
Hide file tree
Showing 13 changed files with 580 additions and 2 deletions.
30 changes: 30 additions & 0 deletions chatapi.rest
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
POST http://localhost:5080/chatapi/v1/chat
Content-Type: application/json

{
"title" : "Hello",
"_experient_id": "63f6e4987c5f93004a3e3ca8",
"_dataset_id": "63f6e4947c5f93004a3e3ca7"
}

###

POST http://localhost:5080/chatapi/v1/chatlog/
Content-Type: application/json

{
"_chat_id" : "63f6e54fc76ccd8396386d03",
"message" : "Hello there from cyberspace!",
"message_type" : "text",
"who" : "openai"
}

###

GET http://localhost:5080/chatapi/v1/chat

###

GET http://localhost:5080/chatapi/v1/chat/63f6e54fc76ccd8396386d03

###
3 changes: 3 additions & 0 deletions config/common.env
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,7 @@ DT_MAX_DEPTH=6
DOCKER_CLIENT_TIMEOUT=120
COMPOSE_HTTP_TIMEOUT=120

OPENAI_API_KEY=your_openai_api_key
OPENAI_ORG_ID=your_openai_org_id

STARTUP_DATASET_PATH=/appsrc/data/datasets/user
16 changes: 16 additions & 0 deletions lab/dbgoose.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const mongoose = require('mongoose');

if (process.env.DBMONGO_HOST && process.env.DBMONGO_PORT) {
mongouri="mongodb://"+process.env.DBMONGO_HOST+":"+process.env.DBMONGO_PORT+"/FGLab";
} else if (process.env.MONGODB_URI) {
mongouri=process.env.MONGODB_URI;
} else {
console.log("Error: No MongoDB instance specified");
process.exit(1);
}
mongoose.connect(mongouri, { useNewUrlParser: true, useUnifiedTopology: true })
const db = mongoose.connection;
db.on('error', (error) => console.error(error));
db.once('open', () => console.log('Mongoose connected to Database'));

module.exports = db;
7 changes: 5 additions & 2 deletions lab/lab.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ var emitEvent = require("./socketServer").emitEvent;
var generateFeaturesFromFileIdAsync = require("./pyutils").generateFeaturesFromFileIdAsync;
var validateDatafileByFileIdAsync = require("./pyutils").validateDatafileByFileIdAsync;
const assert = require("assert");

const openaiRouter = require('./routes/openai');
const chatapiRouter = require('./routes/chatapi');

/***************
* Enums
Expand Down Expand Up @@ -128,8 +129,10 @@ app.use(bodyParser.urlencoded({limit: "50mb", extended: true, parameterLimit:500
app.set('appPath', path.join(path.normalize(__dirname), 'webapp/dist'));
app.use(express.static(app.get('appPath')));

app.use('/openai/v1', openaiRouter);
app.use('/chatapi/v1', chatapiRouter);

/* API */
/* Lab API */

// Registers webhooks
app.post("/api/v1/webhooks", jsonParser, (req, res) => {
Expand Down
21 changes: 21 additions & 0 deletions lab/models/chat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const mongoose = require('mongoose');

const chatSchema = new mongoose.Schema({
title: {
type: String,
required: true
},
_dataset_id: {
type: mongoose.Schema.Types.ObjectId,
},
_experiment_id: {
type: mongoose.Schema.Types.ObjectId,
},
date: {
type: Date,
required: true,
default: Date.now
}
});

module.exports = mongoose.model('Chat', chatSchema);
27 changes: 27 additions & 0 deletions lab/models/chatlog.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const mongoose = require('mongoose');

const chatlogSchema = new mongoose.Schema({
_chat_id: {
type: mongoose.Schema.Types.ObjectId,
required: true
},
message: {
type: String,
},
message_type: {
type: String,
},
source_code: {
type: String,
},
who: {
type: String,
},
date: {
type: Date,
required: true,
default: Date.now
}
});

module.exports = mongoose.model('Chatlog', chatlogSchema);
14 changes: 14 additions & 0 deletions lab/models/openaiconfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const mongoose = require('mongoose');

const openaiconfigSchema = new mongoose.Schema({
org_id: {
type: String,
required: true
},
api_key: {
type: String,
required: true
},
});

module.exports = mongoose.model('OpenAIConfig', openaiconfigSchema);
20 changes: 20 additions & 0 deletions lab/models/openailog.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const mongoose = require('mongoose');

const openailogSchema = new mongoose.Schema({
request: {
type: mongoose.Schema.Types.Mixed,
required: true
},
response: {
type: mongoose.Schema.Types.Mixed,
required: true
},
requestDate: {
type: Date,
required: true,
default: Date.now
}
});


module.exports = mongoose.model('OpenAILog', openailogSchema);
56 changes: 56 additions & 0 deletions lab/openaiutils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@

const OpenAILog = require('./models/openailog');
const OpenAIConfig = require('./models/openaiconfig');7
const db = require('./dbgoose').db;
const Chat = require('./models/chat');


async function getConfigById(req, res, next) {
let config;
try {
config = await OpenAIConfig.findById(req.params.id);
if (config == null) {
return res.status(404).json({ message: 'Cannot find config' });
}
} catch (err) {
return res.status(500).json({ message: err.message });
}

res.config = config;
next();
}

async function logOpenAIRequest(params, response) {
const openailog = new OpenAILog({
request: params,
response: response
});
try {
const newLog = await openailog.save();
console.log('openai log saved');
} catch (err) {
console.log('error: openai log not saved');
}
// call our API to log the request to our server here.
}

async function getChatById(req, res, next) {
let chat;
try {
chat = await Chat.findById(req.params.id);
if (chat == null) {
return res.status(404).json({ message: 'Cannot find chat' });
}
} catch (err) {
return res.status(500).json({ message: err.message });
}

res.chat = chat;
next();
}

module.exports = {
getConfigById,
logOpenAIRequest,
getChatById
};
2 changes: 2 additions & 0 deletions lab/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@
"lodash": "^4.17.21",
"mongodb": "^2.2.35",
"mongoskin": "^2.1.0",
"mongoose": "^6.0.5",
"morgan": "^1.10.0",
"multer": "^1.4.2",
"openai": "^3.0.0",
"request": "^2.88.2",
"request-promise": "^4.2.5",
"serve-favicon": "^2.3.0",
Expand Down
Loading

0 comments on commit f6184ff

Please sign in to comment.