Skip to content

Commit

Permalink
Merge branch 'valentimarco-develop' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
pieroit committed Sep 25, 2024
2 parents 1cba200 + 3bbaa50 commit 8983823
Show file tree
Hide file tree
Showing 9 changed files with 202 additions and 149 deletions.
6 changes: 3 additions & 3 deletions core/cat/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
llm,
embedder,
auth_handler,
memory,
plugins,
upload,
websocket,
)
from cat.routes.memory.memory_router import memory_router
from cat.routes.static import admin, static
from cat.routes.openapi import get_openapi_configuration_function
from cat.looking_glass.cheshire_cat import CheshireCat
Expand Down Expand Up @@ -79,7 +79,7 @@ def custom_generate_unique_id(route: APIRoute):
)

# Add routers to the middleware stack.
cheshire_cat_api.include_router(base.router, tags=["Status"])
cheshire_cat_api.include_router(base.router, tags=["Home"])
cheshire_cat_api.include_router(auth.router, tags=["User Auth"], prefix="/auth")
cheshire_cat_api.include_router(users.router, tags=["Users"], prefix="/users")
cheshire_cat_api.include_router(settings.router, tags=["Settings"], prefix="/settings")
Expand All @@ -88,7 +88,7 @@ def custom_generate_unique_id(route: APIRoute):
)
cheshire_cat_api.include_router(embedder.router, tags=["Embedder"], prefix="/embedder")
cheshire_cat_api.include_router(plugins.router, tags=["Plugins"], prefix="/plugins")
cheshire_cat_api.include_router(memory.router, tags=["Memory"], prefix="/memory")
cheshire_cat_api.include_router(memory_router, prefix="/memory")
cheshire_cat_api.include_router(
upload.router, tags=["Rabbit Hole"], prefix="/rabbithole"
)
Expand Down
10 changes: 10 additions & 0 deletions core/cat/memory/vector_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,13 @@ def connect_to_vector_memory(self) -> None:
https=qdrant_https,
api_key=qdrant_api_key,
)

def delete_collection(self, collection_name: str):
"""Delete specific vector collection"""

return self.vector_db.delete_collection(collection_name)

def get_collection(self, collection_name: str):
"""Get collection info"""

return self.vector_db.get_collection(collection_name)
16 changes: 12 additions & 4 deletions core/cat/memory/vector_memory_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,19 +207,19 @@ def delete_points_by_metadata_filter(self, metadata=None):
)
return res

# delete point in collection
def delete_points(self, points_ids):
"""Delete point in collection"""
res = self.client.delete(
collection_name=self.collection_name,
points_selector=points_ids,
)
return res

# retrieve similar memories from embedding
def recall_memories_from_embedding(
self, embedding, metadata=None, k=5, threshold=None
):
# retrieve memories
"""Retrieve similar memories from embedding"""

memories = self.client.search(
collection_name=self.collection_name,
query_vector=embedding,
Expand Down Expand Up @@ -257,13 +257,21 @@ def recall_memories_from_embedding(
# doc.lc_kwargs = None

return langchain_documents_from_points

def get_points(self, ids: List[str]):
"""Get points by their ids."""
return self.client.retrieve(
collection_name=self.collection_name,
ids=ids,
with_vectors=True,
)

# retrieve all the points in the collection with an optional offset and limit.
def get_all_points(
self,
limit: int = 10000,
offset: str | None = None
):
"""Retrieve all the points in the collection with an optional offset and limit."""

# retrieving the points
all_points, next_page_offset = self.client.scroll(
Expand Down
2 changes: 1 addition & 1 deletion core/cat/routes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

# server status
@router.get("/")
async def home(
async def status(
stray=Depends(HTTPAuth(AuthResource.STATUS, AuthPermission.READ)),
) -> Dict:
"""Server status"""
Expand Down
88 changes: 88 additions & 0 deletions core/cat/routes/memory/collections.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
from typing import Dict
from fastapi import Request, APIRouter, HTTPException, Depends

from cat.looking_glass.cheshire_cat import CheshireCat
from cat.auth.connection import HTTPAuth
from cat.auth.permissions import AuthPermission, AuthResource
from cat.memory.vector_memory import VectorMemory
from cat.looking_glass.stray_cat import StrayCat

router = APIRouter()

# GET collection list with some metadata
@router.get("/collections")
async def get_collections(
request: Request,
stray: StrayCat = Depends(HTTPAuth(AuthResource.MEMORY, AuthPermission.READ))
) -> Dict:
"""Get list of available collections"""

vector_memory: VectorMemory = stray.memory.vectors
collections = list(vector_memory.collections.keys())

collections_metadata = []
for c in collections:
coll_meta = vector_memory.get_collection(c)
collections_metadata.append({
"name": c,
"vectors_count": coll_meta.points_count
})

return {"collections": collections_metadata}


# DELETE all collections
@router.delete("/collections")
async def wipe_collections(
request: Request,
stray: StrayCat = Depends(HTTPAuth(AuthResource.MEMORY, AuthPermission.DELETE)),
) -> Dict:
"""Delete and create all collections"""

vector_memory: VectorMemory = stray.memory.vectors
collections = list(vector_memory.collections.keys())

to_return = {}
for c in collections:
ret = vector_memory.delete_collection(c)
to_return[c] = ret

ccat: CheshireCat = request.app.state.ccat
ccat.load_memory() # recreate the long term memories
ccat.mad_hatter.find_plugins()

return {
"deleted": to_return,
}


# DELETE one collection
@router.delete("/collections/{collection_id}")
async def wipe_single_collection(
request: Request,
collection_id: str,
stray: StrayCat = Depends(HTTPAuth(AuthResource.MEMORY, AuthPermission.DELETE)),
) -> Dict:
"""Delete and recreate a collection"""

vector_memory: VectorMemory = stray.memory.vectors
collections = list(vector_memory.collections.keys())

if collection_id not in collections:
raise HTTPException(
status_code=400, detail={"error": "Collection does not exist."}
)

to_return = {}
ret = vector_memory.delete_collection(collection_id)
to_return[collection_id] = ret

ccat: CheshireCat = request.app.state.ccat
ccat.load_memory() # recreate the long term memories
ccat.mad_hatter.find_plugins()

return {
"deleted": to_return,
}


39 changes: 39 additions & 0 deletions core/cat/routes/memory/convo_history.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@


from typing import Dict
from fastapi import Request, APIRouter, Depends

from cat.auth.connection import HTTPAuth
from cat.auth.permissions import AuthPermission, AuthResource
from cat.looking_glass.stray_cat import StrayCat


router = APIRouter()


# DELETE conversation history from working memory
@router.delete("/conversation_history")
async def wipe_conversation_history(
request: Request,
stray: StrayCat = Depends(HTTPAuth(AuthResource.MEMORY, AuthPermission.DELETE)),
) -> Dict:
"""Delete the specified user's conversation history from working memory"""

stray.working_memory.history = []

return {
"deleted": True,
}


# GET conversation history from working memory
@router.get("/conversation_history")
async def get_conversation_history(
request: Request,
stray: StrayCat = Depends(HTTPAuth(AuthResource.MEMORY, AuthPermission.READ)),
) -> Dict:
"""Get the specified user's conversation history from working memory"""

return {"history": stray.working_memory.history}


11 changes: 11 additions & 0 deletions core/cat/routes/memory/memory_router.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from fastapi import APIRouter

from .points import router as points_router
from .collections import router as collections_router
from .convo_history import router as convo_history_router

memory_router = APIRouter()

memory_router.include_router(points_router, tags=["Vector Memory - Points"])
memory_router.include_router(collections_router, tags=["Vector Memory - Collections"])
memory_router.include_router(convo_history_router, tags=["Working Memory - Current Conversation"])
Loading

0 comments on commit 8983823

Please sign in to comment.