type check fixes

This commit is contained in:
Lorenze Jay
2024-11-19 12:06:29 -08:00
parent 0c5b6f2a93
commit 705ee16c1c
3 changed files with 17 additions and 8 deletions

View File

@@ -11,15 +11,18 @@ class CSVKnowledgeSource(BaseFileKnowledgeSource):
def load_content(self) -> Dict[Path, str]:
"""Load and preprocess CSV file content."""
super().load_content() # Validate the file path
file_path_str = (
str(self.file_path) if isinstance(self.file_path, Path) else self.file_path
file_path = (
self.file_path[0] if isinstance(self.file_path, list) else self.file_path
)
with open(file_path_str, "r", encoding="utf-8") as csvfile:
file_path = Path(file_path) if isinstance(file_path, str) else file_path
with open(file_path, "r", encoding="utf-8") as csvfile:
reader = csv.reader(csvfile)
content = ""
for row in reader:
content += " ".join(row) + "\n"
return {self.file_path: content}
return {file_path: content}
def add(self) -> None:
"""

View File

@@ -10,9 +10,15 @@ class ExcelKnowledgeSource(BaseFileKnowledgeSource):
"""Load and preprocess Excel file content."""
super().load_content() # Validate the file path
pd = self._import_dependencies()
df = pd.read_excel(self.file_path)
if isinstance(self.file_path, list):
file_path = self.file_path[0]
else:
file_path = self.file_path
df = pd.read_excel(file_path)
content = df.to_csv(index=False)
return {self.file_path: content}
return {file_path: content}
def _import_dependencies(self):
"""Dynamically import dependencies."""

View File

@@ -13,11 +13,11 @@ class JSONKnowledgeSource(BaseFileKnowledgeSource):
super().load_content() # Validate the file path
paths = [self.file_path] if isinstance(self.file_path, Path) else self.file_path
content = {}
content: Dict[Path, str] = {}
for path in paths:
with open(path, "r", encoding="utf-8") as json_file:
data = json.load(json_file)
content = self._json_to_text(data)
content[path] = self._json_to_text(data)
return content
def _json_to_text(self, data: Any, level: int = 0) -> str: