-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #580 from jay-m-dev/openai_connection
Openai connection
- Loading branch information
Showing
13 changed files
with
580 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
### |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.