liveBook · Manning

Vector database

As explained in AI Agents in Action

A vector database is a database optimized for storing numerical vector representations and retrieving records by vector-based similarity or distance.

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

How AI Agents in Action covers it

How deep it goes
Explains indexing, embedding, querying, similarity scores, and cosine distance, while deferring nuanced retrieval design.
Examples
Demonstrates an in-memory Python search database and a local Chroma DB collection queried with document embeddings.
What it assumes
Assumes basic documents and queries, while introducing vectorization, embeddings, similarity search, and distance scoring.
What you can do after
Build and query a simple vector store in Python or Chroma DB for document retrieval.
How it connects
Connects vector databases with embeddings, similarity search, semantic search, RAG, document indexing, and agent memory.

Also explained in

Compare how 26 books explain vector database →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of AI Agents in Action. The section links point to the parts of the book it draws on.