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",
url: "http://localhost:8000", // URL of the Chroma server
}
);

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

  • VectorStore
    • Chroma

Constructors

Properties

FilterType: Where
clientParams?: Omit<ChromaClientParams, "path">
collection?: Collection
collectionMetadata?: CollectionMetadata
collectionName: string
embeddings: EmbeddingsInterface

Embeddings interface for generating vector embeddings from text queries, enabling vector-based similarity searches.

filter?: object
index?: ChromaClient
numDimensions?: number
url: string

Methods

  • 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<Record<string, any>>[]

      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.

      • Optionalids?: string[]

    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<Record<string, any>>[]

      An array of Document instances associated with the vectors.

    • Optionaloptions: {
          ids?: string[];
      }

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

      • Optionalids?: string[]

    Returns Promise<string[]>

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

  • Creates a VectorStoreRetriever instance with flexible configuration options.

    Parameters

    • OptionalkOrFields: number | Partial<VectorStoreRetrieverInput<Chroma>>

      If a number is provided, it sets the k parameter (number of items to retrieve).

      • If an object is provided, it should contain various configuration options.
    • Optionalfilter: Where

      Optional filter criteria to limit the items retrieved based on the specified filter type.

    • Optionalcallbacks: Callbacks

      Optional callbacks that may be triggered at specific stages of the retrieval process.

    • Optionaltags: string[]

      Tags to categorize or label the VectorStoreRetriever. Defaults to an empty array if not provided.

    • Optionalmetadata: Record<string, unknown>

      Additional metadata as key-value pairs to add contextual information for the retrieval process.

    • Optionalverbose: boolean

      If true, enables detailed logging for the retrieval process. Defaults to false.

    Returns VectorStoreRetriever<Chroma>

    • A configured VectorStoreRetriever instance based on the provided parameters.

    Basic usage with a k value:

    const retriever = myVectorStore.asRetriever(5);
    

    Usage with a configuration object:

    const retriever = myVectorStore.asRetriever({
    k: 10,
    filter: myFilter,
    tags: ['example', 'test'],
    verbose: true,
    searchType: 'mmr',
    searchKwargs: { alpha: 0.5 },
    });
  • 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.

  • Return documents selected using the maximal marginal relevance. Maximal marginal relevance optimizes for similarity to the query AND diversity among selected documents.

    Parameters

    • query: string

      Text to look up documents similar to.

    • options: MaxMarginalRelevanceSearchOptions<Where>
    • _callbacks: undefined | Callbacks

    Returns Promise<DocumentInterface<Record<string, any>>[]>

    • List of documents selected by maximal marginal relevance.
  • Searches for documents similar to a text query by embedding the query and performing a similarity search on the resulting vector.

    Parameters

    • query: string

      Text query for finding similar documents.

    • Optionalk: number

      Number of similar results to return. Defaults to 4.

    • Optionalfilter: Where

      Optional filter based on FilterType.

    • Optional_callbacks: Callbacks

      Optional callbacks for monitoring search progress

    Returns Promise<DocumentInterface<Record<string, any>>[]>

    A promise resolving to an array of DocumentInterface instances representing similar documents.

  • 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<Record<string, any>>, number][]>

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

  • Searches for documents similar to a text query by embedding the query, and returns results with similarity scores.

    Parameters

    • query: string

      Text query for finding similar documents.

    • Optionalk: number

      Number of similar results to return. Defaults to 4.

    • Optionalfilter: Where

      Optional filter based on FilterType.

    • Optional_callbacks: Callbacks

      Optional callbacks for monitoring search progress

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

    A promise resolving to an array of tuples, each containing a document and its similarity score.

  • Returns Serialized

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

    Parameters

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

      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.