Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add anthropic local embedding + misc #3474

Merged
merged 3 commits into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/plugin-anthropic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
39 changes: 37 additions & 2 deletions packages/plugin-anthropic/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -82,12 +83,46 @@ export const anthropicPlugin: Plugin = {
stopSequences,
});
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 });
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) => {
Expand Down Expand Up @@ -117,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;
}
}
Expand Down
12 changes: 12 additions & 0 deletions packages/plugin-openai/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
Loading