diff --git a/packages/usdk/packages/upstreet-agent/packages/elizaos-core-proxy/embedding.ts b/packages/usdk/packages/upstreet-agent/packages/elizaos-core-proxy/embedding.ts index 5b7f6585d..1c35bc1a8 100644 --- a/packages/usdk/packages/upstreet-agent/packages/elizaos-core-proxy/embedding.ts +++ b/packages/usdk/packages/upstreet-agent/packages/elizaos-core-proxy/embedding.ts @@ -221,16 +221,16 @@ export async function embed(runtime: IAgentRuntime, input: string) { } // BGE - try local first if in Node - if (isNode) { - try { - return await getLocalEmbedding(input); - } catch (error) { - elizaLogger.warn( - "Local embedding failed, falling back to remote", - error - ); - } - } + // if (isNode) { + // try { + // return await getLocalEmbedding(input); + // } catch (error) { + // elizaLogger.warn( + // "Local embedding failed, falling back to remote", + // error + // ); + // } + // } // Fallback to remote override return await getRemoteEmbedding(input, { @@ -242,148 +242,148 @@ export async function embed(runtime: IAgentRuntime, input: string) { dimensions: config.dimensions, }); - async function getLocalEmbedding(input: string): Promise { - elizaLogger.debug("DEBUG - Inside getLocalEmbedding function"); - - // Check if we're in Node.js environment - const isNode = - typeof process !== "undefined" && - process.versions != null && - process.versions.node != null; - - if (!isNode) { - elizaLogger.warn( - "Local embedding not supported in browser, falling back to remote embedding" - ); - throw new Error("Local embedding not supported in browser"); - } - - try { - const moduleImports = await Promise.all([ - import("fs"), - import("url"), - (async () => { - try { - return await import("fastembed"); - } catch { - elizaLogger.error("Failed to load fastembed."); - throw new Error( - "fastembed import failed, falling back to remote embedding" - ); - } - })(), - ]); - - const [fs, { lol }, fastEmbed] = moduleImports; - const { FlagEmbedding, EmbeddingModel } = fastEmbed; - - function getRootPath() { - const __filename = fileURLToPath(import.meta.url); - const __dirname = path.dirname(__filename); - - const rootPath = path.resolve(__dirname, ".."); - if (rootPath.includes("/eliza/")) { - return rootPath.split("/eliza/")[0] + "/eliza/"; - } - - return path.resolve(__dirname, ".."); - } - - const cacheDir = getRootPath() + "/cache/"; - - if (!fs.existsSync(cacheDir)) { - fs.mkdirSync(cacheDir, { recursive: true }); - } - - elizaLogger.debug("Initializing BGE embedding model..."); - - const embeddingModel = await FlagEmbedding.init({ - cacheDir: cacheDir, - model: EmbeddingModel.BGESmallENV15, - // BGE-small-en-v1.5 specific settings - maxLength: 512, // BGE's context window - }); - - elizaLogger.debug("Generating embedding for input:", { - inputLength: input.length, - inputPreview: input.slice(0, 100) + "...", - }); - - // Let fastembed handle tokenization internally - const embedding = await embeddingModel.queryEmbed(input); - - // Debug the raw embedding - elizaLogger.debug("Raw embedding from BGE:", { - type: typeof embedding, - isArray: Array.isArray(embedding), - dimensions: Array.isArray(embedding) - ? embedding.length - : "not an array", - sample: Array.isArray(embedding) - ? embedding.slice(0, 5) - : embedding, - }); - - // Process the embedding into the correct format - let finalEmbedding: number[]; - - if ( - ArrayBuffer.isView(embedding) && - embedding.constructor === Float32Array - ) { - // Direct Float32Array result - finalEmbedding = Array.from(embedding); - } else if ( - Array.isArray(embedding) && - ArrayBuffer.isView(embedding[0]) && - embedding[0].constructor === Float32Array - ) { - // Nested Float32Array result - finalEmbedding = Array.from(embedding[0]); - } else if (Array.isArray(embedding)) { - // Direct array result - finalEmbedding = embedding; - } else { - throw new Error( - `Unexpected embedding format: ${typeof embedding}` - ); - } - - elizaLogger.debug("Processed embedding:", { - length: finalEmbedding.length, - sample: finalEmbedding.slice(0, 5), - allNumbers: finalEmbedding.every((n) => typeof n === "number"), - }); - - // Ensure all values are proper numbers - finalEmbedding = finalEmbedding.map((n) => Number(n)); - - // Validate the final embedding - if ( - !Array.isArray(finalEmbedding) || - finalEmbedding[0] === undefined - ) { - throw new Error( - "Invalid embedding format: must be an array starting with a number" - ); - } - - // Validate embedding dimensions (should be 384 for BGE-small) - if (finalEmbedding.length !== 384) { - elizaLogger.warn( - `Unexpected embedding dimension: ${finalEmbedding.length} (expected 384)` - ); - } - - return finalEmbedding; - } catch { - // Browser implementation - fallback to remote embedding - elizaLogger.warn( - "Local embedding not supported in browser, falling back to remote embedding" - ); - throw new Error("Local embedding not supported in browser"); - } - } + // async function getLocalEmbedding(input: string): Promise { + // elizaLogger.debug("DEBUG - Inside getLocalEmbedding function"); + + // // Check if we're in Node.js environment + // const isNode = + // typeof process !== "undefined" && + // process.versions != null && + // process.versions.node != null; + + // if (!isNode) { + // elizaLogger.warn( + // "Local embedding not supported in browser, falling back to remote embedding" + // ); + // throw new Error("Local embedding not supported in browser"); + // } + + // try { + // const moduleImports = await Promise.all([ + // import("fs"), + // import("url"), + // (async () => { + // try { + // return await import("fastembed"); + // } catch { + // elizaLogger.error("Failed to load fastembed."); + // throw new Error( + // "fastembed import failed, falling back to remote embedding" + // ); + // } + // })(), + // ]); + + // const [fs, { fileURLToPath }, fastEmbed] = moduleImports; + // const { FlagEmbedding, EmbeddingModel } = fastEmbed; + + // function getRootPath() { + // const __filename = fileURLToPath(import.meta.url); + // const __dirname = path.dirname(__filename); + + // const rootPath = path.resolve(__dirname, ".."); + // if (rootPath.includes("/eliza/")) { + // return rootPath.split("/eliza/")[0] + "/eliza/"; + // } + + // return path.resolve(__dirname, ".."); + // } + + // const cacheDir = getRootPath() + "/cache/"; + + // if (!fs.existsSync(cacheDir)) { + // fs.mkdirSync(cacheDir, { recursive: true }); + // } + + // elizaLogger.debug("Initializing BGE embedding model..."); + + // const embeddingModel = await FlagEmbedding.init({ + // cacheDir: cacheDir, + // model: EmbeddingModel.BGESmallENV15, + // // BGE-small-en-v1.5 specific settings + // maxLength: 512, // BGE's context window + // }); + + // elizaLogger.debug("Generating embedding for input:", { + // inputLength: input.length, + // inputPreview: input.slice(0, 100) + "...", + // }); + + // // Let fastembed handle tokenization internally + // const embedding = await embeddingModel.queryEmbed(input); + + // // Debug the raw embedding + // elizaLogger.debug("Raw embedding from BGE:", { + // type: typeof embedding, + // isArray: Array.isArray(embedding), + // dimensions: Array.isArray(embedding) + // ? embedding.length + // : "not an array", + // sample: Array.isArray(embedding) + // ? embedding.slice(0, 5) + // : embedding, + // }); + + // // Process the embedding into the correct format + // let finalEmbedding: number[]; + + // if ( + // ArrayBuffer.isView(embedding) && + // embedding.constructor === Float32Array + // ) { + // // Direct Float32Array result + // finalEmbedding = Array.from(embedding); + // } else if ( + // Array.isArray(embedding) && + // ArrayBuffer.isView(embedding[0]) && + // embedding[0].constructor === Float32Array + // ) { + // // Nested Float32Array result + // finalEmbedding = Array.from(embedding[0]); + // } else if (Array.isArray(embedding)) { + // // Direct array result + // finalEmbedding = embedding; + // } else { + // throw new Error( + // `Unexpected embedding format: ${typeof embedding}` + // ); + // } + + // elizaLogger.debug("Processed embedding:", { + // length: finalEmbedding.length, + // sample: finalEmbedding.slice(0, 5), + // allNumbers: finalEmbedding.every((n) => typeof n === "number"), + // }); + + // // Ensure all values are proper numbers + // finalEmbedding = finalEmbedding.map((n) => Number(n)); + + // // Validate the final embedding + // if ( + // !Array.isArray(finalEmbedding) || + // finalEmbedding[0] === undefined + // ) { + // throw new Error( + // "Invalid embedding format: must be an array starting with a number" + // ); + // } + + // // Validate embedding dimensions (should be 384 for BGE-small) + // if (finalEmbedding.length !== 384) { + // elizaLogger.warn( + // `Unexpected embedding dimension: ${finalEmbedding.length} (expected 384)` + // ); + // } + + // return finalEmbedding; + // } catch { + // // Browser implementation - fallback to remote embedding + // elizaLogger.warn( + // "Local embedding not supported in browser, falling back to remote embedding" + // ); + // throw new Error("Local embedding not supported in browser"); + // } + // } async function retrieveCachedEmbedding( runtime: IAgentRuntime,