Files
anything-llm/server/utils/EmbeddingEngines/lemonade/index.js
Marcello Fitton 4a4378ed99 chore: add ESLint to /server (#5126)
* add eslint config to server

* add break statements to switch case

* add support for browser globals and turn off empty catch blocks

* disable lines with useless try/catch wrappers

* format

* fix no-undef errors

* disbale lines violating no-unsafe-finally

* ignore syncStaticLists.mjs

* use proper null check for creatorId instead of unreachable nullish coalescing

* remove unneeded typescript eslint comment

* make no-unused-private-class-members a warning

* disable line for no-empty-objects

* add new lint script

* fix no-unused-vars violations

* make no-unsued-vars an error

---------

Co-authored-by: shatfield4 <seanhatfield5@gmail.com>
Co-authored-by: Timothy Carambat <rambat1010@gmail.com>
2026-03-05 16:32:45 -08:00

60 lines
1.7 KiB
JavaScript

const { parseLemonadeServerEndpoint } = require("../../AiProviders/lemonade");
class LemonadeEmbedder {
constructor() {
if (!process.env.EMBEDDING_BASE_PATH)
throw new Error("No Lemonade API Base Path was set.");
if (!process.env.EMBEDDING_MODEL_PREF)
throw new Error("No Embedding Model Pref was set.");
this.className = "LemonadeEmbedder";
const { OpenAI: OpenAIApi } = require("openai");
this.lemonade = new OpenAIApi({
baseURL: parseLemonadeServerEndpoint(
process.env.EMBEDDING_BASE_PATH,
"openai"
),
apiKey: null,
});
this.model = process.env.EMBEDDING_MODEL_PREF;
this.embeddingMaxChunkLength =
process.env.EMBEDDING_MODEL_MAX_CHUNK_LENGTH || 8_191;
}
log(text, ...args) {
console.log(`\x1b[36m[${this.className}]\x1b[0m ${text}`, ...args);
}
async embedTextInput(textInput) {
try {
this.log(`Embedding text input...`);
const response = await this.lemonade.embeddings.create({
model: this.model,
input: textInput,
});
return response?.data[0]?.embedding || [];
} catch (error) {
console.error("Failed to get embedding from Lemonade.", error.message);
return [];
}
}
async embedChunks(textChunks = []) {
try {
this.log(`Embedding ${textChunks.length} chunks of text...`);
const response = await this.lemonade.embeddings.create({
model: this.model,
input: textChunks,
});
return response?.data?.map((emb) => emb.embedding) || [];
} catch (error) {
console.error("Failed to get embeddings from Lemonade.", error.message);
return new Array(textChunks.length).fill([]);
}
}
}
module.exports = {
LemonadeEmbedder,
};