MCPcopy
hub / github.com/CaviraOSS/OpenMemory / emb_batch_with_fallback

Function emb_batch_with_fallback

backend/src/memory/embed.ts:186–242  ·  view source on GitHub ↗
(
    txts: Record<string, string>,
)

Source from the content-addressed store, hash-verified

184
185// Batch embedding with fallback chain support (for simple mode)
186async function emb_batch_with_fallback(
187 txts: Record<string, string>,
188): Promise<Record<string, number[]>> {
189 const providers = [...new Set([env.emb_kind, ...env.embedding_fallback])];
190
191 for (let i = 0; i < providers.length; i++) {
192 const provider = providers[i];
193 try {
194 let result: Record<string, number[]>;
195 switch (provider) {
196 case "gemini":
197 result = await emb_gemini(txts);
198 break;
199 case "openai":
200 result = await emb_batch_openai(txts);
201 break;
202 default:
203 // For providers without batch support, embed each sector individually
204 result = {};
205 for (const [s, t] of Object.entries(txts)) {
206 result[s] = await embed_with_provider(provider, t, s);
207 }
208 }
209 if (i > 0) {
210 console.log(
211 `[EMBED] Fallback to ${provider} succeeded for batch`,
212 );
213 }
214 return result;
215 } catch (e) {
216 const errMsg = e instanceof Error ? e.message : String(e);
217 const nextProvider = providers[i + 1];
218
219 if (nextProvider) {
220 console.warn(
221 `[EMBED] ${provider} batch failed: ${errMsg}, trying ${nextProvider}`,
222 );
223 } else {
224 console.error(
225 `[EMBED] All providers failed for batch. Last error (${provider}): ${errMsg}. Using synthetic.`,
226 );
227 // Fall back to synthetic for all sectors
228 const result: Record<string, number[]> = {};
229 for (const [s, t] of Object.entries(txts)) {
230 result[s] = gen_syn_emb(t, s);
231 }
232 return result;
233 }
234 }
235 }
236 // Fallback if providers array is empty
237 const result: Record<string, number[]> = {};
238 for (const [s, t] of Object.entries(txts)) {
239 result[s] = gen_syn_emb(t, s);
240 }
241 return result;
242}
243

Callers 1

embedMultiSectorFunction · 0.85

Calls 4

embed_with_providerFunction · 0.85
emb_geminiFunction · 0.70
emb_batch_openaiFunction · 0.70
gen_syn_embFunction · 0.70

Tested by

no test coverage detected