liveBook · Manning

Pinecone

As explained in Building Reliable AI Systems

Pinecone is a managed vector database service for storing and searching vector embeddings.

Pinecone is a managed vector database for storing embeddings and searching them by similarity. Its cloud API handles index creation, vector storage, and filtered queries without requiring a team to operate the search infrastructure. That makes it useful when an assistant must retrieve relevant documents quickly, though cost at scale remains a consideration. section 4.5.3

A health-care policy assistant illustrates the workflow. Split policy documents into searchable chunks, embed each chunk, and store its vector alongside the original text and policy metadata. At question time, embed the query and retrieve matching chunks before generating an answer from them. section 4.5.4

The implementation hinges on a few choices:

  • Chunk documents with enough context to remain interpretable. The example uses 500-character chunks with 100-character overlap and retains section titles. section 4.5.4
  • Choose an embedding model suited to the domain. The health-care example uses a model pretrained on medical literature; the Pinecone index dimension must match its output, and the index uses cosine similarity. section 4.5.4
  • Store policy ID, department, effective date, and status with each vector. Query filters can then restrict results to relevant policies rather than relying on similarity alone. section 4.5.4
vectors_to_upsert = []   #1
for i, chunk in enumerate(all_chunks):
    vector_data = {    #2
        "id": chunk["chunk_id"],
        "values": embeddings[i].tolist(),
        "metadata": {
            "content": chunk["content"],
            "section_title": chunk["section_title"],
            "policy_id": chunk["policy_id"],
            "department": chunk["department"],
            "effective_date": chunk["effective_date"],
            "status": chunk["status"]
        }
    }
    vectors_to_upsert.append(vector_data)

index.upsert(vectors=vectors_to_upsert)   #3

index_stats = index.describe_index_stats()   #4
print(f"Total vectors in index: {index_stats['total_vector_count']}")

The upsert listing shows the storage boundary: each record carries a chunk ID, embedding values, text, and policy metadata. It also checks the index's vector count after insertion. section 4.5.4

def search_policies(query, filters=None, top_k=3):
    """
    Search policy documents with semantic search and optional filters

    Args:
        query: User question or query string
        filters: Dictionary of metadata filters to apply
        top_k: Number of results to return

    Returns:
        List of relevant policy chunks with metadata
    """
    query_embedding = model.encode([query])[0].tolist()    #1

    filter_dict = None    #2
    if filters:    #2
        filter_dict = {}    #2
        for key, value in filters.items():    #2
            filter_dict[key] = value    #2

    query_results = index.query(    #3
        vector=query_embedding,    #3
        filter=filter_dict,    #3
        top_k=top_k,    #3
        include_metadata=True    #3
    )

    results = []    #4
    for match in query_results['matches']:    #4
        results.append({ #4
            "score": match['score'],
            "content": match['metadata']['content'],
            "section_title": match['metadata']['section_title'],
            "policy_id": match['metadata']['policy_id'],
            "department": match['metadata']['department'],
            "effective_date": match['metadata']['effective_date']
        })

    return results

query = "What's covered for physical therapy after knee surgery?"   #5
filters = {"status": "Active", "department": "Benefits"}    #5
results = search_policies(query, filters)    #5

for i, result in enumerate(results):    #6
    print(f"\nResult {i+1} (Score: {result['score']:.4f})")
    print(f"Policy: {result['policy_id']} ({result['department']})")
    print(f"Section: {result['section_title']}")
    print(f"Content: {result['content'][:200]}...")

The search function embeds the question, passes optional metadata filters to the index query, and requests metadata with the top matches. Retrieved passages can then supply context and source information for an answer. section 4.5.4

How Building Reliable AI Systems covers it

How deep it goes
Works through document preparation, embedding, index setup, upsert, and querying, but does not explain Pinecone's infrastructure internals.
Examples
Uses Python listings for a health-care policy assistant that indexes policy chunks and searches them with optional filters.
What it assumes
Assumes Python familiarity while walking through chunking, embedding generation, and index creation.
What you can do after
Apply it via Pinecone's Python API to index embedded policy chunks and retrieve filtered context for answers.
How it connects
Connects vector embeddings and cosine similarity to metadata filtering, semantic search, and answer generation.

Also explained in

Compare how 3 books explain Pinecone →

Related concepts

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