2023-12-08 01:27:36 +01:00
|
|
|
const { toChunks, maximumChunkLength } = require("../../helpers");
|
2023-11-14 22:49:31 +01:00
|
|
|
|
|
|
|
class LocalAiEmbedder {
|
|
|
|
constructor() {
|
|
|
|
if (!process.env.EMBEDDING_BASE_PATH)
|
|
|
|
throw new Error("No embedding base path was set.");
|
|
|
|
if (!process.env.EMBEDDING_MODEL_PREF)
|
|
|
|
throw new Error("No embedding model was set.");
|
2024-04-30 21:33:42 +02:00
|
|
|
|
|
|
|
const { OpenAI: OpenAIApi } = require("openai");
|
|
|
|
this.openai = new OpenAIApi({
|
|
|
|
baseURL: process.env.EMBEDDING_BASE_PATH,
|
|
|
|
apiKey: process.env.LOCAL_AI_API_KEY ?? null,
|
2023-11-14 22:49:31 +01:00
|
|
|
});
|
|
|
|
|
2023-12-20 01:20:34 +01:00
|
|
|
// Limit of how many strings we can process in a single pass to stay with resource or network limits
|
2023-12-20 20:20:40 +01:00
|
|
|
this.maxConcurrentChunks = 50;
|
2023-12-08 01:27:36 +01:00
|
|
|
this.embeddingMaxChunkLength = maximumChunkLength();
|
2023-11-14 22:49:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async embedTextInput(textInput) {
|
2024-04-30 19:11:56 +02:00
|
|
|
const result = await this.embedChunks(
|
|
|
|
Array.isArray(textInput) ? textInput : [textInput]
|
|
|
|
);
|
2023-11-14 22:49:31 +01:00
|
|
|
return result?.[0] || [];
|
|
|
|
}
|
|
|
|
|
|
|
|
async embedChunks(textChunks = []) {
|
|
|
|
const embeddingRequests = [];
|
2023-12-20 20:20:40 +01:00
|
|
|
for (const chunk of toChunks(textChunks, this.maxConcurrentChunks)) {
|
2023-11-14 22:49:31 +01:00
|
|
|
embeddingRequests.push(
|
|
|
|
new Promise((resolve) => {
|
2024-04-30 21:33:42 +02:00
|
|
|
this.openai.embeddings
|
|
|
|
.create({
|
2023-11-14 22:49:31 +01:00
|
|
|
model: process.env.EMBEDDING_MODEL_PREF,
|
|
|
|
input: chunk,
|
|
|
|
})
|
2024-04-30 21:33:42 +02:00
|
|
|
.then((result) => {
|
|
|
|
resolve({ data: result?.data, error: null });
|
2023-11-14 22:49:31 +01:00
|
|
|
})
|
|
|
|
.catch((e) => {
|
2024-01-18 20:40:48 +01:00
|
|
|
e.type =
|
|
|
|
e?.response?.data?.error?.code ||
|
|
|
|
e?.response?.status ||
|
|
|
|
"failed_to_embed";
|
|
|
|
e.message = e?.response?.data?.error?.message || e.message;
|
|
|
|
resolve({ data: [], error: e });
|
2023-11-14 22:49:31 +01:00
|
|
|
});
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const { data = [], error = null } = await Promise.all(
|
|
|
|
embeddingRequests
|
|
|
|
).then((results) => {
|
|
|
|
// If any errors were returned from LocalAI abort the entire sequence because the embeddings
|
|
|
|
// will be incomplete.
|
|
|
|
const errors = results
|
|
|
|
.filter((res) => !!res.error)
|
|
|
|
.map((res) => res.error)
|
|
|
|
.flat();
|
|
|
|
if (errors.length > 0) {
|
2024-01-18 20:40:48 +01:00
|
|
|
let uniqueErrors = new Set();
|
|
|
|
errors.map((error) =>
|
|
|
|
uniqueErrors.add(`[${error.type}]: ${error.message}`)
|
|
|
|
);
|
|
|
|
|
2023-11-14 22:49:31 +01:00
|
|
|
return {
|
|
|
|
data: [],
|
2024-01-18 20:40:48 +01:00
|
|
|
error: Array.from(uniqueErrors).join(", "),
|
2023-11-14 22:49:31 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
data: results.map((res) => res?.data || []).flat(),
|
|
|
|
error: null,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!!error) throw new Error(`LocalAI Failed to embed: ${error}`);
|
|
|
|
return data.length > 0 &&
|
|
|
|
data.every((embd) => embd.hasOwnProperty("embedding"))
|
|
|
|
? data.map((embd) => embd.embedding)
|
|
|
|
: null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
LocalAiEmbedder,
|
|
|
|
};
|