Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/crewai/knowledge/source/base_file_knowledge_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@ def _save_documents(self):
"""Save the documents to the storage."""
self.storage.save(self.chunks)

def add(self) -> None:
"""
Process content from files, chunk it, compute embeddings, and save them.
This method is called after content is loaded from files.
"""
for _, text in self.content.items():
new_chunks = self._chunk_text(text)
self.chunks.extend(new_chunks)
self._save_documents()

def convert_to_path(self, path: Union[Path, str]) -> Path:
"""Convert a path to a Path object."""
return Path(KNOWLEDGE_DIRECTORY + "/" + path) if isinstance(path, str) else path
Expand Down
19 changes: 19 additions & 0 deletions tests/knowledge/test_knowledge_source_instantiation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from pathlib import Path
from unittest.mock import patch

import pytest

from crewai.knowledge.source.pdf_knowledge_source import PDFKnowledgeSource


@patch('crewai.knowledge.source.base_file_knowledge_source.BaseFileKnowledgeSource.validate_content')
@patch('crewai.knowledge.source.pdf_knowledge_source.PDFKnowledgeSource.load_content')
def test_pdf_knowledge_source_instantiation(mock_load_content, mock_validate_content, tmp_path):
"""Test that PDFKnowledgeSource can be instantiated without errors."""
mock_load_content.return_value = {}

pdf_path = tmp_path / "test.pdf"
pdf_path.touch() # Create the file

pdf_source = PDFKnowledgeSource(file_paths=[pdf_path])
assert isinstance(pdf_source, PDFKnowledgeSource)
Loading