Interface IEmbeddingSchema

Interface representing the schema for configuring an embedding mechanism. Defines how embeddings are created and compared within the swarm.

interface IEmbeddingSchema {
    calculateSimilarity(a: Embeddings, b: Embeddings): Promise<number>;
    callbacks?: Partial<IEmbeddingCallbacks>;
    createEmbedding(text: string, embeddingName: string): Promise<Embeddings>;
    embeddingName: string;
    persist?: boolean;
    readEmbeddingCache?: (
        embeddingName: string,
        stringHash: string,
    ) => number[] | Promise<number[]>;
    writeEmbeddingCache?: (
        embeddings: number[],
        embeddingName: string,
        stringHash: string,
    ) => void | Promise<void>;
}

Properties

callbacks?: Partial<IEmbeddingCallbacks>

Optional partial set of callbacks for embedding events, allowing customization of creation and comparison.

embeddingName: string

The unique name of the embedding mechanism within the swarm.

persist?: boolean

Optional flag to enable serialization of navigation stack and active agent state to persistent storage (e.g., hard drive).

readEmbeddingCache?: (
    embeddingName: string,
    stringHash: string,
) => number[] | Promise<number[]>

Retrieves the embedding vector for a specific string hash, returning null if not found. Used to check if a precomputed embedding exists in the cache.

If reading from storage fails (e.g., file corruption).

writeEmbeddingCache?: (
    embeddings: number[],
    embeddingName: string,
    stringHash: string,
) => void | Promise<void>

Stores an embedding vector for a specific string hash, persisting it for future retrieval. Used to cache computed embeddings to avoid redundant processing.

If writing to storage fails (e.g., permissions or disk space).

Methods

  • Calculates the similarity between two embeddings. Commonly used for search or ranking operations (e.g., cosine similarity).

    Parameters

    • a: Embeddings
    • b: Embeddings

    Returns Promise<number>

    If similarity calculation fails (e.g., due to invalid embeddings or computation errors).

  • Creates an embedding from the provided text. Typically used for indexing or search operations in storage.

    Parameters

    • text: string
    • embeddingName: string

    Returns Promise<Embeddings>

    If embedding creation fails (e.g., due to invalid text or model errors).