diff --git a/agentuniverse/agent/action/knowledge/reader/file/txt_reader.py b/agentuniverse/agent/action/knowledge/reader/file/txt_reader.py new file mode 100644 index 00000000..bd285899 --- /dev/null +++ b/agentuniverse/agent/action/knowledge/reader/file/txt_reader.py @@ -0,0 +1,39 @@ + +from pathlib import Path +from typing import List, Optional, Dict + +from agentuniverse.agent.action.knowledge.reader.reader import Reader +from agentuniverse.agent.action.knowledge.store.document import Document + + +class LineTxtReader(Reader): + + def load_data(self, fpath: Path, ext_info: Optional[Dict] = None) -> List[Document]: + dlist = [] + + with open(fpath, 'r', encoding='utf-8') as file: + + metadata = {"file_name": file.name} + if ext_info is not None: + metadata.update(ext_info) + + for line in file: + dlist.append(Document(text=line, metadata=metadata or {})) + + return dlist + + +class TxtReader(Reader): + """Txt reader.""" + + def load_data(self, fpath: Path, ext_info: Optional[Dict] = None) -> List[Document]: + + with open(fpath, 'r', encoding='utf-8') as file: + + metadata = {"file_name": file.name} + if ext_info is not None: + metadata.update(ext_info) + + txt = file.read() + + return [Document(text=txt, metadata=metadata or {})]