Normalize lemonade embedder error handling (#5547)

closes #5318
resolves #5305
This commit is contained in:
Timothy Carambat 2026-04-28 10:59:14 -07:00 committed by GitHub
parent c8113aa188
commit b5f29f6dca
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -28,17 +28,10 @@ class LemonadeEmbedder {
} }
async embedTextInput(textInput) { async embedTextInput(textInput) {
try { const result = await this.embedChunks(
this.log(`Embedding text input...`); Array.isArray(textInput) ? textInput : [textInput]
const response = await this.lemonade.embeddings.create({ );
model: this.model, return result?.[0] || [];
input: textInput,
});
return response?.data[0]?.embedding || [];
} catch (error) {
this.log("Failed to get embedding from Lemonade.", error.message);
throw error;
}
} }
async embedChunks(textChunks = []) { async embedChunks(textChunks = []) {
@ -48,25 +41,56 @@ class LemonadeEmbedder {
const allResults = []; const allResults = [];
for (const chunk of toChunks(textChunks, this.maxConcurrentChunks)) { for (const chunk of toChunks(textChunks, this.maxConcurrentChunks)) {
try { const { data = [], error = null } = await new Promise((resolve) => {
const response = await this.lemonade.embeddings.create({ this.lemonade.embeddings
model: this.model, .create({
input: chunk, model: this.model,
}); input: chunk,
})
.then((result) => {
if (result?.error) {
const errMsg =
result.error?.details?.response?.error?.message ||
result.error?.message ||
"Unknown error";
const errType =
result.error?.details?.response?.error?.type ||
result.error?.type ||
"api_error";
resolve({
data: [],
error: { type: errType, message: errMsg },
});
return;
}
resolve({ data: result?.data, error: null });
})
.catch((e) => {
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 });
});
});
const embeddings = response?.data?.map((emb) => emb.embedding) || []; if (error) {
if (embeddings.length === 0) const errorMsg = `Lemonade Failed to embed: [${error.type}]: ${error.message}`;
throw new Error("Lemonade returned empty embeddings for batch"); this.log(errorMsg);
throw new Error(errorMsg);
allResults.push(...embeddings);
reportEmbeddingProgress(allResults.length, textChunks.length);
} catch (error) {
this.log("Failed to get embeddings from Lemonade.", error.message);
throw new Error(`Lemonade Failed to embed: ${error.message}`);
} }
allResults.push(...(data || []));
reportEmbeddingProgress(
Math.min(allResults.length, textChunks.length),
textChunks.length
);
} }
return allResults.length > 0 ? allResults : null; return allResults.length > 0 &&
allResults.every((embd) => embd.hasOwnProperty("embedding"))
? allResults.map((embd) => embd.embedding)
: null;
} }
} }