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

Function embedMultiSector

backend/src/memory/embed.ts:543–616  ·  view source on GitHub ↗
(
    id: string,
    txt: string,
    secs: string[],
    chunks?: Array<{ text: string }>,
)

Source from the content-addressed store, hash-verified

541};
542
543export async function embedMultiSector(
544 id: string,
545 txt: string,
546 secs: string[],
547 chunks?: Array<{ text: string }>,
548): Promise<EmbeddingResult[]> {
549 const r: EmbeddingResult[] = [];
550 await q.ins_log.run(id, "multi-sector", "pending", Date.now(), null);
551 for (let a = 0; a < 3; a++) {
552 try {
553 const simp = env.embed_mode === "simple";
554 if (
555 simp &&
556 (env.emb_kind === "gemini" || env.emb_kind === "openai")
557 ) {
558 console.log(
559 `[EMBED] Simple mode (1 batch for ${secs.length} sectors)`,
560 );
561 const tb: Record<string, string> = {};
562 secs.forEach((s) => (tb[s] = txt));
563 // Use batch embedding with fallback support
564 const b = await emb_batch_with_fallback(tb);
565 Object.entries(b).forEach(([s, v]) =>
566 r.push({ sector: s, vector: v, dim: v.length }),
567 );
568 } else {
569 console.log(`[EMBED] Advanced mode (${secs.length} calls)`);
570 const par = env.adv_embed_parallel && env.emb_kind !== "gemini";
571 if (par) {
572 const p = secs.map(async (s) => {
573 let v: number[];
574 if (chunks && chunks.length > 1) {
575 const cv: number[][] = [];
576 for (const c of chunks)
577 cv.push(await embedForSector(c.text, s));
578 v = agg_chunks(cv);
579 } else v = await embedForSector(txt, s);
580 return { sector: s, vector: v, dim: v.length };
581 });
582 r.push(...(await Promise.all(p)));
583 } else {
584 for (let i = 0; i < secs.length; i++) {
585 const s = secs[i];
586 let v: number[];
587 if (chunks && chunks.length > 1) {
588 const cv: number[][] = [];
589 for (const c of chunks)
590 cv.push(await embedForSector(c.text, s));
591 v = agg_chunks(cv);
592 } else v = await embedForSector(txt, s);
593 r.push({ sector: s, vector: v, dim: v.length });
594 if (env.embed_delay_ms > 0 && i < secs.length - 1)
595 await new Promise((x) =>
596 setTimeout(x, env.embed_delay_ms),
597 );
598 }
599 }
600 }

Callers 2

add_hsg_memoryFunction · 0.90
update_memoryFunction · 0.90

Calls 5

emb_batch_with_fallbackFunction · 0.85
embedForSectorFunction · 0.70
agg_chunksFunction · 0.70
runMethod · 0.45
allMethod · 0.45

Tested by

no test coverage detected