forked from stanford-oval/storm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Related to stanford-oval#167 Add support for PGVector and GraphRAG retrievals. * **knowledge_storm/rm.py**: - Add `PGVectorRetriever` class to support PGVector retrieval. - Add `GraphRAGRM` class to support GraphRAG retrieval. - Update import statements to include necessary libraries for PGVector and GraphRAG. * **examples/storm_examples/run_storm_wiki_claude.py** and **examples/storm_examples/run_storm_wiki_gemini.py**: - Update environment variable descriptions to include PGVector and GraphRAG. - Add cases for `pgvector` and `graphrag` retrievers in the main function. * **knowledge_storm/collaborative_storm/engine.py**: - Add `pgvector_retriever` attribute to `CollaborativeStormLMConfigs` class. - Add `set_pgvector_retriever` method to `CollaborativeStormLMConfigs` class. - Update import statements to include `PGVectorRetriever`. * **tests/test_pgvector_retriever.py**: - Add unit tests for the new `PGVectorRetriever` class.
- Loading branch information
1 parent
5a61eec
commit c31981e
Showing
5 changed files
with
254 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import unittest | ||
from unittest.mock import patch, MagicMock | ||
from knowledge_storm.rm import PGVectorRetriever | ||
|
||
class TestPGVectorRetriever(unittest.TestCase): | ||
|
||
@patch('knowledge_storm.rm.create_engine') | ||
@patch('knowledge_storm.rm.HuggingFaceEmbeddings') | ||
def setUp(self, MockHuggingFaceEmbeddings, MockCreateEngine): | ||
self.mock_model = MockHuggingFaceEmbeddings.return_value | ||
self.mock_engine = MockCreateEngine.return_value | ||
self.retriever = PGVectorRetriever( | ||
db_url='postgresql://user:password@localhost/dbname', | ||
table_name='documents', | ||
embedding_model='BAAI/bge-m3', | ||
k=3 | ||
) | ||
|
||
@patch('knowledge_storm.rm.PGVectorRetriever.Session') | ||
def test_forward(self, MockSession): | ||
mock_session = MockSession.return_value | ||
mock_query = MagicMock() | ||
mock_session.query.return_value.order_by.return_value.limit.return_value.all.return_value = [ | ||
MagicMock(description='desc1', content='content1', title='title1', url='url1'), | ||
MagicMock(description='desc2', content='content2', title='title2', url='url2') | ||
] | ||
self.retriever.model.embed_query.return_value = [0.1, 0.2, 0.3] | ||
|
||
results = self.retriever.forward('test query', []) | ||
|
||
self.assertEqual(len(results), 2) | ||
self.assertEqual(results[0]['description'], 'desc1') | ||
self.assertEqual(results[0]['snippets'], ['content1']) | ||
self.assertEqual(results[0]['title'], 'title1') | ||
self.assertEqual(results[0]['url'], 'url1') | ||
|
||
def test_get_usage_and_reset(self): | ||
self.retriever.usage = 5 | ||
usage = self.retriever.get_usage_and_reset() | ||
self.assertEqual(usage, {'PGVectorRetriever': 5}) | ||
self.assertEqual(self.retriever.usage, 0) | ||
|
||
if __name__ == '__main__': | ||
unittest.main() |