0
0
LangChainframework~5 mins

Chroma vector store setup in LangChain

Choose your learning style9 modes available
Introduction

Chroma vector store helps you save and search data by meaning, not just words. It makes finding related info faster and smarter.

You want to find documents similar to a question quickly.
You need to organize text data by meaning for a chatbot.
You want to build a search tool that understands context.
You are working with AI models that use vector search.
You want to store and retrieve embeddings efficiently.
Syntax
LangChain
from langchain.vectorstores import Chroma

# Create or load a Chroma vector store
vector_store = Chroma(collection_name="my_collection", embedding_function=embedding_function)

# Add documents to the store
vector_store.add_texts(texts)

# Search for similar documents
results = vector_store.similarity_search(query, k=3)

embedding_function converts text into numbers that capture meaning.

collection_name is like a folder name to organize your data.

Examples
This example creates a Chroma store for books and searches for texts related to Python programming.
LangChain
from langchain.vectorstores import Chroma
from langchain.embeddings import OpenAIEmbeddings

embedding_function = OpenAIEmbeddings()
vector_store = Chroma(collection_name="books", embedding_function=embedding_function)
vector_store.add_texts(["Learn Python", "Learn JavaScript"])
results = vector_store.similarity_search("Python programming", k=2)
This example loads an existing Chroma collection without adding new texts and searches for related info.
LangChain
from langchain.vectorstores import Chroma
from langchain.embeddings import OpenAIEmbeddings

embedding_function = OpenAIEmbeddings()
# Load existing collection
vector_store = Chroma(collection_name="my_collection", embedding_function=embedding_function)
results = vector_store.similarity_search("Find related info", k=1)
Sample Program

This program sets up a Chroma vector store, adds three greetings, and searches for texts similar to "Hello". It prints the top 2 matches.

LangChain
from langchain.vectorstores import Chroma
from langchain.embeddings import OpenAIEmbeddings

# Create embedding function
embedding_function = OpenAIEmbeddings()

# Create a new Chroma vector store
vector_store = Chroma(collection_name="example_collection", embedding_function=embedding_function)

# Add some texts
texts = ["Hello world", "Hi there", "Greetings"]
vector_store.add_texts(texts)

# Search for similar texts
query = "Hello"
results = vector_store.similarity_search(query, k=2)

# Print results
for i, doc in enumerate(results, 1):
    print(f"Result {i}: {doc.page_content}")
OutputSuccess
Important Notes

Make sure your embedding function is compatible with Chroma.

Adding texts multiple times appends data; avoid duplicates if not needed.

Use meaningful collection names to keep your data organized.

Summary

Chroma stores text as vectors to find similar meaning quickly.

Set it up by providing an embedding function and collection name.

Add texts and use similarity_search to find related content.