Skip to content

Commit

Permalink
Merge branch 'develop' into odi-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
twilwa authored Jan 14, 2025
2 parents f5aa3dc + ecfd42c commit 640f398
Show file tree
Hide file tree
Showing 31 changed files with 5,368 additions and 98 deletions.
6 changes: 6 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,12 @@ SMALL_ANTHROPIC_MODEL= # Default: claude-3-haiku-20240307
MEDIUM_ANTHROPIC_MODEL= # Default: claude-3-5-sonnet-20241022
LARGE_ANTHROPIC_MODEL= # Default: claude-3-5-sonnet-20241022

# VoyageAI Configuration
VOYAGEAI_API_KEY=
USE_VOYAGEAI_EMBEDDING= # Set to TRUE for VoyageAI, leave blank for local
VOYAGEAI_EMBEDDING_MODEL= # Default: voyage-3-lite
VOYAGEAI_EMBEDDING_DIMENSIONS= # Default: 512

# Heurist Configuration
HEURIST_API_KEY= # Get from https://heurist.ai/dev-access
SMALL_HEURIST_MODEL= # Default: meta-llama/llama-3-70b-instruct
Expand Down
4 changes: 3 additions & 1 deletion agent/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@
"@elizaos/plugin-aptos": "workspace:*",
"@elizaos/plugin-b2": "workspace:*",
"@elizaos/plugin-coinmarketcap": "workspace:*",
"@elizaos/plugin-birdeye": "workspace:*",
"@elizaos/plugin-coingecko": "workspace:*",
"@elizaos/plugin-coinmarketcap": "workspace:*",
"@elizaos/plugin-binance": "workspace:*",
"@elizaos/plugin-avail": "workspace:*",
"@elizaos/plugin-bootstrap": "workspace:*",
Expand Down Expand Up @@ -108,4 +110,4 @@
"ts-node": "10.9.2",
"tsup": "8.3.5"
}
}
}
36 changes: 19 additions & 17 deletions agent/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,16 @@ import createGoatPlugin from "@elizaos/plugin-goat";
import { DirectClient } from "@elizaos/client-direct";
import { ThreeDGenerationPlugin } from "@elizaos/plugin-3d-generation";
import { abstractPlugin } from "@elizaos/plugin-abstract";
import { akashPlugin } from "@elizaos/plugin-akash";
import { alloraPlugin } from "@elizaos/plugin-allora";
import { aptosPlugin } from "@elizaos/plugin-aptos";
import { artheraPlugin } from "@elizaos/plugin-arthera";
import { autonomePlugin } from "@elizaos/plugin-autonome";
import { availPlugin } from "@elizaos/plugin-avail";
import { avalanchePlugin } from "@elizaos/plugin-avalanche";
import { b2Plugin } from "@elizaos/plugin-b2";
import { binancePlugin } from "@elizaos/plugin-binance";
import { birdeyePlugin } from "@elizaos/plugin-birdeye";
import {
advancedTradePlugin,
coinbaseCommercePlugin,
Expand All @@ -56,8 +59,8 @@ import {
tradePlugin,
webhookPlugin,
} from "@elizaos/plugin-coinbase";
import { coinmarketcapPlugin } from "@elizaos/plugin-coinmarketcap";
import { coingeckoPlugin } from "@elizaos/plugin-coingecko";
import { coinmarketcapPlugin } from "@elizaos/plugin-coinmarketcap";
import { confluxPlugin } from "@elizaos/plugin-conflux";
import { createCosmosPlugin } from "@elizaos/plugin-cosmos";
import { cronosZkEVMPlugin } from "@elizaos/plugin-cronoszkevm";
Expand All @@ -66,45 +69,41 @@ import { evmPlugin } from "@elizaos/plugin-evm";
import { flowPlugin } from "@elizaos/plugin-flow";
import { fuelPlugin } from "@elizaos/plugin-fuel";
import { genLayerPlugin } from "@elizaos/plugin-genlayer";
import { giphyPlugin } from "@elizaos/plugin-giphy";
import { hyperliquidPlugin } from "@elizaos/plugin-hyperliquid";
import { imageGenerationPlugin } from "@elizaos/plugin-image-generation";
import { lensPlugin } from "@elizaos/plugin-lensNetwork";
import { letzAIPlugin } from "@elizaos/plugin-letzai";
import { multiversxPlugin } from "@elizaos/plugin-multiversx";
import { nearPlugin } from "@elizaos/plugin-near";
import createNFTCollectionsPlugin from "@elizaos/plugin-nft-collections";
import { nftGenerationPlugin } from "@elizaos/plugin-nft-generation";
import { createNodePlugin } from "@elizaos/plugin-node";
import { obsidianPlugin } from "@elizaos/plugin-obsidian";
import { OpacityAdapter } from "@elizaos/plugin-opacity";
import { openWeatherPlugin } from "@elizaos/plugin-open-weather";
import { quaiPlugin } from "@elizaos/plugin-quai";
import { sgxPlugin } from "@elizaos/plugin-sgx";
import { solanaPlugin } from "@elizaos/plugin-solana";
import { solanaAgentkitPlguin } from "@elizaos/plugin-solana-agentkit";
import { autonomePlugin } from "@elizaos/plugin-autonome";
import { stargazePlugin } from "@elizaos/plugin-stargaze";
import { storyPlugin } from "@elizaos/plugin-story";
import { suiPlugin } from "@elizaos/plugin-sui";
import { TEEMode, teePlugin } from "@elizaos/plugin-tee";
import { teeLogPlugin } from "@elizaos/plugin-tee-log";
import { teeMarlinPlugin } from "@elizaos/plugin-tee-marlin";
import { verifiableLogPlugin } from "@elizaos/plugin-tee-verifiable-log";
import { thirdwebPlugin } from "@elizaos/plugin-thirdweb";
import { tonPlugin } from "@elizaos/plugin-ton";
import { squidRouterPlugin } from "@elizaos/plugin-squid-router";
import { webSearchPlugin } from "@elizaos/plugin-web-search";

import { giphyPlugin } from "@elizaos/plugin-giphy";
import { letzAIPlugin } from "@elizaos/plugin-letzai";
import { thirdwebPlugin } from "@elizaos/plugin-thirdweb";
import { hyperliquidPlugin } from "@elizaos/plugin-hyperliquid";
import { zksyncEraPlugin } from "@elizaos/plugin-zksync-era";

import { OpacityAdapter } from "@elizaos/plugin-opacity";
import { openWeatherPlugin } from "@elizaos/plugin-open-weather";
import { stargazePlugin } from "@elizaos/plugin-stargaze";
import { akashPlugin } from "@elizaos/plugin-akash";
import { quaiPlugin } from "@elizaos/plugin-quai";
import Database from "better-sqlite3";
import fs from "fs";
import net from "net";
import path from "path";
import { fileURLToPath } from "url";
import yargs from "yargs";
import { verifiableLogPlugin } from "@elizaos/plugin-tee-verifiable-log";
import createNFTCollectionsPlugin from "@elizaos/plugin-nft-collections";

const __filename = fileURLToPath(import.meta.url); // get the resolved path to the file
const __dirname = path.dirname(__filename); // get the name of the directory
Expand Down Expand Up @@ -801,9 +800,11 @@ export async function createAgent(
]
: []),
...(teeMode !== TEEMode.OFF && walletSecretSalt ? [teePlugin] : []),
(teeMode !== TEEMode.OFF && walletSecretSalt &&getSecret(character,"VLOG")
teeMode !== TEEMode.OFF &&
walletSecretSalt &&
getSecret(character, "VLOG")
? verifiableLogPlugin
: null),
: null,
getSecret(character, "SGX") ? sgxPlugin : null,
getSecret(character, "ENABLE_TEE_LOG") &&
((teeMode !== TEEMode.OFF && walletSecretSalt) ||
Expand Down Expand Up @@ -859,6 +860,7 @@ export async function createAgent(
getSecret(character, "AVALANCHE_PRIVATE_KEY")
? avalanchePlugin
: null,
getSecret(character, "BIRDEYE_API_KEY") ? birdeyePlugin : null,
getSecret(character, "ECHOCHAMBERS_API_URL") &&
getSecret(character, "ECHOCHAMBERS_API_KEY")
? echoChambersPlugin
Expand Down
165 changes: 85 additions & 80 deletions packages/core/src/embedding.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { getEmbeddingModelSettings, getEndpoint } from "./models.ts";
import { IAgentRuntime, ModelProviderName } from "./types.ts";
import path from "node:path";
import settings from "./settings.ts";
import elizaLogger from "./logger.ts";
import { getVoyageAIEmbeddingConfig } from "./voyageai.ts";
import { models, getEmbeddingModelSettings, getEndpoint } from "./models.ts";
import { IAgentRuntime, ModelProviderName } from "./types.ts";
import LocalEmbeddingModelManager from "./localembeddingManager.ts";

interface EmbeddingOptions {
Expand All @@ -20,63 +22,93 @@ export const EmbeddingProvider = {
GaiaNet: "GaiaNet",
Heurist: "Heurist",
BGE: "BGE",
VoyageAI: "VoyageAI",
} as const;

export type EmbeddingProviderType =
(typeof EmbeddingProvider)[keyof typeof EmbeddingProvider];

export namespace EmbeddingProvider {
export type OpenAI = typeof EmbeddingProvider.OpenAI;
export type Ollama = typeof EmbeddingProvider.Ollama;
export type GaiaNet = typeof EmbeddingProvider.GaiaNet;
export type BGE = typeof EmbeddingProvider.BGE;
export type VoyageAI = typeof EmbeddingProvider.VoyageAI;
}

export type EmbeddingConfig = {
readonly dimensions: number;
readonly model: string;
readonly provider: EmbeddingProviderType;
readonly provider: EmbeddingProvider;
readonly endpoint?: string;
readonly apiKey?: string;
readonly maxInputTokens?: number;
};

export const getEmbeddingConfig = (): EmbeddingConfig => ({
dimensions:
settings.USE_OPENAI_EMBEDDING?.toLowerCase() === "true"
? getEmbeddingModelSettings(ModelProviderName.OPENAI).dimensions
: settings.USE_OLLAMA_EMBEDDING?.toLowerCase() === "true"
? getEmbeddingModelSettings(ModelProviderName.OLLAMA).dimensions
: settings.USE_GAIANET_EMBEDDING?.toLowerCase() === "true"
? getEmbeddingModelSettings(ModelProviderName.GAIANET)
.dimensions
: settings.USE_HEURIST_EMBEDDING?.toLowerCase() === "true"
? getEmbeddingModelSettings(ModelProviderName.HEURIST)
.dimensions
: 384, // BGE
model:
settings.USE_OPENAI_EMBEDDING?.toLowerCase() === "true"
? getEmbeddingModelSettings(ModelProviderName.OPENAI).name
: settings.USE_OLLAMA_EMBEDDING?.toLowerCase() === "true"
? getEmbeddingModelSettings(ModelProviderName.OLLAMA).name
: settings.USE_GAIANET_EMBEDDING?.toLowerCase() === "true"
? getEmbeddingModelSettings(ModelProviderName.GAIANET).name
: settings.USE_HEURIST_EMBEDDING?.toLowerCase() === "true"
? getEmbeddingModelSettings(ModelProviderName.HEURIST).name
: "BGE-small-en-v1.5",
provider:
settings.USE_OPENAI_EMBEDDING?.toLowerCase() === "true"
? "OpenAI"
: settings.USE_OLLAMA_EMBEDDING?.toLowerCase() === "true"
? "Ollama"
: settings.USE_GAIANET_EMBEDDING?.toLowerCase() === "true"
? "GaiaNet"
: settings.USE_HEURIST_EMBEDDING?.toLowerCase() === "true"
? "Heurist"
: "BGE",
});
// Get embedding config based on settings
export function getEmbeddingConfig(): EmbeddingConfig {
if (settings.USE_OPENAI_EMBEDDING?.toLowerCase() === "true") {
return {
dimensions: 1536,
model: "text-embedding-3-small",
provider: "OpenAI",
endpoint: "https://api.openai.com/v1",
apiKey: settings.OPENAI_API_KEY,
maxInputTokens: 1000000,
};
}
if (settings.USE_OLLAMA_EMBEDDING?.toLowerCase() === "true") {
return {
dimensions: 1024,
model: settings.OLLAMA_EMBEDDING_MODEL || "mxbai-embed-large",
provider: "Ollama",
endpoint: "https://ollama.eliza.ai/",
apiKey: settings.OLLAMA_API_KEY,
maxInputTokens: 1000000,
};
}
if (settings.USE_GAIANET_EMBEDDING?.toLowerCase() === "true") {
return {
dimensions: 768,
model: settings.GAIANET_EMBEDDING_MODEL || "nomic-embed",
provider: "GaiaNet",
endpoint: settings.SMALL_GAIANET_SERVER_URL || settings.MEDIUM_GAIANET_SERVER_URL || settings.LARGE_GAIANET_SERVER_URL,
apiKey: settings.GAIANET_API_KEY,
maxInputTokens: 1000000,
};
}
if (settings.USE_VOYAGEAI_EMBEDDING?.toLowerCase() === "true") {
return getVoyageAIEmbeddingConfig();
}

// Fallback to local BGE
return {
dimensions: 384,
model: "BGE-small-en-v1.5",
provider: "BGE",
maxInputTokens: 1000000,
};
};

async function getRemoteEmbedding(
input: string,
options: EmbeddingOptions
options: EmbeddingConfig
): Promise<number[]> {
// Ensure endpoint ends with /v1 for OpenAI
const baseEndpoint = options.endpoint.endsWith("/v1")
? options.endpoint
: `${options.endpoint}${options.isOllama ? "/v1" : ""}`;
elizaLogger.debug("Getting remote embedding using provider:", options.provider);

// Construct full URL
const fullUrl = `${baseEndpoint}/embeddings`;
const fullUrl = `${options.endpoint}/embeddings`;

// jank. voyageai is the only one that doesn't use "dimensions".
const body = options.provider === "VoyageAI" ? {
input,
model: options.model,
output_dimension: options.dimensions,
} : {
input,
model: options.model,
dimensions: options.dimensions,
};

const requestOptions = {
method: "POST",
Expand All @@ -88,14 +120,7 @@ async function getRemoteEmbedding(
}
: {}),
},
body: JSON.stringify({
input,
model: options.model,
dimensions:
options.dimensions ||
options.length ||
getEmbeddingConfig().dimensions, // Prefer dimensions, fallback to length
}),
body: JSON.stringify(body),
};

try {
Expand Down Expand Up @@ -141,44 +166,18 @@ export function getEmbeddingType(runtime: IAgentRuntime): "local" | "remote" {
}

export function getEmbeddingZeroVector(): number[] {
let embeddingDimension = 384; // Default BGE dimension

if (settings.USE_OPENAI_EMBEDDING?.toLowerCase() === "true") {
embeddingDimension = getEmbeddingModelSettings(
ModelProviderName.OPENAI
).dimensions; // OpenAI dimension
} else if (settings.USE_OLLAMA_EMBEDDING?.toLowerCase() === "true") {
embeddingDimension = getEmbeddingModelSettings(
ModelProviderName.OLLAMA
).dimensions; // Ollama mxbai-embed-large dimension
} else if (settings.USE_GAIANET_EMBEDDING?.toLowerCase() === "true") {
embeddingDimension = getEmbeddingModelSettings(
ModelProviderName.GAIANET
).dimensions; // GaiaNet dimension
} else if (settings.USE_HEURIST_EMBEDDING?.toLowerCase() === "true") {
embeddingDimension = getEmbeddingModelSettings(
ModelProviderName.HEURIST
).dimensions; // Heurist dimension
}

return Array(embeddingDimension).fill(0);
// Default BGE dimension is 384
return Array(getEmbeddingConfig().dimensions).fill(0);
}

/**
* Gets embeddings from a remote API endpoint. Falls back to local BGE/384
*
* @param {string} input - The text to generate embeddings for
* @param {EmbeddingOptions} options - Configuration options including:
* - model: The model name to use
* - endpoint: Base API endpoint URL
* - apiKey: Optional API key for authentication
* - isOllama: Whether this is an Ollama endpoint
* - dimensions: Desired embedding dimensions
* @param {IAgentRuntime} runtime - The agent runtime context
* @returns {Promise<number[]>} Array of embedding values
* @throws {Error} If the API request fails
* @throws {Error} If the API request fails or configuration is invalid
*/

export async function embed(runtime: IAgentRuntime, input: string) {
elizaLogger.debug("Embedding request:", {
modelProvider: runtime.character.modelProvider,
Expand Down Expand Up @@ -207,6 +206,11 @@ export async function embed(runtime: IAgentRuntime, input: string) {
const config = getEmbeddingConfig();
const isNode = typeof process !== "undefined" && process.versions?.node;

// Attempt remote embedding if it is configured.
if (config.provider !== EmbeddingProvider.BGE) {
return await getRemoteEmbedding(input, config);
}

// Determine which embedding path to use
if (config.provider === EmbeddingProvider.OpenAI) {
return await getRemoteEmbedding(input, {
Expand Down Expand Up @@ -271,6 +275,7 @@ export async function embed(runtime: IAgentRuntime, input: string) {
getEndpoint(runtime.character.modelProvider),
apiKey: runtime.token,
dimensions: config.dimensions,
provider: config.provider,
});

async function getLocalEmbedding(input: string): Promise<number[]> {
Expand Down
Loading

0 comments on commit 640f398

Please sign in to comment.