Skip to content

Commit 528b035

Browse files
updates
1 parent 937a4cb commit 528b035

File tree

7 files changed

+281
-98
lines changed

7 files changed

+281
-98
lines changed

cookbook/knowledge/filters/async_filtering.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@
114114
# Query for Jordan Mitchell's experience and skills
115115
asyncio.run(
116116
agent.aprint_response(
117-
"Tell me about the candidate's experience and skills",
117+
"Search the knowledge base for the candidate's experience and skills",
118118
knowledge_filters={"user_id": "jordan_mitchell"},
119119
markdown=True,
120120
)

cookbook/knowledge/filters/filtering_with_conditions_on_agent.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
"""
77

88
from agno.agent import Agent
9-
from agno.utils.search_filters import AND, EQ, IN, NOT
109
from agno.knowledge.knowledge import Knowledge
1110
from agno.utils.media import (
1211
SampleDataFileExtension,
1312
download_knowledge_filters_sample_data,
1413
)
14+
from agno.utils.search_filters import AND, EQ, IN, NOT
1515
from agno.vectordb.pgvector import PgVector
1616

1717
# Download all sample sales documents and get their paths
@@ -88,7 +88,7 @@
8888
sales_agent.print_response(
8989
"Describe revenue performance for the region",
9090
# knowledge_filters=[{"region": "north_america", "data_type": "sales"}],
91-
knowledge_filters=[(IN("region", ["north_america", "europe"]))],
91+
knowledge_filters=[(IN("region", ["north_america"]))],
9292
markdown=True,
9393
)
9494

cookbook/knowledge/filters/filtering_with_conditions_on_team.py

Lines changed: 87 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
"""
77

88
from agno.agent import Agent
9-
from agno.utils.search_filters import AND, EQ, IN, NOT, OR
109
from agno.knowledge.knowledge import Knowledge
1110
from agno.knowledge.reader.pdf_reader import PDFReader
1211
from agno.models.openai import OpenAIChat
@@ -15,6 +14,7 @@
1514
SampleDataFileExtension,
1615
download_knowledge_filters_sample_data,
1716
)
17+
from agno.utils.search_filters import AND, EQ, IN, NOT, OR
1818
from agno.vectordb.pgvector import PgVector
1919

2020
# Download all sample CVs and get their paths
@@ -34,51 +34,51 @@
3434
)
3535

3636
# Add documents with metadata for filtering
37-
# knowledge_base.add_contents(
38-
# [
39-
# {
40-
# "path": downloaded_cv_paths[0],
41-
# "metadata": {
42-
# "user_id": "jordan_mitchell",
43-
# "document_type": "cv",
44-
# "year": 2025,
45-
# },
46-
# },
47-
# {
48-
# "path": downloaded_cv_paths[1],
49-
# "metadata": {
50-
# "user_id": "taylor_brooks",
51-
# "document_type": "cv",
52-
# "year": 2025,
53-
# },
54-
# },
55-
# {
56-
# "path": downloaded_cv_paths[2],
57-
# "metadata": {
58-
# "user_id": "morgan_lee",
59-
# "document_type": "cv",
60-
# "year": 2025,
61-
# },
62-
# },
63-
# {
64-
# "path": downloaded_cv_paths[3],
65-
# "metadata": {
66-
# "user_id": "casey_jordan",
67-
# "document_type": "cv",
68-
# "year": 2020,
69-
# },
70-
# },
71-
# {
72-
# "path": downloaded_cv_paths[4],
73-
# "metadata": {
74-
# "user_id": "alex_rivera",
75-
# "document_type": "cv",
76-
# "year": 2020,
77-
# },
78-
# },
79-
# ],
80-
# reader=PDFReader(chunk=True),
81-
# )
37+
knowledge_base.add_contents(
38+
[
39+
{
40+
"path": downloaded_cv_paths[0],
41+
"metadata": {
42+
"user_id": "jordan_mitchell",
43+
"document_type": "cv",
44+
"year": 2025,
45+
},
46+
},
47+
{
48+
"path": downloaded_cv_paths[1],
49+
"metadata": {
50+
"user_id": "taylor_brooks",
51+
"document_type": "cv",
52+
"year": 2025,
53+
},
54+
},
55+
{
56+
"path": downloaded_cv_paths[2],
57+
"metadata": {
58+
"user_id": "morgan_lee",
59+
"document_type": "cv",
60+
"year": 2025,
61+
},
62+
},
63+
{
64+
"path": downloaded_cv_paths[3],
65+
"metadata": {
66+
"user_id": "casey_jordan",
67+
"document_type": "cv",
68+
"year": 2020,
69+
},
70+
},
71+
{
72+
"path": downloaded_cv_paths[4],
73+
"metadata": {
74+
"user_id": "alex_rivera",
75+
"document_type": "cv",
76+
"year": 2020,
77+
},
78+
},
79+
],
80+
reader=PDFReader(chunk=True),
81+
)
8282

8383
# Create knowledge search agent
8484
web_agent = Agent(
@@ -102,54 +102,53 @@
102102
markdown=True,
103103
)
104104

105-
# print("--------------------------------")
106-
# print("Using IN operator")
107-
# team_with_knowledge.print_response(
108-
# "Tell me about the candidate's work and experience",
109-
# knowledge_filters=[
110-
# (
111-
# IN(
112-
# "user_id",
113-
# [
114-
# "jordan_mitchell",
115-
# "taylor_brooks",
116-
# "morgan_lee",
117-
# "casey_jordan",
118-
# "alex_rivera",
119-
# ],
120-
# )
121-
# )
122-
# ],
123-
# markdown=True,
124-
# )
105+
print("--------------------------------")
106+
print("Using IN operator")
107+
team_with_knowledge.print_response(
108+
"Tell me about the candidate's work and experience",
109+
knowledge_filters=[
110+
(
111+
IN(
112+
"user_id",
113+
[
114+
"jordan_mitchell",
115+
"taylor_brooks",
116+
"morgan_lee",
117+
"casey_jordan",
118+
"alex_rivera",
119+
],
120+
)
121+
)
122+
],
123+
markdown=True,
124+
)
125125

126-
# print("--------------------------------")
127-
# print("Using NOT operator")
128-
# team_with_knowledge.print_response(
129-
# "Tell me about the candidate's work and experience",
130-
# knowledge_filters=[
131-
# AND(
132-
# IN("user_id", ["jordan_mitchell", "taylor_brooks"]),
133-
# NOT(IN("user_id", ["morgan_lee", "casey_jordan", "alex_rivera"])),
134-
# )
135-
# ],
136-
# markdown=True,
137-
# )
126+
print("--------------------------------")
127+
print("Using NOT operator")
128+
team_with_knowledge.print_response(
129+
"Tell me about the candidate's work and experience",
130+
knowledge_filters=[
131+
AND(
132+
IN("user_id", ["jordan_mitchell", "taylor_brooks"]),
133+
NOT(IN("user_id", ["morgan_lee", "casey_jordan", "alex_rivera"])),
134+
)
135+
],
136+
markdown=True,
137+
)
138138

139-
# print("--------------------------------")
140-
# print("Using OR operator")
141-
# team_with_knowledge.print_response(
142-
# "Tell me about the candidate's work and experience",
143-
# knowledge_filters=[
144-
# OR(EQ("user_id", "this candidate does not exist"), EQ("year", 2020))
145-
# ],
146-
# markdown=True,
147-
# )
139+
print("--------------------------------")
140+
print("Using OR operator")
141+
team_with_knowledge.print_response(
142+
"Tell me about the candidate's work and experience",
143+
knowledge_filters=[
144+
OR(EQ("user_id", "this candidate does not exist"), EQ("year", 2020))
145+
],
146+
markdown=True,
147+
)
148148

149149

150150
team_with_knowledge.print_response(
151151
"Tell me about the candidate's work and experience",
152152
knowledge_filters=EQ("user_id", "jordan_mitchell"), # Wrong: plain strings
153153
markdown=True,
154154
)
155-

libs/agno/agno/agent/agent.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5412,7 +5412,6 @@ async def aget_tools(
54125412

54135413
# Consider both run_context.knowledge_filters and knowledge_filters (deprecated)
54145414
run_context.knowledge_filters = run_context.knowledge_filters or knowledge_filters
5415-
54165415
# Connect MCP tools
54175416
await self._connect_mcp_tools()
54185417

@@ -7359,7 +7358,6 @@ def _get_user_message(
73597358
dependencies = run_context.dependencies or dependencies
73607359
metadata = run_context.metadata or metadata
73617360
knowledge_filters = run_context.knowledge_filters or knowledge_filters
7362-
73637361
# Get references from the knowledge base to use in the user message
73647362
references = None
73657363

@@ -8047,7 +8045,7 @@ def get_relevant_docs_from_knowledge(
80478045
filters = valid_filters
80488046
if not filters:
80498047
log_warning("No valid filters remain after validation. Search will proceed without filters.")
8050-
8048+
80518049
if invalid_keys == [] and valid_filters == {}:
80528050
log_warning("No valid filters provided. Search will proceed without filters.")
80538051
filters = None

libs/agno/agno/knowledge/knowledge.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1495,7 +1495,7 @@ def validate_filters(self, filters: Optional[Any]) -> Tuple[Any, List[str]]:
14951495
else:
14961496
invalid_keys.append(key)
14971497
log_debug(f"Invalid filter key: {key} - not present in knowledge base")
1498-
1498+
14991499
elif isinstance(filters, List):
15001500
# Validate that list contains FilterExpr instances
15011501
for i, filter_item in enumerate(filters):
@@ -1506,9 +1506,10 @@ def validate_filters(self, filters: Optional[Any]) -> Tuple[Any, List[str]]:
15061506
f"Use filter expressions like EQ('key', 'value'), IN('key', [values]), "
15071507
f"AND(...), OR(...), NOT(...) from agno.utils.search_filters"
15081508
)
1509+
15091510
# Filter expressions are already validated, return empty dict/list
15101511
# The actual filtering happens in the vector_db layer
1511-
return {}, []
1512+
return filters, []
15121513

15131514
return valid_filters, invalid_keys
15141515

libs/agno/agno/team/team.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8382,6 +8382,7 @@ def get_relevant_docs_from_knowledge(
83828382
) -> Optional[List[Union[Dict[str, Any], str]]]:
83838383
"""Return a list of references from the knowledge base"""
83848384
from agno.knowledge.document import Document
8385+
83858386
if num_documents is None and self.knowledge is not None:
83868387
num_documents = self.knowledge.max_results
83878388

@@ -8399,7 +8400,7 @@ def get_relevant_docs_from_knowledge(
83998400
filters = valid_filters
84008401
if not filters:
84018402
log_warning("No valid filters remain after validation. Search will proceed without filters.")
8402-
8403+
84038404
if invalid_keys == [] and valid_filters == {}:
84048405
log_warning("No valid filters provided. Search will proceed without filters.")
84058406
filters = None

0 commit comments

Comments
 (0)