Skip to content

Commit

Permalink
Merge branch 'v2-develop' into tcm/discord-test
Browse files Browse the repository at this point in the history
  • Loading branch information
tcm390 authored Feb 13, 2025
2 parents 53d85aa + 6d7f53e commit af93b5f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
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

0 comments on commit af93b5f

Please sign in to comment.