Class that implements a vector store using Hierarchical Navigable Small World (HNSW) graphs. It extends the SaveableVectorStore class and provides methods for adding documents and vectors, performing similarity searches, and saving and loading the vector store.

Hierarchy

Constructors

Properties

FilterType: ((doc) => boolean)

Type declaration

    • (doc): boolean
    • Parameters

      Returns boolean

embeddings: Embeddings
lc_kwargs: SerializedFields
lc_namespace: string[] = ...

A path to the module that contains the class, eg. ["langchain", "llms"] Usually should be the same as the entrypoint the class is exported from.

lc_serializable: boolean = false
_index?: HierarchicalNSW

Accessors

  • get lc_aliases(): undefined | {
        [key: string]: string;
    }
  • A map of aliases for constructor args. Keys are the attribute names, e.g. "foo". Values are the alias that will replace the key in serialization. This is used to eg. make argument names match Python.

    Returns undefined | {
        [key: string]: string;
    }

  • get lc_attributes(): undefined | SerializedFields
  • A map of additional attributes to merge with constructor args. Keys are the attribute names, e.g. "foo". Values are the attribute values, which will be serialized. These attributes need to be accepted by the constructor as arguments.

    Returns undefined | SerializedFields

  • get lc_secrets(): undefined | {
        [key: string]: string;
    }
  • A map of secrets, which will be omitted from serialization. Keys are paths to the secret in constructor args, e.g. "foo.bar.baz". Values are the secret ids, which will be used when deserializing.

    Returns undefined | {
        [key: string]: string;
    }

Methods

  • Method to add documents to the vector store. It first converts the documents to vectors using the embeddings, then adds the vectors to the vector store.

    Parameters

    • documents: Document<Record<string, any>>[]

      The documents to be added to the vector store.

    Returns Promise<void>

    A Promise that resolves when the documents have been added.

  • Method to add vectors to the vector store. It first initializes the index if it hasn't been initialized yet, then adds the vectors to the index and the documents to the document store.

    Parameters

    • vectors: number[][]

      The vectors to be added to the vector store.

    • documents: Document<Record<string, any>>[]

      The documents corresponding to the vectors.

    Returns Promise<void>

    A Promise that resolves when the vectors and documents have been added.

  • Method to delete the vector store from a directory. It deletes the hnswlib.index file, the docstore.json file, and the args.json file from the directory.

    Parameters

    • params: {
          directory: string;
      }

      An object with a directory property that specifies the directory from which to delete the vector store.

      • directory: string

    Returns Promise<void>

    A Promise that resolves when the vector store has been deleted.

  • Method to save the vector store to a directory. It saves the HNSW index, the arguments, and the document store to the directory.

    Parameters

    • directory: string

      The directory to which to save the vector store.

    Returns Promise<void>

    A Promise that resolves when the vector store has been saved.

  • Method to perform a similarity search in the vector store using a query vector. It returns the k most similar documents along with their similarity scores. An optional filter function can be provided to filter the documents.

    Parameters

    • query: number[]

      The query vector.

    • k: number

      The number of most similar documents to return.

    • Optional filter: ((doc) => boolean)

      An optional filter function to filter the documents.

        • (doc): boolean
        • Parameters

          Returns boolean

    Returns Promise<[Document<Record<string, any>>, number][]>

    A Promise that resolves to an array of tuples, where each tuple contains a document and its similarity score.

  • Static method to create a new HNSWLib instance from texts and metadata. It creates a new Document instance for each text and metadata, then calls the fromDocuments method to create the HNSWLib instance.

    Parameters

    • texts: string[]

      The texts to be used to create the documents.

    • metadatas: object | object[]

      The metadata to be used to create the documents.

    • embeddings: Embeddings

      The embeddings to be used by the HNSWLib instance.

    • Optional dbConfig: {
          docstore?: SynchronousInMemoryDocstore;
      }

      An optional configuration object for the document store.

    Returns Promise<HNSWLib>

    A Promise that resolves to a new HNSWLib instance.

  • Static method to load a vector store from a directory. It reads the HNSW index, the arguments, and the document store from the directory, then creates a new HNSWLib instance with these values.

    Parameters

    • directory: string

      The directory from which to load the vector store.

    • embeddings: Embeddings

      The embeddings to be used by the HNSWLib instance.

    Returns Promise<HNSWLib>

    A Promise that resolves to a new HNSWLib instance.

Generated using TypeDoc