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


