Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Implement filtering by metadata #337

Merged
merged 3 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions agents-api/agents_api/models/docs/list_docs.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""This module contains functions for querying document-related data from the 'cozodb' database using datalog queries."""

from typing import Literal
import json
from typing import Literal, Any
from uuid import UUID


Expand Down Expand Up @@ -35,9 +36,17 @@ def ensure_owner_exists_query(
def list_docs_snippets_by_owner_query(
owner_type: Literal["user", "agent"],
owner_id: UUID,
metadata_filter: dict[str, Any] = {},
) -> tuple[str, dict]:
owner_id = str(owner_id)

metadata_filter_str = ", ".join(
creatorrr marked this conversation as resolved.
Show resolved Hide resolved
[
f"metadata->{json.dumps(k)} == {json.dumps(v)}"
for k, v in metadata_filter.items()
]
)

# Query to retrieve document snippets by owner (user or agent)
query = f"""
{{
Expand Down Expand Up @@ -65,7 +74,8 @@ def list_docs_snippets_by_owner_query(
snippet_idx,
title,
snippet,
}}
}},
{metadata_filter_str}
whiterabbit1983 marked this conversation as resolved.
Show resolved Hide resolved
}}"""

return (query, {"owner_id": owner_id})
12 changes: 2 additions & 10 deletions agents-api/agents_api/routers/agents/routers.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,19 +355,10 @@ async def list_docs(
detail="metadata_filter is not a valid JSON",
)

# TODO: Implement metadata filter
if metadata_filter:
raise HTTPException(
status_code=status.HTTP_501_NOT_IMPLEMENTED,
detail="metadata_filter is not implemented",
)

if not len(list(ensure_owner_exists_query("agent", agent_id).iterrows())):
raise AgentNotFoundError("", agent_id)

resp = list_docs_snippets_by_owner_query(
owner_type="agent",
owner_id=agent_id,
metadata_filter=metadata_filter,
)

return DocsList(
Expand All @@ -377,6 +368,7 @@ async def list_docs(
id=row["doc_id"],
title=row["title"],
content=row["snippet"],
metadata=row.get("metadata")
)
for _, row in resp.iterrows()
]
Expand Down
8 changes: 1 addition & 7 deletions agents-api/agents_api/routers/users/routers.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,19 +288,13 @@ async def list_docs(
detail="metadata_filter is not a valid JSON",
)

# TODO: Implement metadata filter
if metadata_filter:
raise HTTPException(
status_code=status.HTTP_501_NOT_IMPLEMENTED,
detail="metadata_filter is not implemented",
)

if not len(list(ensure_owner_exists_query("user", user_id).iterrows())):
raise UserNotFoundError("", user_id)

resp = list_docs_snippets_by_owner_query(
owner_type="user",
owner_id=user_id,
metadata_filter=metadata_filter,
)

return DocsList(
Expand Down
Loading