Skip to content

Commit

Permalink
Feat: CSV Url Knowledgebase
Browse files Browse the repository at this point in the history
  • Loading branch information
manthanguptaa committed Dec 11, 2024
1 parent 51e8ac0 commit e29ae0d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
18 changes: 18 additions & 0 deletions phi/document/reader/csv_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,21 @@ def read(self, file: Union[Path, IO[Any]], delimiter: str = ",", quotechar: str
except Exception as e:
logger.error(f"Error reading: {file.name if isinstance(file, IO) else file}: {e}")
return []


class CSVUrlReader(Reader):
"""Reader for CSV files"""

def read(self, url: str) -> List[Document]:
if not url:
raise ValueError("No URL provided")

try:
import httpx
except ImportError:
raise ImportError("`httpx` not installed")

logger.info(f"Reading: {url}")
response = httpx.get(url)

return CSVReader().read(file=io.StringIO(response.text))
12 changes: 11 additions & 1 deletion phi/knowledge/csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from typing import Union, List, Iterator

from phi.document import Document
from phi.document.reader.csv_reader import CSVReader
from phi.document.reader.csv_reader import CSVReader, CSVUrlReader
from phi.knowledge.agent import AgentKnowledge


Expand All @@ -26,3 +26,13 @@ def document_lists(self) -> Iterator[List[Document]]:
yield self.reader.read(file=_csv)
elif _csv_path.exists() and _csv_path.is_file() and _csv_path.suffix == ".csv":
yield self.reader.read(file=_csv_path)


class CSVUrlKnowledgeBase(AgentKnowledge):
url: List[str]
reader: CSVUrlReader = CSVUrlReader()

@property
def document_lists(self) -> Iterator[List[Document]]:
for url in self.url:
yield self.reader.read(url=url)

0 comments on commit e29ae0d

Please sign in to comment.