-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopenaiController.js
183 lines (132 loc) · 4.39 KB
/
openaiController.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
const fetch = require('node-fetch');
const { Configuration, OpenAIApi } = require("openai");
var Airtable = require('airtable');
var base = new Airtable({apiKey: process.env.AIRTABLE_API_KEY}).base('app5eTobFcbCN5edO');
/// TEXT PARSING FUNCTIONS
function wordWrapResponse(text) {
const numCharacters = 18;
let searchCharacter = 18;
const numRows = Math.round(text.length / numCharacters);
let formattedText = text;
//adds string in a specific location
function addStr(str, index, stringToAdd){
return str.substring(0, index) + stringToAdd + str.substring(index, str.length);
}
// calculates spacing and returns a new string
function addWrapSpacing(text, spaces, insertNumber){
let newText = text;
if(spaces === 0 ){
newText = newText.slice(0, insertNumber) + newText.slice(insertNumber + 1);
formattedText = newText;
}
else{
for (let i = 0; i < (spaces - 1); i++) {
newText = addStr(newText, insertNumber, ' ');
}
formattedText = newText;
}
}
// checks if character at position 18 is a character, if it is, it calculates the length of the word wrap and returns a new string
function addWordBreak(text, searchNumber, originalSearchNumber){
// console.log(`searching at position ${searchNumber} with a value of ${text[searchNumber]}`)
if (text[searchNumber]){
if(text[searchNumber] === ' '){
const numberOfSpaces = originalSearchNumber - searchNumber;
addWrapSpacing(text, numberOfSpaces, searchNumber);
}
else{
searchNumber -=1;
addWordBreak(text, searchNumber, originalSearchNumber);
}
}
}
for (let i = 0; i < numRows; i++) {
addWordBreak(formattedText, searchCharacter, searchCharacter)
searchCharacter += numCharacters;
}
return formattedText;
}
/// OPEN AI API CALL ///
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
async function isContentSafe(text){
const moderation = await openai.createModeration({ input: text });
if(moderation.data.results[0].flagged === false){
return true
} else {
base('AI_INPUTS').create({ //AIRTABLE STUFF
"QUESTION": text,
"RESPONSE": "UNSAFE"
}, function(err, record) {
if (err) {
console.error(err);
return;
}
// console.log(record.getId());
});
return false
}
}
async function getEmbeddingData(text, res){
console.log('IM EMBEDDING FUNCTION', text)
const embeddingValue = await fetch('http://0.0.0.0:5000/embedding', {
method: 'POST', // or 'PUT'
body: text,
})
.then(response => response.json())
.catch((error) => {
console.error('Error:', error);
});
base('AI_INPUTS').create({ //AIRTABLE STUFF
"QUESTION": text,
"RESPONSE": `${embeddingValue.question.toUpperCase()} * ${embeddingValue.answer}`
}, function(err, record) {
if (err) {
console.error(err);
return;
}
// console.log(record.getId());
});
return embeddingValue
}
/// OPEN AI PROMPTS ///
//CONTENT FILTER
const contentFilter =(input)=>{
return `"<|endoftext|>[${input}]\n--\nLabel:"`
}
/// SCREEN SAVER MESSAGES ///
module.exports = {isContentSafe, wordWrapResponse, getEmbeddingData}
// const example = `I'm sorry, I don't know what you're talking about.`
// function padRight(text, max) {
// return text + ' '.repeat(max - text.length);
// }
// function redoSpaces(text, rowLength, totalLength) {
// const words = text.split(' ');
// const rows = [];
// let currRow = [];
// let currRowLength = 0;
// words.forEach(word => {
// if ((currRowLength + word.length + 1) <= rowLength) {
// currRowLength += word.length;
// currRow.push(word);
// } else {
// rows.push(currRow);
// currRow = [word];
// currRowLength = word.length;
// }
// });
// if (currRow.length) {
// rows.push(currRow);
// }
// const rowsWithSpaces = rows
// .map(row => row.join(' '))
// .map(rowAsString => padRight(rowAsString, rowLength));
// const asString = rowsWithSpaces.join('');
// return padRight(asString, totalLength);
// }
// const redone = redoSpaces(example, 18, 108);
// console.log('redone length', redone.length);
// const visual = redone.replace(/ /g, '^');
// console.log(`-->${visual}<--`);