Skip to content

Commit

Permalink
fix(agents-api): Add metadata_filter to openapi spec
Browse files Browse the repository at this point in the history
Signed-off-by: Diwank Singh Tomer <diwank.singh@gmail.com>
  • Loading branch information
creatorrr committed Nov 9, 2024
1 parent 46a1b3c commit 99349b1
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 33 deletions.
4 changes: 2 additions & 2 deletions agents-api/agents_api/autogen/Docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from typing import Annotated, Any, Literal
from uuid import UUID

from pydantic import AwareDatetime, BaseModel, ConfigDict, Field, StrictBool
from pydantic import AwareDatetime, BaseModel, ConfigDict, Field


class BaseDocSearchRequest(BaseModel):
Expand All @@ -18,7 +18,7 @@ class BaseDocSearchRequest(BaseModel):
"""
The language to be used for text-only search. Support for other languages coming soon.
"""
metadata_filter: dict[str, float | str | StrictBool | None] = {}
metadata_filter: dict[str, Any] = {}
mmr_strength: Annotated[float, Field(ge=0.0, lt=1.0)] = 0
"""
MMR Strength (mmr_strength = 1 - mmr_lambda)
Expand Down
1 change: 1 addition & 0 deletions agents-api/agents_api/autogen/Tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
BaseModel,
ConfigDict,
Field,
RootModel,
StrictBool,
)

Expand Down
22 changes: 16 additions & 6 deletions agents-api/agents_api/dependencies/query_filter.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import Any, Callable
from typing import Annotated, Any, Callable

from fastapi import Request
from fastapi import Query, Request
from pydantic import BaseModel, ConfigDict


def convert_value(value: str) -> Any:
Expand All @@ -15,9 +16,13 @@ def convert_value(value: str) -> Any:
return value


class MetadataFilter(BaseModel):
model_config = ConfigDict(extra="allow")


def create_filter_extractor(
prefix: str = "filter",
) -> Callable[[Request], dict[str, Any]]:
prefix: str = "metadata_filter",
) -> Callable[[Request], MetadataFilter]:
"""
Creates a dependency function to extract filter parameters with a given prefix.
Expand All @@ -31,7 +36,12 @@ def create_filter_extractor(
# Add a dot to the prefix to allow for nested filters
prefix += "."

def extract_filters(request: Request) -> dict[str, Any]:
def extract_filters(
request: Request,
metadata_filter: Annotated[
MetadataFilter, Query(default_factory=MetadataFilter)
],
) -> MetadataFilter:
"""
Extracts query parameters that start with the specified prefix and returns them as a dictionary.
Expand All @@ -49,6 +59,6 @@ def extract_filters(request: Request) -> dict[str, Any]:
filter_key = key[len(prefix) :]
filters[filter_key] = convert_value(value)

return filters
return MetadataFilter(**filters)

return extract_filters
29 changes: 20 additions & 9 deletions agents-api/agents_api/routers/docs/list_docs.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,33 @@
from typing import Annotated, Literal
import json
from typing import Annotated, Literal, Optional
from uuid import UUID

from fastapi import Depends
from pydantic import BaseModel, BeforeValidator, ConfigDict
from fastapi import Depends, Query

from ...autogen.openapi_model import Doc, ListResponse
from ...dependencies.developer_id import get_developer_id
from ...dependencies.query_filter import create_filter_extractor
from ...dependencies.query_filter import create_filter_extractor, MetadataFilter
from ...models.docs.list_docs import list_docs as list_docs_query
from .router import router


class ListDocsQuery(BaseModel):
limit: int = 100
offset: int = 0
sort_by: Literal["created_at", "updated_at"] = "created_at"
direction: Literal["asc", "desc"] = "desc"
metadata_filter: MetadataFilter = None


@router.get("/users/{user_id}/docs", tags=["docs"])
async def list_user_docs(
x_developer_id: Annotated[UUID, Depends(get_developer_id)],
user_id: UUID,
metadata_filter: Annotated[
dict, Depends(create_filter_extractor("metadata_filter"))
MetadataFilter,
Depends(create_filter_extractor("metadata_filter")),
],
user_id: UUID,
limit: int = 100,
offset: int = 0,
sort_by: Literal["created_at", "updated_at"] = "created_at",
Expand All @@ -30,7 +41,7 @@ async def list_user_docs(
offset=offset,
sort_by=sort_by,
direction=direction,
metadata_filter=metadata_filter or {},
metadata_filter=metadata_filter.model_dump(mode="json"),
)

return ListResponse[Doc](items=docs)
Expand All @@ -39,10 +50,10 @@ async def list_user_docs(
@router.get("/agents/{agent_id}/docs", tags=["docs"])
async def list_agent_docs(
x_developer_id: Annotated[UUID, Depends(get_developer_id)],
agent_id: UUID,
metadata_filter: Annotated[
dict, Depends(create_filter_extractor("metadata_filter"))
MetadataFilter, Depends(create_filter_extractor("metadata_filter"))
],
agent_id: UUID,
limit: int = 100,
offset: int = 0,
sort_by: Literal["created_at", "updated_at"] = "created_at",
Expand All @@ -56,7 +67,7 @@ async def list_agent_docs(
offset=offset,
sort_by=sort_by,
direction=direction,
metadata_filter=metadata_filter or {},
metadata_filter=metadata_filter.model_dump(mode="json"),
)

return ListResponse[Doc](items=docs)
4 changes: 2 additions & 2 deletions integrations-service/integrations/autogen/Docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from typing import Annotated, Any, Literal
from uuid import UUID

from pydantic import AwareDatetime, BaseModel, ConfigDict, Field, StrictBool
from pydantic import AwareDatetime, BaseModel, ConfigDict, Field


class BaseDocSearchRequest(BaseModel):
Expand All @@ -18,7 +18,7 @@ class BaseDocSearchRequest(BaseModel):
"""
The language to be used for text-only search. Support for other languages coming soon.
"""
metadata_filter: dict[str, float | str | StrictBool | None] = {}
metadata_filter: dict[str, Any] = {}
mmr_strength: Annotated[float, Field(ge=0.0, lt=1.0)] = 0
"""
MMR Strength (mmr_strength = 1 - mmr_lambda)
Expand Down
1 change: 1 addition & 0 deletions integrations-service/integrations/autogen/Tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
BaseModel,
ConfigDict,
Field,
RootModel,
StrictBool,
)

Expand Down
4 changes: 2 additions & 2 deletions typespec/common/types.tsp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Common;
//

alias Metadata = Record<unknown>;
alias MetadataFilter = Record<concreteType>;
alias MetadataFilter = Record<unknown>;

model ResourceCreatedResponse {
@doc("ID of created resource")
Expand Down Expand Up @@ -52,7 +52,7 @@ model PaginationOptions {
@query direction: sortDirection = "asc",

/** Object to filter results by metadata */
@query metadata_filter: MetadataFilter,
@query metadata_filter: Record<unknown> = #{},
}

@events
Expand Down
15 changes: 3 additions & 12 deletions typespec/tsp-output/@typespec/openapi3/openapi-1.0.0.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1340,12 +1340,8 @@ components:
description: Object to filter results by metadata
schema:
type: object
additionalProperties:
anyOf:
- type: number
- type: string
- type: boolean
nullable: true
additionalProperties: {}
default: {}
explode: false
Common.PaginationOptions.offset:
name: offset
Expand Down Expand Up @@ -2451,12 +2447,7 @@ components:
default: en-US
metadata_filter:
type: object
additionalProperties:
anyOf:
- type: number
- type: string
- type: boolean
nullable: true
additionalProperties: {}
default: {}
mmr_strength:
type: number
Expand Down

0 comments on commit 99349b1

Please sign in to comment.