Compare commits

...

6 Commits

Author SHA1 Message Date
Devin AI
2c12b91083 fix: use isinstance for proper type narrowing in Knowledge.query
Co-Authored-By: Joe Moura <joao@crewai.com>
2024-12-27 20:55:45 +00:00
Devin AI
2d23ef7e12 fix: improve type narrowing in Knowledge.query with local variable
Co-Authored-By: Joe Moura <joao@crewai.com>
2024-12-27 20:54:14 +00:00
Devin AI
b56c4c52c0 fix: handle None storage in Knowledge.query for type checks
Co-Authored-By: Joe Moura <joao@crewai.com>
2024-12-27 20:51:50 +00:00
João Moura
81759e8c72 Merge branch 'main' into fix-knowledgestorage-default-instantiation 2024-12-27 17:18:33 -03:00
ericklima-ca
27472ba69e refactor: Change storage field to optional and improve error handling when saving documents 2024-12-26 22:27:19 -04:00
ericklima-ca
25aa774d8c fix: Change storage initialization to None for KnowledgeStorage 2024-12-26 21:30:06 -04:00
3 changed files with 20 additions and 8 deletions

View File

@@ -14,13 +14,13 @@ class Knowledge(BaseModel):
Knowledge is a collection of sources and setup for the vector store to save and query relevant context.
Args:
sources: List[BaseKnowledgeSource] = Field(default_factory=list)
storage: KnowledgeStorage = Field(default_factory=KnowledgeStorage)
storage: Optional[KnowledgeStorage] = Field(default=None)
embedder_config: Optional[Dict[str, Any]] = None
"""
sources: List[BaseKnowledgeSource] = Field(default_factory=list)
model_config = ConfigDict(arbitrary_types_allowed=True)
storage: KnowledgeStorage = Field(default_factory=KnowledgeStorage)
storage: Optional[KnowledgeStorage] = Field(default=None)
embedder_config: Optional[Dict[str, Any]] = None
collection_name: Optional[str] = None
@@ -49,9 +49,15 @@ class Knowledge(BaseModel):
"""
Query across all knowledge sources to find the most relevant information.
Returns the top_k most relevant chunks.
"""
results = self.storage.search(
Raises:
ValueError: If no storage is configured for querying.
"""
storage = self.storage
if not isinstance(storage, KnowledgeStorage):
raise ValueError("No storage found to perform query.")
# Using isinstance check for proper type narrowing
results = storage.search(
query,
limit,
)

View File

@@ -22,7 +22,7 @@ class BaseFileKnowledgeSource(BaseKnowledgeSource, ABC):
default_factory=list, description="The path to the file"
)
content: Dict[Path, str] = Field(init=False, default_factory=dict)
storage: KnowledgeStorage = Field(default_factory=KnowledgeStorage)
storage: Optional[KnowledgeStorage] = Field(default=None)
safe_file_paths: List[Path] = Field(default_factory=list)
@field_validator("file_path", "file_paths", mode="before")
@@ -62,7 +62,10 @@ class BaseFileKnowledgeSource(BaseKnowledgeSource, ABC):
def _save_documents(self):
"""Save the documents to the storage."""
self.storage.save(self.chunks)
if self.storage:
self.storage.save(self.chunks)
else:
raise ValueError("No storage found to save documents.")
def convert_to_path(self, path: Union[Path, str]) -> Path:
"""Convert a path to a Path object."""

View File

@@ -16,7 +16,7 @@ class BaseKnowledgeSource(BaseModel, ABC):
chunk_embeddings: List[np.ndarray] = Field(default_factory=list)
model_config = ConfigDict(arbitrary_types_allowed=True)
storage: KnowledgeStorage = Field(default_factory=KnowledgeStorage)
storage: Optional[KnowledgeStorage] = Field(default=None)
metadata: Dict[str, Any] = Field(default_factory=dict) # Currently unused
collection_name: Optional[str] = Field(default=None)
@@ -46,4 +46,7 @@ class BaseKnowledgeSource(BaseModel, ABC):
Save the documents to the storage.
This method should be called after the chunks and embeddings are generated.
"""
self.storage.save(self.chunks)
if self.storage:
self.storage.save(self.chunks)
else:
raise ValueError("No storage found to save documents.")