From 1a5cd640f970b3b3baf84479edcade58fb483868 Mon Sep 17 00:00:00 2001 From: Sayo Date: Thu, 13 Feb 2025 19:13:17 +0530 Subject: [PATCH 1/3] added test for openai, anthropic embeding, added fastembed for anthropic --- packages/plugin-anthropic/package.json | 1 + packages/plugin-anthropic/src/index.ts | 32 ++++++++++++++++++++++++++ packages/plugin-openai/src/index.ts | 12 ++++++++++ 3 files changed, 45 insertions(+) diff --git a/packages/plugin-anthropic/package.json b/packages/plugin-anthropic/package.json index 7e97ce362e1..9cd965e40f5 100644 --- a/packages/plugin-anthropic/package.json +++ b/packages/plugin-anthropic/package.json @@ -19,6 +19,7 @@ ], "dependencies": { "@ai-sdk/anthropic": "^1.1.6", + "fastembed": "^1.0.0", "@elizaos/core": "workspace:*", "tsup": "8.3.5", "zod": "3.21.4" diff --git a/packages/plugin-anthropic/src/index.ts b/packages/plugin-anthropic/src/index.ts index 62ca07e73ed..d2bbbb53403 100644 --- a/packages/plugin-anthropic/src/index.ts +++ b/packages/plugin-anthropic/src/index.ts @@ -6,6 +6,7 @@ import { } from "@elizaos/core"; import { generateText } from "ai"; import { z } from "zod"; +import { EmbeddingModel, FlagEmbedding } from "fastembed"; // Define a configuration schema for the Anthropics plugin. const configSchema = z.object({ @@ -82,12 +83,43 @@ export const anthropicPlugin: Plugin = { stopSequences, }); return text; + }, + [ModelClass.TEXT_EMBEDDING]: async (runtime, text: string | null) => { + if (!text) return new Array(1536).fill(0); + + const model = await FlagEmbedding.init({ model: EmbeddingModel.BGESmallENV15 }); + const embedding = await model.queryEmbed(text); + + const finalEmbedding = Array.isArray(embedding) + ? ArrayBuffer.isView(embedding[0]) + ? Array.from(embedding[0] as never) + : embedding + : Array.from(embedding); + + if (!Array.isArray(finalEmbedding) || finalEmbedding[0] === undefined) { + throw new Error("Invalid embedding format"); + } + + return finalEmbedding.map(Number); } }, tests: [ { name: "anthropic_plugin_tests", tests: [ + { + name: 'anthropic_test_text_embedding', + fn: async (runtime) => { + try { + console.log("testing embedding"); + const embedding = await runtime.useModel(ModelClass.TEXT_EMBEDDING, "Hello, world!"); + console.log("embedding done", embedding); + } catch (error) { + console.error("Error in test_text_embedding:", error); + throw error; + } + } + }, { name: 'anthropic_test_text_small', fn: async (runtime) => { diff --git a/packages/plugin-openai/src/index.ts b/packages/plugin-openai/src/index.ts index bfc795010c2..87888799bde 100644 --- a/packages/plugin-openai/src/index.ts +++ b/packages/plugin-openai/src/index.ts @@ -310,6 +310,18 @@ export const openaiPlugin: Plugin = { } } }, + { + name: 'openai_test_text_embedding', + fn: async (runtime) => { + try { + const embedding = await runtime.useModel(ModelClass.TEXT_EMBEDDING, "Hello, world!"); + console.log("embedding", embedding); + } catch (error) { + console.error("Error in test_text_embedding:", error); + throw error; + } + } + }, { name: 'openai_test_text_large', fn: async (runtime) => { From a314c48930fe78d1e2cd2a4095286abcd77f6e56 Mon Sep 17 00:00:00 2001 From: Sayo Date: Thu, 13 Feb 2025 19:14:02 +0530 Subject: [PATCH 2/3] add todo flags added todo flag --- packages/plugin-anthropic/src/index.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/plugin-anthropic/src/index.ts b/packages/plugin-anthropic/src/index.ts index d2bbbb53403..869f3813582 100644 --- a/packages/plugin-anthropic/src/index.ts +++ b/packages/plugin-anthropic/src/index.ts @@ -85,6 +85,9 @@ export const anthropicPlugin: Plugin = { return text; }, [ModelClass.TEXT_EMBEDDING]: async (runtime, text: string | null) => { + + // TODO: Make this fallback only!!! + // TODO: pass on cacheDir to FlagEmbedding.init if (!text) return new Array(1536).fill(0); const model = await FlagEmbedding.init({ model: EmbeddingModel.BGESmallENV15 }); From c64ee09ddebbd34609e5f31e2900db73018c3d1d Mon Sep 17 00:00:00 2001 From: Sayo Date: Thu, 13 Feb 2025 19:20:49 +0530 Subject: [PATCH 3/3] fix semantics --- packages/plugin-anthropic/src/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/plugin-anthropic/src/index.ts b/packages/plugin-anthropic/src/index.ts index 869f3813582..6f7e5576020 100644 --- a/packages/plugin-anthropic/src/index.ts +++ b/packages/plugin-anthropic/src/index.ts @@ -152,9 +152,9 @@ export const anthropicPlugin: Plugin = { if (text.length === 0) { throw new Error("Failed to generate text"); } - console.log("generated with test_text_small:", text); + console.log("generated with test_text_large:", text); } catch (error) { - console.error("Error in test_text_small:", error); + console.error("Error in test_text_large:", error); throw error; } }