Knowledge Bases
RAG with vector search for AI workflows
Store and search documents with vector embeddings for Retrieval Augmented Generation (RAG).
Overview
Section titled “Overview”The knowledge store uses pgvector for semantic search:
- Documents are embedded and stored as vectors
- Search finds semantically similar content
- Results can be used as context for AI completions
Storing Documents
Section titled “Storing Documents”from bifrost import knowledge
# Store a single documentdoc_id = await knowledge.store( "Our refund policy allows returns within 30 days...", namespace="policies")
# With metadata and key for upsertawait knowledge.store( "Updated policy content...", namespace="policies", key="refund-policy", # Upserts if key exists metadata={"version": "2.0", "effective_date": "2024-01-01"})Batch Storage
Section titled “Batch Storage”For multiple documents:
docs = [ {"content": "Document 1...", "key": "doc-1"}, {"content": "Document 2...", "key": "doc-2"}, {"content": "Document 3...", "metadata": {"type": "faq"}}]
doc_ids = await knowledge.store_many(docs, namespace="support")Searching Documents
Section titled “Searching Documents”# Basic searchresults = await knowledge.search( "What is the refund policy?", namespace="policies")
for doc in results: print(f"Score: {doc.score}") print(f"Content: {doc.content}")Search Options
Section titled “Search Options”results = await knowledge.search( "refund policy", namespace=["policies", "faq"], # Multiple namespaces limit=10 # Default is 5)Namespaces
Section titled “Namespaces”Organize documents by namespace:
| Namespace | Use Case |
|---|---|
policies | Company policies and procedures |
faq | Frequently asked questions |
docs | Product documentation |
tickets | Historical support tickets |
List available namespaces:
namespaces = await knowledge.list_namespaces()for ns in namespaces: print(f"{ns.namespace}: {ns.scopes['total']} documents")Scopes
Section titled “Scopes”Documents can be global or organization-specific:
# Global scope (all orgs can search)await knowledge.store(content, namespace="global-policies", scope="global")
# Org-specific (default - only that org sees it)await knowledge.store(content, namespace="org-policies", org_id="org-123")Using with AI Completions
Section titled “Using with AI Completions”The ai.complete() function can include knowledge context:
from bifrost import ai
response = await ai.complete( "What is our vacation policy?", knowledge=["policies", "hr"])Bifrost automatically:
- Searches the specified namespaces
- Retrieves top matching documents
- Includes them as context in the prompt
- Returns a grounded response
Deleting Documents
Section titled “Deleting Documents”# Delete by IDawait knowledge.delete(doc_id)
# Or delete by key (if you stored with a key)# Re-store with empty content to effectively removeExample: Support Bot
Section titled “Example: Support Bot”from bifrost import workflow, ai, knowledge
@workflowasync def answer_question(question: str): # Direct RAG approach response = await ai.complete( question, knowledge=["faq", "docs"], system="Answer based on the provided context. If unsure, say so." )
return {"answer": response.content}
@workflowasync def index_faq(question: str, answer: str): await knowledge.store( f"Q: {question}\nA: {answer}", namespace="faq", key=f"faq-{hash(question)}" ) return {"status": "indexed"}Next Steps
Section titled “Next Steps”- Using AI in Workflows - Completions
- Agents and Chat - Conversational AI