mindcraft/utils/gpt.js

67 lines
2 KiB
JavaScript
Raw Normal View History

2023-09-04 17:01:04 -05:00
import OpenAIApi from 'openai';
2023-08-15 23:39:02 -07:00
2023-09-04 17:01:04 -05:00
let openAiConfig = null;
2023-08-15 23:39:02 -07:00
if (process.env.OPENAI_ORG_ID) {
2023-09-04 17:01:04 -05:00
openAiConfig = {
2023-08-15 23:39:02 -07:00
organization: process.env.OPENAI_ORG_ID,
apiKey: process.env.OPENAI_API_KEY,
2023-09-04 17:01:04 -05:00
};
2023-08-15 23:39:02 -07:00
} else {
2023-09-04 17:01:04 -05:00
openAiConfig = {
2023-08-15 23:39:02 -07:00
apiKey: process.env.OPENAI_API_KEY,
2023-09-04 17:01:04 -05:00
};
2023-08-15 23:39:02 -07:00
}
const openai = new OpenAIApi(openAiConfig);
export async function sendRequest(turns, systemMessage, stop_seq='***') {
2023-08-15 23:39:02 -07:00
let messages = [{'role': 'system', 'content': systemMessage}].concat(turns);
2023-08-15 23:39:02 -07:00
let res = null;
try {
2023-11-19 15:34:53 -06:00
console.log('Awaiting openai api response...')
2023-09-04 17:01:04 -05:00
let completion = await openai.chat.completions.create({
2023-11-19 15:34:53 -06:00
model: 'gpt-3.5-turbo',
2023-08-15 23:39:02 -07:00
messages: messages,
2023-08-17 00:00:57 -07:00
stop: stop_seq,
2023-08-15 23:39:02 -07:00
});
2023-11-19 15:34:53 -06:00
console.log('Received.')
2023-09-04 17:01:04 -05:00
res = completion.choices[0].message.content;
2023-08-15 23:39:02 -07:00
}
catch (err) {
2023-11-12 14:53:23 -08:00
if (err.code == 'context_length_exceeded' && turns.length > 1) {
console.log('Context length exceeded, trying again with shorter context.');
return await sendRequest(turns.slice(1), systemMessage, stop_seq);
} else {
console.log(err);
res = 'My brain disconnected, try again.';
}
2023-08-15 23:39:02 -07:00
}
return res;
}
2023-11-15 13:18:38 -08:00
export async function embed(text) {
const embedding = await openai.embeddings.create({
model: "text-embedding-ada-002",
input: text,
encoding_format: "float",
});
return embedding.data[0].embedding;
}
2023-11-15 18:53:02 -06:00
export function cosineSimilarity(a, b) {
let dotProduct = 0;
let magnitudeA = 0;
let magnitudeB = 0;
for (let i = 0; i < a.length; i++) {
dotProduct += a[i] * b[i]; // calculate dot product
magnitudeA += Math.pow(a[i], 2); // calculate magnitude of a
magnitudeB += Math.pow(b[i], 2); // calculate magnitude of b
}
magnitudeA = Math.sqrt(magnitudeA);
magnitudeB = Math.sqrt(magnitudeB);
return dotProduct / (magnitudeA * magnitudeB); // calculate cosine similarity
}