diff --git a/src/crewai/knowledge/source/base_file_knowledge_source.py b/src/crewai/knowledge/source/base_file_knowledge_source.py index 7a82a1108..7765486bf 100644 --- a/src/crewai/knowledge/source/base_file_knowledge_source.py +++ b/src/crewai/knowledge/source/base_file_knowledge_source.py @@ -1,12 +1,10 @@ from abc import ABC, abstractmethod from pathlib import Path -from typing import Dict, List, Optional, Union from pydantic import Field, field_validator from crewai.knowledge.source.base_knowledge_source import BaseKnowledgeSource from crewai.knowledge.storage.knowledge_storage import KnowledgeStorage -from crewai.utilities.constants import KNOWLEDGE_DIRECTORY from crewai.utilities.logger import Logger from crewai.utilities.paths import get_knowledge_directory @@ -15,18 +13,19 @@ class BaseFileKnowledgeSource(BaseKnowledgeSource, ABC): """Base class for knowledge sources that load content from files.""" _logger: Logger = Logger(verbose=True) - file_path: Optional[Union[Path, List[Path], str, List[str]]] = Field( + file_path: Path | list[Path] | str | list[str] | None = Field( default=None, description="[Deprecated] The path to the file. Use file_paths instead.", ) - file_paths: Optional[Union[Path, List[Path], str, List[str]]] = Field( + file_paths: Path | list[Path] | str | list[str] | None = Field( default_factory=list, description="The path to the file" ) - content: Dict[Path, str] = Field(init=False, default_factory=dict) - storage: Optional[KnowledgeStorage] = Field(default=None) - safe_file_paths: List[Path] = Field(default_factory=list) + content: dict[Path, str] = Field(init=False, default_factory=dict) + storage: KnowledgeStorage | None = Field(default=None) + safe_file_paths: list[Path] = Field(default_factory=list) @field_validator("file_path", "file_paths", mode="before") + @classmethod def validate_file_path(cls, v, info): """Validate that at least one of file_path or file_paths is provided.""" # Single check if both are None, O(1) instead of nested conditions @@ -47,9 +46,8 @@ class BaseFileKnowledgeSource(BaseKnowledgeSource, ABC): self.content = self.load_content() @abstractmethod - def load_content(self) -> Dict[Path, str]: + def load_content(self) -> dict[Path, str]: """Load and preprocess file content. Should be overridden by subclasses. Assume that the file path is relative to the project root in the knowledge directory.""" - pass def validate_content(self): """Validate the paths.""" @@ -75,11 +73,11 @@ class BaseFileKnowledgeSource(BaseKnowledgeSource, ABC): else: raise ValueError("No storage found to save documents.") - def convert_to_path(self, path: Union[Path, str]) -> Path: + def convert_to_path(self, path: Path | str) -> Path: """Convert a path to a Path object.""" return Path(get_knowledge_directory() + "/" + path) if isinstance(path, str) else path - def _process_file_paths(self) -> List[Path]: + def _process_file_paths(self) -> list[Path]: """Convert file_path to a list of Path objects.""" if hasattr(self, "file_path") and self.file_path is not None: @@ -94,7 +92,7 @@ class BaseFileKnowledgeSource(BaseKnowledgeSource, ABC): raise ValueError("Your source must be provided with a file_paths: []") # Convert single path to list - path_list: List[Union[Path, str]] = ( + path_list: list[Path | str] = ( [self.file_paths] if isinstance(self.file_paths, (str, Path)) else list(self.file_paths) diff --git a/src/crewai/knowledge/source/excel_knowledge_source.py b/src/crewai/knowledge/source/excel_knowledge_source.py index 363f5d4e2..d79c50369 100644 --- a/src/crewai/knowledge/source/excel_knowledge_source.py +++ b/src/crewai/knowledge/source/excel_knowledge_source.py @@ -26,6 +26,7 @@ class ExcelKnowledgeSource(BaseKnowledgeSource): safe_file_paths: list[Path] = Field(default_factory=list) @field_validator("file_path", "file_paths", mode="before") + @classmethod def validate_file_path(cls, v, info): """Validate that at least one of file_path or file_paths is provided.""" # Single check if both are None, O(1) instead of nested conditions