Examples
- Examples
- Getting Started
- Agents
- Teams
- Workflows
- Applications
- Streamlit Apps
- Evals
Agent Concepts
- Reasoning
- Multimodal
- RAG
- User Control Flows
- Knowledge
- Knowledge Filters
- ArXiv Knowledge Base
- Combined Knowledge Base
- CSV Knowledge Base
- CSV URL Knowledge Base
- Document Knowledge Base
- DOCX Knowledge Base
- Memory
- Async
- Hybrid Search
- Storage
- Tools
- Vector Databases
- Context
- Embedders
- Agent State
- Observability
- Miscellaneous
Models
- Anthropic
- AWS Bedrock
- AWS Bedrock Claude
- Azure AI Foundry
- Azure OpenAI
- Cerebras
- Cerebras OpenAI
- Cohere
- DeepInfra
- DeepSeek
- Fireworks
- Gemini
- Groq
- Hugging Face
- IBM
- LM Studio
- LiteLLM
- LiteLLM OpenAI
- Meta
- Mistral
- NVIDIA
- Ollama
- OpenAI
- Perplexity
- Together
- XAI
- Vercel
Docx
Filtering with Docx
Learn how to filter knowledge base searches using Docx documents with user-specific metadata.
Code
"""
User-Level Knowledge Filtering Example
This cookbook demonstrates how to use knowledge filters to restrict knowledge base searches to specific users, document types, or any other metadata attributes.
Key concepts demonstrated:
1. Loading documents with user-specific metadata
2. Filtering knowledge base searches by user ID
3. Combining multiple filter criteria
4. Comparing results across different filter combinations
You can pass filters in the following ways:
1. If you pass on Agent only, we use that for all runs
2. If you pass on run/print_response only, we use that for that run
3. If you pass on both, we override with the filters passed on run/print_response for that run
"""
from agno.agent import Agent
from agno.knowledge.docx import DocxKnowledgeBase
from agno.utils.media import (
SampleDataFileExtension,
download_knowledge_filters_sample_data,
)
from agno.vectordb.lancedb import LanceDb
# Download all sample CVs and get their paths
downloaded_cv_paths = download_knowledge_filters_sample_data(
num_files=5, file_extension=SampleDataFileExtension.DOCX
)
# Initialize LanceDB
vector_db = LanceDb(
table_name="recipes",
uri="tmp/lancedb",
)
# Now use the downloaded paths in knowledge base initialization
knowledge_base = DocxKnowledgeBase(
path=[
{
"path": downloaded_cv_paths[0],
"metadata": {
"user_id": "jordan_mitchell",
"document_type": "cv",
"year": 2025,
},
},
{
"path": downloaded_cv_paths[1],
"metadata": {
"user_id": "taylor_brooks",
"document_type": "cv",
"year": 2025,
},
},
{
"path": downloaded_cv_paths[2],
"metadata": {
"user_id": "morgan_lee",
"document_type": "cv",
"year": 2025,
},
},
{
"path": downloaded_cv_paths[3],
"metadata": {
"user_id": "casey_jordan",
"document_type": "cv",
"year": 2025,
},
},
{
"path": downloaded_cv_paths[4],
"metadata": {
"user_id": "alex_rivera",
"document_type": "cv",
"year": 2025,
},
},
],
vector_db=vector_db,
)
# Load all documents into the vector database
knowledge_base.load(recreate=True)
# Step 2: Query the knowledge base with different filter combinations
# ------------------------------------------------------------------------------
# Option 1: Filters on the Agent
# Initialize the Agent with the knowledge base and filters
agent = Agent(
knowledge=knowledge_base,
search_knowledge=True,
knowledge_filters={"user_id": "jordan_mitchell"},
)
# Query for Jordan Mitchell's experience and skills
agent.print_response(
"Tell me about Jordan Mitchell's experience and skills",
markdown=True,
)
# # Option 2: Filters on the run/print_response
# agent = Agent(
# knowledge=knowledge_base,
# search_knowledge=True,
# )
# # Query for Taylor Brooks as a candidate
# agent.print_response(
# "Tell me about Taylor Brooks as a candidate",
# knowledge_filters={"user_id": "taylor_brooks"},
# markdown=True,
# )
Usage
1
Install libraries
pip install -U agno openai lancedb
2
Run the example
python cookbook/agent_concepts/knowledge/filters/docx/filtering.py
Was this page helpful?
Assistant
Responses are generated using AI and may contain mistakes.