Skip to content

Commit

Permalink
Add test for VectorDBMemory
Browse files Browse the repository at this point in the history
  • Loading branch information
dandansamax committed Aug 28, 2023
1 parent 8d48140 commit 89a5a89
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 3 deletions.
1 change: 1 addition & 0 deletions camel/memory/vector_storage/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ def delete_vectors(
@abstractmethod
def search(
self,
collection: str,
query_vector: VectorRecord,
limit: int,
) -> List[VectorRecord]:
Expand Down
5 changes: 3 additions & 2 deletions camel/memory/vector_storage/qdrant.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def delete_vectors(
raise RuntimeError("Deleting vector records should "
"contains either id or vector.")
search_result = self.client.search(
collection_name="test_collection",
collection_name=collection,
query_vector=v.vector,
with_payload=False,
limit=1,
Expand All @@ -142,14 +142,15 @@ def delete_vectors(

def search(
self,
collection: str,
query_vector: VectorRecord,
limit: int = 3,
) -> List[VectorRecord]:
# TODO: filter
if query_vector.vector is None:
raise RuntimeError("Searching vector cannot be None")
search_result = self.client.search(
collection_name="test_collection",
collection_name=collection,
query_vector=query_vector.vector,
with_payload=True,
limit=limit,
Expand Down
6 changes: 5 additions & 1 deletion camel/memory/vectordb_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,11 @@ def read(
"Reading vector database memeory without message input is not "
"allowed.")
query_vector = self.embedding.embed(current_state.content)
results = self.storage.search(VectorRecord(vector=query_vector), limit)
results = self.storage.search(
self.collection_name,
VectorRecord(vector=query_vector),
limit,
)
return [
BaseMessage(**res.payload) for res in results
if res.payload is not None
Expand Down
48 changes: 48 additions & 0 deletions test/memory/test_vectordb_memory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
# Licensed under the Apache License, Version 2.0 (the “License”);
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an “AS IS” BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========

from dataclasses import asdict

from camel.memory.vectordb_memory import VectorDBMemory
from camel.messages import BaseMessage
from camel.typing import RoleType


# flake8: noqa :E501
def testVectorDBMemeory():
texts = [
"Every once in a long while, a game comes along that is so memorable, exciting, fresh, and well-written that it sets a new high-water mark for an entire genre.",
"Baldur's Gate 3 is such an achievement for the tabletop roleplaying-inspired, swords and sorcery adventuring that its BioWare-made CRPG predecessors helped popularize decades ago.",
"Larian Studios has turned this corner of Dungeons & Dragons' Forgotten Realms into a beautiful, detailed world stocked with too many fully-realized, powerfully written, and skillfully voiced characters to count.",
"There are heart-wrenching choices to be made, alliances to be forged, bears to be romanced, and a vast diversity of interesting, challenging turn-based combat encounters.",
"I didn't merely enjoy my 130-plus hours on this journey.",
"I fell in love.",
]
memory = VectorDBMemory()

messages = [
BaseMessage(
"AI user",
role_type=RoleType.USER,
meta_dict={"idx": idx},
content=sentence,
) for idx, sentence in enumerate(texts)
]
memory.write(messages)

query_message = BaseMessage(
"AI user", role_type=RoleType.USER, meta_dict=None,
content="How long have I been playing this game?")
search_results = memory.read(query_message)
assert asdict(search_results[0]) == asdict(messages[4])

0 comments on commit 89a5a89

Please sign in to comment.