Skip to content

Commit d353e07

Browse files
kausmeowskepler
andauthored
fix: add missing async method to Combined KB (#3412)
## Summary The CombinedKnowledgeBase is missing the implementation for the `async_document_lists` method. Plus, the abstract method is not marked as `async`. ## Type of change - [x] Bug fix - [ ] New feature - [ ] Breaking change - [ ] Improvement - [ ] Model update - [ ] Other: --- ## Checklist - [ ] Code complies with style guidelines - [ ] Ran format/validation scripts (`./scripts/format.sh` and `./scripts/validate.sh`) - [ ] Self-review completed - [ ] Documentation updated (comments, docstrings) - [x] Examples and guides: Relevant cookbook examples have been included or updated (if applicable) - [ ] Tested in clean environment - [ ] Tests added/updated (if applicable) --------- Co-authored-by: Fabio Natanael Kepler <fabio@kepler.pro.br>
1 parent d44d8bc commit d353e07

File tree

3 files changed

+97
-3
lines changed

3 files changed

+97
-3
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import asyncio
2+
from pathlib import Path
3+
4+
from agno.agent import Agent
5+
from agno.knowledge.combined import CombinedKnowledgeBase
6+
from agno.knowledge.csv import CSVKnowledgeBase
7+
from agno.knowledge.pdf import PDFKnowledgeBase
8+
from agno.knowledge.pdf_url import PDFUrlKnowledgeBase
9+
from agno.knowledge.website import WebsiteKnowledgeBase
10+
from agno.vectordb.pgvector import PgVector
11+
12+
db_url = "postgresql+psycopg://ai:ai@localhost:5532/ai"
13+
14+
# Create CSV knowledge base
15+
csv_kb = CSVKnowledgeBase(
16+
path=Path("data/csvs"),
17+
vector_db=PgVector(
18+
table_name="csv_documents",
19+
db_url=db_url,
20+
),
21+
)
22+
23+
# Create PDF URL knowledge base
24+
pdf_url_kb = PDFUrlKnowledgeBase(
25+
urls=["https://agno-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf"],
26+
vector_db=PgVector(
27+
table_name="pdf_documents",
28+
db_url=db_url,
29+
),
30+
)
31+
32+
# Create Website knowledge base
33+
website_kb = WebsiteKnowledgeBase(
34+
urls=["https://docs.agno.com/introduction"],
35+
max_links=10,
36+
vector_db=PgVector(
37+
table_name="website_documents",
38+
db_url=db_url,
39+
),
40+
)
41+
42+
# Create Local PDF knowledge base
43+
local_pdf_kb = PDFKnowledgeBase(
44+
path="data/pdfs",
45+
vector_db=PgVector(
46+
table_name="pdf_documents",
47+
db_url=db_url,
48+
),
49+
)
50+
51+
# Combine knowledge bases
52+
knowledge_base = CombinedKnowledgeBase(
53+
sources=[
54+
csv_kb,
55+
pdf_url_kb,
56+
website_kb,
57+
local_pdf_kb,
58+
],
59+
vector_db=PgVector(
60+
table_name="combined_documents",
61+
db_url=db_url,
62+
),
63+
)
64+
65+
# Initialize the Agent with the combined knowledge base
66+
agent = Agent(
67+
knowledge=knowledge_base,
68+
search_knowledge=True,
69+
)
70+
71+
if __name__ == "__main__":
72+
# Comment out after first run
73+
asyncio.run(knowledge_base.aload(recreate=False))
74+
75+
# Create and use the agent
76+
asyncio.run(
77+
agent.aprint_response(
78+
"Ask me about something from the knowledge base", markdown=True
79+
)
80+
)

libs/agno/agno/knowledge/agent.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def document_lists(self) -> Iterator[List[Document]]:
4444
raise NotImplementedError
4545

4646
@property
47-
def async_document_lists(self) -> AsyncIterator[List[Document]]:
47+
async def async_document_lists(self) -> AsyncIterator[List[Document]]:
4848
"""Iterator that yields lists of documents in the knowledge base
4949
Each object yielded by the iterator is a list of documents.
5050
"""
@@ -167,7 +167,7 @@ async def aload(
167167

168168
log_info("Loading knowledge base")
169169
num_documents = 0
170-
async for document_list in self.async_document_lists:
170+
async for document_list in await self.async_document_lists:
171171
documents_to_load = document_list
172172
# Track metadata for filtering capabilities
173173
for doc in document_list:

libs/agno/agno/knowledge/combined.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Iterator, List
1+
from typing import AsyncIterator, Iterator, List
22

33
from agno.document import Document
44
from agno.knowledge.agent import AgentKnowledge
@@ -20,3 +20,17 @@ def document_lists(self) -> Iterator[List[Document]]:
2020
for kb in self.sources:
2121
log_debug(f"Loading documents from {kb.__class__.__name__}")
2222
yield from kb.document_lists
23+
24+
@property
25+
async def async_document_lists(self) -> AsyncIterator[List[Document]]:
26+
"""Iterate over knowledge bases and yield lists of documents.
27+
Each object yielded by the iterator is a list of documents.
28+
29+
Returns:
30+
Iterator[List[Document]]: Iterator yielding list of documents
31+
"""
32+
33+
for kb in self.sources:
34+
log_debug(f"Loading documents from {kb.__class__.__name__}")
35+
async for document in await kb.async_document_lists:
36+
yield document

0 commit comments

Comments
 (0)