A vector database stores numerical representations of documents or conversations so systems can retrieve content by similarity or distance. It commonly supports retrieval augmented generation (RAG): indexed content supplies relevant context to an LLM’s prompt. section 8.2 section 8.9
A typical document-retrieval workflow follows these steps:
- Load a document, divide it into context chunks, embed the chunks as vectors, and store them. section 8.2
- Embed a user query, search for similar vectors, and return the matching documents or chunks with scores. section 8.3.2
- Use retrieved content as prompt context so an LLM can answer questions about the indexed information. section 8.2
Querying documents with Chroma DB
A local Chroma DB collection can store document embeddings, documents, and IDs. A query is embedded and submitted with a result count, returning IDs, distances, and text. Distance—not similarity—is reported: cosine distance is zero for the most similar content. section 8.3.4
Listing 8.5 document_query_chromadb.py (relevant code sections)
embeddings = [get_embedding(doc) for doc in documents] #1
ids = [f"id{i}" for i in range(len(documents))] #1
chroma_client = chromadb.Client() #2
collection = chroma_client.create_collection(
name="documents") #2
collection.add( #3
embeddings=embeddings,
documents=documents,
ids=ids
)
def query_chromadb(query, top_n=2): #4
query_embedding = get_embedding(query)
results = collection.query(
query_embeddings=[query_embedding],
n_results=top_n
)
return [(id, score, text) for id, score, text in
zip(results['ids'][0],
results['distances'][0],
results['documents'][0])]
while True: #5
query = input("Enter a search query (or 'exit' to stop): ")
if query.lower() == 'exit':
break
top_n = int(input("How many top matches do you want to see? "))
search_results = query_chromadb(query, top_n)
print("Top Matched Documents:")
for id, score, text in search_results:
print(f"""
ID:{id} TEXT: {text} SCORE: {round(score, 2)}
""") #5
print("\n")
###Output
Enter a search query (or 'exit' to stop): dogs are lazy
How many top matches do you want to see? 3
Top Matched Documents:
ID:id7 TEXT: The dog is lazy but the brown fox is quick! SCORE: 0.24
ID:id5 TEXT: The brown fox is quick and the blue dog is lazy! SCORE: 0.28
ID:id2 TEXT: The quick brown fox jumps over the lazy dog. SCORE: 0.29
Inspect the complete Chroma workflow from embedding documents through querying stored records. section 8.3.4

























