From 1d3fb97ebaef6a0b217afa25a90e4f155d430fa6 Mon Sep 17 00:00:00 2001 From: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Mon, 17 Feb 2025 08:16:19 +0000 Subject: [PATCH] fix: Initialize storage in StringKnowledgeSource - Add storage initialization in model_post_init - Add test coverage for StringKnowledgeSource - Fixes #2150 Co-Authored-By: Joe Moura --- .../source/string_knowledge_source.py | 6 ++++- tests/knowledge/knowledge_test.py | 25 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/crewai/knowledge/source/string_knowledge_source.py b/src/crewai/knowledge/source/string_knowledge_source.py index 614303b1f..f8905407b 100644 --- a/src/crewai/knowledge/source/string_knowledge_source.py +++ b/src/crewai/knowledge/source/string_knowledge_source.py @@ -12,8 +12,12 @@ class StringKnowledgeSource(BaseKnowledgeSource): collection_name: Optional[str] = Field(default=None) def model_post_init(self, _): - """Post-initialization method to validate content.""" + """Post-initialization method to validate content and initialize storage.""" self.validate_content() + if self.storage is None: + from crewai.knowledge.storage.knowledge_storage import KnowledgeStorage + self.storage = KnowledgeStorage(collection_name=self.collection_name) + self.storage.initialize_knowledge_storage() def validate_content(self): """Validate string content.""" diff --git a/tests/knowledge/knowledge_test.py b/tests/knowledge/knowledge_test.py index fad2d2513..696830875 100644 --- a/tests/knowledge/knowledge_test.py +++ b/tests/knowledge/knowledge_test.py @@ -37,6 +37,28 @@ def reset_knowledge_storage(mock_vector_db): yield +def test_string_knowledge_source(mock_vector_db): + """Test StringKnowledgeSource with simple text content.""" + content = "Users name is John. He is 30 years old and lives in San Francisco." + string_source = StringKnowledgeSource(content=content) + mock_vector_db.sources = [string_source] + mock_vector_db.query.return_value = [{"context": content, "score": 0.9}] + + # Test initialization + assert string_source.content == content + + # Test adding content + string_source.add() + assert len(string_source.chunks) > 0 + + # Test querying + query = "Where does John live?" + results = mock_vector_db.query(query) + assert len(results) > 0 + assert "San Francisco" in results[0]["context"] + mock_vector_db.query.assert_called_once() + + def test_single_short_string(mock_vector_db): # Create a knowledge base with a single short string content = "Brandon's favorite color is blue and he likes Mexican food." @@ -418,6 +440,9 @@ def test_hybrid_string_and_files(mock_vector_db, tmpdir): mock_vector_db.query.assert_called_once() + + + def test_pdf_knowledge_source(mock_vector_db): # Get the directory of the current file current_dir = Path(__file__).parent