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
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.

  • Parameters

    • OptionalkOrFields: number | Partial<VectorStoreRetrieverInput<Chroma>>
    • Optionalfilter: Where
    • Optionalcallbacks: Callbacks
    • Optionaltags: string[]
    • Optionalmetadata: Record<string, unknown>
    • Optionalverbose: boolean

    Returns VectorStoreRetriever<Chroma>

  • 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.
  • Parameters

    • query: string
    • Optionalk: number
    • Optionalfilter: Where
    • Optional_callbacks: Callbacks

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

  • 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.

  • Parameters

    • query: string
    • Optionalk: number
    • Optionalfilter: Where
    • Optional_callbacks: Callbacks

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

  • 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.