langchain.js
    Preparing search index...

    Chroma vector store integration.

    Setup: Install @langchain/community and chromadb.

    npm install @langchain/community chromadb
    
    Instantiate
    import { Chroma } from '@langchain/community/vectorstores/chroma';
    // Or other embeddings
    import { OpenAIEmbeddings } from '@langchain/openai';

    const embeddings = new OpenAIEmbeddings({
    model: "text-embedding-3-small",
    })

    const vectorStore = new Chroma(
    embeddings,
    {
    collectionName: "foo",
    host: "localhost",
    }
    );

    Add documents
    import type { Document } from '@langchain/core/documents';

    const document1 = { pageContent: "foo", metadata: { baz: "bar" } };
    const document2 = { pageContent: "thud", metadata: { bar: "baz" } };
    const document3 = { pageContent: "I will be deleted :(", metadata: {} };

    const documents: Document[] = [document1, document2, document3];
    const ids = ["1", "2", "3"];
    await vectorStore.addDocuments(documents, { ids });

    Delete documents
    await vectorStore.delete({ ids: ["3"] });
    

    Similarity search
    const results = await vectorStore.similaritySearch("thud", 1);
    for (const doc of results) {
    console.log(`* ${doc.pageContent} [${JSON.stringify(doc.metadata, null)}]`);
    }
    // Output: * thud [{"baz":"bar"}]

    Similarity search with filter
    const resultsWithFilter = await vectorStore.similaritySearch("thud", 1, { baz: "bar" });

    for (const doc of resultsWithFilter) {
    console.log(`* ${doc.pageContent} [${JSON.stringify(doc.metadata, null)}]`);
    }
    // Output: * foo [{"baz":"bar"}]

    Similarity search with score
    const resultsWithScore = await vectorStore.similaritySearchWithScore("qux", 1);
    for (const [doc, score] of resultsWithScore) {
    console.log(`* [SIM=${score.toFixed(6)}] ${doc.pageContent} [${JSON.stringify(doc.metadata, null)}]`);
    }
    // Output: * [SIM=0.000000] qux [{"bar":"baz","baz":"bar"}]

    As a retriever
    const retriever = vectorStore.asRetriever({
    searchType: "mmr", // Leave blank for standard similarity search
    k: 1,
    });
    const resultAsRetriever = await retriever.invoke("thud");
    console.log(resultAsRetriever);

    // Output: [Document({ metadata: { "baz":"bar" }, pageContent: "thud" })]

    Hierarchy (View Summary)

    Index

    Constructors

    • Parameters

      Returns Chroma

    Properties

    clientParams?: Omit<ChromaClientArgs, "path">
    collection?: Collection
    collectionMetadata?: CollectionMetadata
    collectionName: string
    filter?: object
    FilterType: Where
    index?: ChromaClient
    numDimensions?: number
    url: string

    Methods

    • Returns string

    • Adds documents to the Chroma database. The documents are first converted to vectors using the embeddings instance, and then added to the database.

      Parameters

      • documents: Document[]

        An array of Document instances to be added to the database.

      • Optionaloptions: { ids?: string[] }

        Optional. An object containing an array of ids for the documents.

      Returns Promise<string[]>

      A promise that resolves when the documents have been added to the database.

    • Adds vectors to the Chroma database. The vectors are associated with the provided documents.

      Parameters

      • vectors: number[][]

        An array of vectors to be added to the database.

      • documents: Document[]

        An array of Document instances associated with the vectors.

      • Optionaloptions: { ids?: string[] }

        Optional. An object containing an array of ids for the vectors.

      Returns Promise<string[]>

      A promise that resolves with an array of document IDs when the vectors have been added to the database.

    • Deletes documents from the Chroma database. The documents to be deleted can be specified by providing an array of ids or a filter object.

      Parameters

      • params: ChromaDeleteParams<Where>

        An object containing either an array of ids of the documents to be deleted or a filter object to specify the documents to be deleted.

      Returns Promise<void>

      A promise that resolves when the specified documents have been deleted from the database.

    • Ensures that a collection exists in the Chroma database. If the collection does not exist, it is created.

      Returns Promise<Collection>

      A promise that resolves with the Collection instance.

    • Searches for vectors in the Chroma database that are similar to the provided query vector. The search can be filtered using the provided filter object or the filter property of the Chroma instance.

      Parameters

      • query: number[]

        The query vector.

      • k: number

        The number of similar vectors to return.

      • Optionalfilter: Where

        Optional. A filter object to filter the search results.

      Returns Promise<[Document, number][]>

      A promise that resolves with an array of tuples, each containing a Document instance and a similarity score.

    • Creates a new Chroma instance from an array of Document instances. The documents are added to the Chroma database.

      Parameters

      • docs: Document[]

        An array of Document instances.

      • embeddings: EmbeddingsInterface

        An Embeddings instance used to generate embeddings for the documents.

      • dbConfig: ChromaLibArgs

        A ChromaLibArgs object containing the configuration for the Chroma database.

      Returns Promise<Chroma>

      A promise that resolves with a new Chroma instance.

    • Creates a new Chroma instance from an existing collection in the Chroma database.

      Parameters

      • embeddings: EmbeddingsInterface

        An Embeddings instance used to generate embeddings for the documents.

      • dbConfig: ChromaLibArgs

        A ChromaLibArgs object containing the configuration for the Chroma database.

      Returns Promise<Chroma>

      A promise that resolves with a new Chroma instance.

    • Creates a new Chroma instance from an array of text strings. The text strings are converted to Document instances and added to the Chroma database.

      Parameters

      • texts: string[]

        An array of text strings.

      • metadatas: object | object[]

        An array of metadata objects or a single metadata object. If an array is provided, it must have the same length as the texts array.

      • embeddings: EmbeddingsInterface

        An Embeddings instance used to generate embeddings for the documents.

      • dbConfig: ChromaLibArgs

        A ChromaLibArgs object containing the configuration for the Chroma database.

      Returns Promise<Chroma>

      A promise that resolves with a new Chroma instance.