Skip to content

Commit

Permalink
refactor: Lint agents-api (CI)
Browse files Browse the repository at this point in the history
  • Loading branch information
Vedantsahai18 authored and github-actions[bot] committed Dec 20, 2024
1 parent 6c77490 commit b427e38
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 8 deletions.
1 change: 1 addition & 0 deletions agents-api/agents_api/queries/docs/delete_doc.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Timescale-based deletion of a doc record.
"""

from typing import Literal
from uuid import UUID

Expand Down
3 changes: 2 additions & 1 deletion agents-api/agents_api/queries/docs/get_doc.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Timescale-based retrieval of a single doc record.
"""

from typing import Literal
from uuid import UUID

Expand Down Expand Up @@ -41,7 +42,7 @@ async def get_doc(
developer_id: UUID,
doc_id: UUID,
owner_type: Literal["user", "agent", "org"] | None = None,
owner_id: UUID | None = None
owner_id: UUID | None = None,
) -> tuple[str, list]:
"""
Fetch a single doc, optionally constrained to a given owner.
Expand Down
1 change: 1 addition & 0 deletions agents-api/agents_api/queries/docs/list_docs.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Timescale-based listing of docs with optional owner filter and pagination.
"""

from typing import Literal
from uuid import UUID

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
Timescale-based doc embedding search using the `embedding` column.
"""

import asyncpg
from typing import Literal, List
from typing import List, Literal
from uuid import UUID

import asyncpg
from beartype import beartype
from fastapi import HTTPException
from sqlglot import parse_one
Expand All @@ -32,6 +32,7 @@
LIMIT $2;
""").sql(pretty=True)


@wrap_in_class(
Doc,
one=False,
Expand Down
2 changes: 1 addition & 1 deletion agents-api/agents_api/queries/docs/search_docs_by_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
Timescale-based doc text search using the `search_tsv` column.
"""

import asyncpg
from typing import Literal
from uuid import UUID

import asyncpg
from beartype import beartype
from fastapi import HTTPException
from sqlglot import parse_one
Expand Down
14 changes: 10 additions & 4 deletions agents-api/agents_api/queries/docs/search_docs_hybrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,41 @@
via a simple distribution-based score fusion or direct weighting in Python.
"""

from typing import Literal, List
from typing import List, Literal
from uuid import UUID

from beartype import beartype
from fastapi import HTTPException

from ...autogen.openapi_model import Doc
from ..utils import run_concurrently
from .search_docs_by_text import search_docs_by_text
from .search_docs_by_embedding import search_docs_by_embedding
from .search_docs_by_text import search_docs_by_text


def dbsf_normalize(scores: List[float]) -> List[float]:
"""
Example distribution-based normalization: clamp each score
from (mean - 3*stddev) to (mean + 3*stddev) and scale to 0..1
"""
import statistics

if len(scores) < 2:
return scores
m = statistics.mean(scores)
sd = statistics.pstdev(scores) # population std
if sd == 0:
return scores
upper = m + 3*sd
lower = m - 3*sd
upper = m + 3 * sd
lower = m - 3 * sd

def clamp_scale(v):
c = min(upper, max(lower, v))
return (c - lower) / (upper - lower)

return [clamp_scale(s) for s in scores]


@beartype
def fuse_results(
text_docs: List[Doc], embedding_docs: List[Doc], alpha: float
Expand Down Expand Up @@ -151,6 +156,7 @@ async def search_docs_hybrid(
# text_results, embed_results = await run_concurrently([task1, task2])
# Otherwise just do them in parallel with e.g. asyncio.gather:
from asyncio import gather

text_results, embed_results = await gather(*tasks)

# fuse them
Expand Down

0 comments on commit b427e38

Please sign in to comment.