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
Json
Filtering on load with Json
Learn how to filter knowledge base at load time using Json documents with user-specific metadata.
Code
from agno.agent import Agent
from agno.knowledge.json import JSONKnowledgeBase
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.JSON
)
# Initialize LanceDB
# By default, it stores data in /tmp/lancedb
vector_db = LanceDb(
table_name="recipes",
uri="tmp/lancedb", # You can change this path to store data elsewhere
)
# Step 1: Initialize knowledge base with documents and metadata
# ------------------------------------------------------------------------------
# When loading the knowledge base, we can attach metadata that will be used for filtering
# This metadata can include user IDs, document types, dates, or any other attributes
# Initialize the JSONKnowledgeBase
knowledge_base = JSONKnowledgeBase(
vector_db=vector_db,
num_documents=5,
)
knowledge_base.load_document(
path=downloaded_cv_paths[0],
metadata={"user_id": "jordan_mitchell", "document_type": "cv", "year": 2025},
recreate=True, # Set to True only for the first run, then set to False
)
# Load second document with user_2 metadata
knowledge_base.load_document(
path=downloaded_cv_paths[1],
metadata={"user_id": "taylor_brooks", "document_type": "cv", "year": 2025},
)
# Load second document with user_3 metadata
knowledge_base.load_document(
path=downloaded_cv_paths[2],
metadata={"user_id": "morgan_lee", "document_type": "cv", "year": 2025},
)
# Load second document with user_4 metadata
knowledge_base.load_document(
path=downloaded_cv_paths[3],
metadata={"user_id": "casey_jordan", "document_type": "cv", "year": 2025},
)
# Load second document with user_5 metadata
knowledge_base.load_document(
path=downloaded_cv_paths[4],
metadata={"user_id": "alex_rivera", "document_type": "cv", "year": 2025},
)
# Step 2: Query the knowledge base with different filter combinations
# ------------------------------------------------------------------------------
# Uncomment the example you want to run
# Option 1: Filters on the Agent
# Initialize the Agent with the knowledge base
agent = Agent(
knowledge=knowledge_base,
search_knowledge=True,
knowledge_filters={"user_id": "jordan_mitchell"},
)
agent.print_response(
"Tell me about Jordan Mitchell's experience and skills",
markdown=True,
)
# agent = Agent(
# knowledge=knowledge_base,
# search_knowledge=True,
# )
# agent.print_response(
# "Tell me about Jordan Mitchell's experience and skills",
# knowledge_filters = {"user_id": "jordan_mitchell"},
# markdown=True,
# )
Usage
1
Install libraries
pip install -U agno openai lancedb
2
Run the example
python cookbook/agent_concepts/knowledge/filters/json/filtering_on_load.py
Was this page helpful?
Assistant
Responses are generated using AI and may contain mistakes.