📖 Scenario: You are building a simple application that stores text data as vectors using the Chroma vector store from LangChain. This will help you quickly find similar texts later.
🎯 Goal: Create a Chroma vector store with some sample documents and set it up to be ready for similarity search.
📋 What You'll Learn
Create a list of documents with exact texts
Set a collection name for the Chroma vector store
Initialize the Chroma vector store with the documents and collection name
Add the final step to persist the vector store
💡 Why This Matters
🌍 Real World
Vector stores help applications find similar texts quickly, useful in chatbots, search engines, and recommendation systems.
💼 Career
Understanding how to set up vector stores is important for roles in AI development, data science, and software engineering working with natural language processing.
Progress0 / 4 steps
1
Create the documents list
Create a list called documents with these exact strings: 'Hello world', 'LangChain is awesome', and 'Vector stores are useful'.
LangChain
Hint
Use a Python list with the exact strings inside quotes.
2
Set the collection name
Create a variable called collection_name and set it to the string 'my_collection'.
LangChain
Hint
Assign the exact string 'my_collection' to the variable collection_name.
3
Initialize the Chroma vector store
Import Chroma from langchain.vectorstores and OpenAIEmbeddings from langchain.embeddings. Then create a variable called embedding as an instance of OpenAIEmbeddings(). Finally, create a variable called vectordb by initializing Chroma with embedding, documents, and collection_name as the collection name.
LangChain
Hint
Use Chroma.from_texts with the exact parameters: texts=documents, embedding=embedding, and collection_name=collection_name.
4
Persist the vector store
Call the persist method on the vectordb variable to save the vector store data.
LangChain
Hint
Call persist() on the vectordb variable to save the data.
Practice
(1/5)
1. What is the main purpose of setting up a Chroma vector store in Langchain?
easy
A. To generate text summaries automatically
B. To store text as vectors for fast similarity search
C. To create a relational database for text storage
D. To visualize text data in charts
Solution
Step 1: Understand Chroma vector store role
Chroma stores text as vectors to quickly find similar meanings, not for relational data or visualization.
Step 2: Identify main use case
Its main use is fast similarity search using vector embeddings.
Final Answer:
To store text as vectors for fast similarity search -> Option B
Quick Check:
Chroma = vector storage for similarity [OK]
Hint: Chroma = vector storage for quick similarity search [OK]
Common Mistakes:
Thinking Chroma is a database for tables
Confusing it with text summarization tools
Assuming it creates visual charts
2. Which of the following is the correct way to initialize a Chroma vector store with an embedding function named embed and collection name my_collection?
easy
A. store = Chroma(embedding=embed, collection='my_collection')
B. store = Chroma(collection='my_collection', embed=embedding_function)
C. store = Chroma('my_collection', embed_function=embed)
D. store = Chroma(embedding_function=embed, collection_name='my_collection')
Solution
Step 1: Check parameter names for Chroma initialization
The correct parameters are embedding_function and collection_name.
Step 2: Match correct syntax
store = Chroma(embedding_function=embed, collection_name='my_collection') uses embedding_function=embed and collection_name='my_collection', which is correct.
Final Answer:
store = Chroma(embedding_function=embed, collection_name='my_collection') -> Option D
Quick Check:
embedding_function + collection_name = store = Chroma(embedding_function=embed, collection_name='my_collection') [OK]
Hint: Use embedding_function and collection_name keys exactly [OK]
Common Mistakes:
Using wrong parameter names like embed or collection
Passing parameters in wrong order without keywords
Confusing embedding_function with embed_function
3. Given the following code snippet, what will results contain after running store.similarity_search('Hello world')?
A. A list containing 'Hello world' as the most similar text
B. An empty list because no texts were added
C. A list containing 'Goodbye world' only
D. A syntax error due to missing parameters
Solution
Step 1: Analyze texts added to the store
The store has two texts: 'Hello world' and 'Goodbye world'.
Step 2: Understand similarity_search behavior
Searching 'Hello world' will return texts most similar, so 'Hello world' itself is the closest match.
Final Answer:
A list containing 'Hello world' as the most similar text -> Option A
Quick Check:
Added texts include 'Hello world' = A list containing 'Hello world' as the most similar text [OK]
Hint: Similarity search returns closest added text matches [OK]
Common Mistakes:
Assuming no texts were added so result is empty
Thinking it returns unrelated texts
Confusing syntax error with runtime behavior
4. Identify the error in this Chroma vector store setup code:
embed = SomeEmbeddingFunction()
store = Chroma(embedding_function=embed)
store.add_texts(['Sample text'])
medium
A. Missing collection_name parameter in Chroma initialization
B. Embedding function is not assigned correctly
C. add_texts method does not exist
D. Chroma requires a database URL parameter
Solution
Step 1: Check required parameters for Chroma
Chroma requires both embedding_function and collection_name to be set.
Step 2: Identify missing parameter
The code misses collection_name, which is mandatory.
Final Answer:
Missing collection_name parameter in Chroma initialization -> Option A
Quick Check:
collection_name missing = Missing collection_name parameter in Chroma initialization [OK]
Hint: Always include collection_name when creating Chroma store [OK]
Common Mistakes:
Assuming embedding_function alone is enough
Thinking add_texts method is invalid
Believing database URL is mandatory
5. You want to store multiple documents in a Chroma vector store and then find the top 2 most similar documents to a query. Which code snippet correctly sets this up and performs the search?
hard
A. embed = Embedding()
store = Chroma(collection_name='docs')
docs = ['Doc1', 'Doc2', 'Doc3']
store.add_texts(docs)
results = store.similarity_search('query text', top=2)
B. embed = Embedding()
store = Chroma(embedding_function=embed, collection='docs')
docs = ['Doc1', 'Doc2', 'Doc3']
store.add_texts(docs)
results = store.similarity_search('query text', k=2)
C. embed = Embedding()
store = Chroma(embedding_function=embed, collection_name='docs')
docs = ['Doc1', 'Doc2', 'Doc3']
store.add_texts(docs)
results = store.similarity_search('query text', k=2)
D. embed = Embedding()
store = Chroma(embedding_function=embed, collection_name='docs')
docs = ['Doc1', 'Doc2', 'Doc3']
store.add_texts(docs)
results = store.similarity_search('query text')
Solution
Step 1: Verify Chroma initialization parameters
embedding_function and collection_name must be set correctly; embed = Embedding()
store = Chroma(embedding_function=embed, collection_name='docs')
docs = ['Doc1', 'Doc2', 'Doc3']
store.add_texts(docs)
results = store.similarity_search('query text', k=2) does this properly.
Step 2: Check similarity_search usage for top results
embed = Embedding()
store = Chroma(embedding_function=embed, collection_name='docs')
docs = ['Doc1', 'Doc2', 'Doc3']
store.add_texts(docs)
results = store.similarity_search('query text', k=2) uses k=2 to get top 2 results, which is the correct parameter.
Final Answer:
Correct setup with embedding_function, collection_name, and k=2 in similarity_search -> Option C