Mocking VectorStore class #30832
Replies: 1 comment 5 replies
-
To create a stateful class MockVectorStore:
def __init__(self):
self.documents = []
self.index = {}
async def aadd_documents(self, docs):
ids = []
for doc in docs:
doc_id = f"doc_{len(self.documents)}"
self.documents.append(doc)
self.index[doc_id] = doc
ids.append(doc_id)
return ids
async def asimilarity_search(self, query, k=None, filter=None):
# For simplicity, return all documents as similar
results = [
{
"page_content": doc,
"metadata": {"some": "metadata", "value": {"body": doc}}
}
for doc in self.documents
]
if filter:
# Apply filtering logic based on metadata
results = [result for result in results if all(
result["metadata"].get(key) == value for key, value in filter.items()
)]
return results[:k] if k else results
# Example usage
mock_store = MockVectorStore()
await mock_store.aadd_documents(["This is a new test", "This is a second test"])
results = await mock_store.asimilarity_search("Who was inspired by Ada Lovelace?", k=2)
print(results) This To continue talking to Dosu, mention @dosu. Help Dosu learn! Give it feedback: Great Response | Irrelevant Answer | Incorrect Sources | Too Verbose | Hallucination | Bug Report | Other |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
For my testing, I want to avoid creating an instance of postgres to test my code. Do you have any suggestions on how to create a stateful MockVectorStore class that replicates the behavior of the class? Primarily I want to mock the aadd_documents and asimilarity_search methods.
Beta Was this translation helpful? Give feedback.
All reactions