Skip to content

Commit

Permalink
Merge branch 'feature/delete_points_by_source' of https://github.com/…
Browse files Browse the repository at this point in the history
…AlboCode/core into AlboCode-feature/delete_points_by_source
  • Loading branch information
pieroit committed Sep 27, 2023
2 parents 5f97e8e + 02f1ece commit 702d15a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 17 deletions.
7 changes: 6 additions & 1 deletion core/cat/memory/vector_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,12 @@ def recall_memories_from_text(self, text, metadata=None, k=5, threshold=None):
return self.recall_memories_from_embedding(
query_embedding, metadata=metadata, k=k, threshold=threshold
)

def delete_points_by_metadata_filter(self, metadata=None):
res = self.client.delete(
collection_name=self.collection_name,
points_selector=self._qdrant_filter_from_dict(metadata),
)
return res
# delete point in collection
def delete_points(self, points_ids):
res = self.client.delete(
Expand Down
55 changes: 39 additions & 16 deletions core/cat/routes/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
# GET memories from recall
@router.get("/recall/")
async def recall_memories_from_text(
request: Request,
text: str = Query(description="Find memories similar to this text."),
k: int = Query(default=100, description="How many memories to return."),
user_id: str = Query(default="user", description="User id."),
request: Request,
text: str = Query(description="Find memories similar to this text."),
k: int = Query(default=100, description="How many memories to return."),
user_id: str = Query(default="user", description="User id."),
) -> Dict:
"""Search k memories similar to given text."""

Expand All @@ -36,7 +36,7 @@ async def recall_memories_from_text(
}
else:
user_filter = None

memories = vector_memory.collections[c].recall_memories_from_embedding(
query_embedding,
k=k,
Expand All @@ -46,7 +46,7 @@ async def recall_memories_from_text(
recalled[c] = []
for metadata, score, vector, id in memories:
memory_dict = dict(metadata)
memory_dict.pop("lc_kwargs", None) # langchain stuff, not needed
memory_dict.pop("lc_kwargs", None) # langchain stuff, not needed
memory_dict["id"] = id
memory_dict["score"] = float(score)
memory_dict["vector"] = vector
Expand All @@ -55,7 +55,7 @@ async def recall_memories_from_text(
return {
"query": query,
"vectors": {
"embedder": str(ccat.embedder.__class__.__name__), # TODO: should be the config class name
"embedder": str(ccat.embedder.__class__.__name__), # TODO: should be the config class name
"collections": recalled
}
}
Expand Down Expand Up @@ -87,7 +87,7 @@ async def get_collections(request: Request) -> Dict:
# DELETE all collections
@router.delete("/collections/")
async def wipe_collections(
request: Request,
request: Request,
) -> Dict:
"""Delete and create all collections"""

Expand All @@ -111,7 +111,8 @@ async def wipe_collections(

# DELETE one collection
@router.delete("/collections/{collection_id}/")
async def wipe_single_collection(request: Request, collection_id: str) -> Dict:
async def wipe_single_collection(request: Request,
collection_id: str) -> Dict:
"""Delete and recreate a collection"""

ccat = request.app.state.ccat
Expand All @@ -127,7 +128,6 @@ async def wipe_single_collection(request: Request, collection_id: str) -> Dict:

to_return = {}


ret = vector_memory.vector_db.delete_collection(collection_name=collection_id)
to_return[collection_id] = ret

Expand All @@ -143,15 +143,15 @@ async def wipe_single_collection(request: Request, collection_id: str) -> Dict:
# DELETE memories
@router.delete("/collections/{collection_id}/points/{memory_id}/")
async def wipe_memory_point(
request: Request,
collection_id: str,
memory_id: str
request: Request,
collection_id: str,
memory_id: str
) -> Dict:
"""Delete a specific point in memory"""

ccat = request.app.state.ccat
vector_memory = ccat.memory.vectors

# check if collection exists
collections = list(vector_memory.collections.keys())
if collection_id not in collections:
Expand Down Expand Up @@ -179,10 +179,33 @@ async def wipe_memory_point(
}


@router.delete("/collections/{collection_id}/points")
async def wipe_memory_points_by_source(
request: Request,
collection_id: str,
source: str = Query(description="Source of the points that want to remove"),
) -> Dict:
"""Delete a specific point in memory"""

ccat = request.app.state.ccat
vector_memory = ccat.memory.vectors

metadata = {
"source": source
}

# delete point
points = vector_memory.collections[collection_id].delete_points_by_metadata_filter(metadata)

return {
"deleted": points
}


# DELETE conversation history from working memory
@router.delete("/conversation_history/")
async def wipe_conversation_history(
request: Request,
request: Request,
) -> Dict:
"""Delete conversation history from working memory"""

Expand All @@ -191,4 +214,4 @@ async def wipe_conversation_history(

return {
"deleted": True,
}
}

0 comments on commit 702d15a

Please sign in to comment.