@inductiv/node-red-openai-api
Version:
Enhance your Node-RED projects with advanced AI capabilities.
114 lines (94 loc) • 3.03 kB
JavaScript
const OpenAI = require("openai").OpenAI;
const fs = require("fs");
async function createVectorStoreFileBatch(parameters) {
const openai = new OpenAI(this.clientParams);
const { vector_store_id, ...params } = parameters.payload;
const response = await openai.vectorStores.fileBatches.create(
vector_store_id,
params
);
return response;
}
async function createAndPollVectorStoreFileBatch(parameters) {
const openai = new OpenAI(this.clientParams);
const { vector_store_id, pollIntervalMs, ...body } = parameters.payload;
const response = await openai.vectorStores.fileBatches.createAndPoll(
vector_store_id,
body,
{ pollIntervalMs }
);
return response;
}
async function retrieveVectorStoreFileBatch(parameters) {
const openai = new OpenAI(this.clientParams);
const { vector_store_id, batch_id, ...params } = parameters.payload;
const response = await openai.vectorStores.fileBatches.retrieve(batch_id, {
vector_store_id,
...params,
});
return response;
}
async function pollVectorStoreFileBatch(parameters) {
const openai = new OpenAI(this.clientParams);
const { vector_store_id, batch_id, pollIntervalMs } = parameters.payload;
const response = await openai.vectorStores.fileBatches.poll(
vector_store_id,
batch_id,
{ pollIntervalMs }
);
return response;
}
async function cancelVectorStoreFileBatch(parameters) {
const openai = new OpenAI(this.clientParams);
const { vector_store_id, batch_id, ...params } = parameters.payload;
const response = await openai.vectorStores.fileBatches.cancel(batch_id, {
vector_store_id,
...params,
});
return response;
}
async function listVectorStoreBatchFiles(parameters) {
const openai = new OpenAI(this.clientParams);
const { vector_store_id, batch_id, ...params } = parameters.payload;
const list = await openai.vectorStores.fileBatches.listFiles(batch_id, {
vector_store_id,
...params,
});
const batchFiles = [...list.data];
return batchFiles;
}
async function uploadAndPollVectorStoreFileBatch(parameters) {
const openai = new OpenAI(this.clientParams);
const {
vector_store_id,
files,
file_ids,
pollIntervalMs,
maxConcurrency,
} = parameters.payload;
if (!files || !Array.isArray(files)) {
throw new Error("Files is not defined or not an array");
}
// Validate file paths
files.forEach((path) => {
if (!fs.existsSync(path)) {
throw new Error(`File does not exist: ${path}`);
}
});
const fileStreams = files.map((path) => fs.createReadStream(path));
const response = await openai.vectorStores.fileBatches.uploadAndPoll(
vector_store_id,
{ files: fileStreams, fileIds: file_ids },
{ pollIntervalMs, maxConcurrency }
);
return response;
}
module.exports = {
createVectorStoreFileBatch,
createAndPollVectorStoreFileBatch,
retrieveVectorStoreFileBatch,
pollVectorStoreFileBatch,
cancelVectorStoreFileBatch,
listVectorStoreBatchFiles,
uploadAndPollVectorStoreFileBatch,
};