Skip to content

Commit

Permalink
feat: Move to async cozo client
Browse files Browse the repository at this point in the history
  • Loading branch information
whiterabbit1983 committed Nov 7, 2024
1 parent 2d2f624 commit f81c1f9
Show file tree
Hide file tree
Showing 98 changed files with 260 additions and 214 deletions.
2 changes: 1 addition & 1 deletion agents-api/agents_api/activities/embed_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ async def embed_batch(snippets):
await asyncio.gather(*[embed_batch(snippets) for snippets in batched_snippets]),
)

embed_snippets_query(
await embed_snippets_query(
developer_id=payload.developer_id,
doc_id=payload.doc_id,
snippet_indices=indices,
Expand Down
2 changes: 1 addition & 1 deletion agents-api/agents_api/activities/execute_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ async def execute_system(

# Handle chat operations
if system.operation == "chat" and system.resource == "session":
developer = get_developer(developer_id=arguments.pop("developer_id"))
developer = await get_developer(developer_id=arguments.pop("developer_id"))
session_id = arguments.pop("session_id")
x_custom_api_key = arguments.pop("x_custom_api_key", None)
chat_input = ChatInput(**arguments)
Expand Down
6 changes: 4 additions & 2 deletions agents-api/agents_api/activities/task_steps/prompt_step.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from datetime import datetime
from typing import Callable
from typing import Awaitable, Callable

from anthropic import AsyncAnthropic # Import AsyncAnthropic client
from anthropic.types.beta.beta_message import BetaMessage
Expand Down Expand Up @@ -58,7 +58,9 @@ def format_tool(tool: Tool) -> dict:
}

if tool.type == "system":
handler: Callable = get_handler_with_filtered_params(tool.system)
handler: Callable[..., Awaitable] = get_handler_with_filtered_params(
tool.system
)

lc_tool: BaseTool = tool_decorator(handler)

Expand Down
4 changes: 2 additions & 2 deletions agents-api/agents_api/activities/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import string
import time
import urllib.parse
from typing import Any, Callable, ParamSpec, TypeVar
from typing import Any, Callable, ParamSpec, TypeVar, Awaitable

import re2
import zoneinfo
Expand Down Expand Up @@ -270,7 +270,7 @@ def filtered_handler(*args, **kwargs):
return filtered_handler


def get_handler(system: SystemDef) -> Callable:
def get_handler(system: SystemDef) -> Callable[..., Awaitable]:
"""
Internal function to get the base handler without parameter filtering.
Expand Down
2 changes: 1 addition & 1 deletion agents-api/agents_api/clients/cozo.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Dict

from pycozo.client import Client
from pycozo_async.client import Client

from ..env import cozo_auth, cozo_host
from ..web import app
Expand Down
2 changes: 1 addition & 1 deletion agents-api/agents_api/common/utils/cozo.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from uuid import UUID

from beartype import beartype
from pycozo import Client
from pycozo_async import Client

# Define a mock client for testing purposes, simulating Cozo API client behavior.
_fake_client: SimpleNamespace = SimpleNamespace()
Expand Down
6 changes: 3 additions & 3 deletions agents-api/agents_api/dependencies/developer_id.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ async def get_developer_id(
except ValueError as e:
raise InvalidHeaderFormat("X-Developer-Id must be a valid UUID") from e

verify_developer(developer_id=x_developer_id)
await verify_developer(developer_id=x_developer_id)

return x_developer_id

Expand All @@ -36,7 +36,7 @@ async def get_developer_data(
assert (
not x_developer_id
), "X-Developer-Id header not allowed in multi-tenant mode"
return get_developer(developer_id=UUID("00000000-0000-0000-0000-000000000000"))
return await get_developer(developer_id=UUID("00000000-0000-0000-0000-000000000000"))

if not x_developer_id:
raise InvalidHeaderFormat("X-Developer-Id header required")
Expand All @@ -47,6 +47,6 @@ async def get_developer_data(
except ValueError as e:
raise InvalidHeaderFormat("X-Developer-Id must be a valid UUID") from e

developer = get_developer(developer_id=x_developer_id)
developer = await get_developer(developer_id=x_developer_id)

return developer
13 changes: 9 additions & 4 deletions agents-api/agents_api/metrics/counters.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import inspect
from functools import wraps
from typing import Callable, ParamSpec, TypeVar
from typing import Awaitable, Callable, ParamSpec, TypeVar

from prometheus_client import Counter

Expand All @@ -8,17 +9,21 @@


def increase_counter(metric_label: str, id_field_name: str = "developer_id"):
def decor(func: Callable[P, T]):
def decor(func: Callable[P, T] | Callable[P, Awaitable[T]]):
metric = Counter(
metric_label,
f"Number of {metric_label} calls",
labelnames=(id_field_name,),
)

@wraps(func)
def wrapper(*args: P.args, **kwargs: P.kwargs) -> T:
async def wrapper(*args: P.args, **kwargs: P.kwargs) -> T:
metric.labels(kwargs.get(id_field_name, "not_set")).inc()
return func(*args, **kwargs)
return (
await func(*args, **kwargs)
if inspect.iscoroutinefunction(func)
else func(*args, **kwargs)
)

return wrapper

Expand Down
2 changes: 1 addition & 1 deletion agents-api/agents_api/models/agent/create_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from beartype import beartype
from fastapi import HTTPException
from pycozo.client import QueryException
from pycozo_async.client import QueryException
from pydantic import ValidationError

from ...autogen.openapi_model import Agent, CreateAgentRequest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from beartype import beartype
from fastapi import HTTPException
from pycozo.client import QueryException
from pycozo_async.client import QueryException
from pydantic import ValidationError

from ...autogen.openapi_model import Agent, CreateOrUpdateAgentRequest
Expand Down
2 changes: 1 addition & 1 deletion agents-api/agents_api/models/agent/delete_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from beartype import beartype
from fastapi import HTTPException
from pycozo.client import QueryException
from pycozo_async.client import QueryException
from pydantic import ValidationError

from ...autogen.openapi_model import ResourceDeletedResponse
Expand Down
2 changes: 1 addition & 1 deletion agents-api/agents_api/models/agent/get_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from beartype import beartype
from fastapi import HTTPException
from pycozo.client import QueryException
from pycozo_async.client import QueryException
from pydantic import ValidationError

from ...autogen.openapi_model import Agent
Expand Down
2 changes: 1 addition & 1 deletion agents-api/agents_api/models/agent/list_agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from beartype import beartype
from fastapi import HTTPException
from pycozo.client import QueryException
from pycozo_async.client import QueryException
from pydantic import ValidationError

from ...autogen.openapi_model import Agent
Expand Down
2 changes: 1 addition & 1 deletion agents-api/agents_api/models/agent/patch_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from beartype import beartype
from fastapi import HTTPException
from pycozo.client import QueryException
from pycozo_async.client import QueryException
from pydantic import ValidationError

from ...autogen.openapi_model import PatchAgentRequest, ResourceUpdatedResponse
Expand Down
2 changes: 1 addition & 1 deletion agents-api/agents_api/models/agent/update_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from beartype import beartype
from fastapi import HTTPException
from pycozo.client import QueryException
from pycozo_async.client import QueryException
from pydantic import ValidationError

from ...autogen.openapi_model import ResourceUpdatedResponse, UpdateAgentRequest
Expand Down
6 changes: 3 additions & 3 deletions agents-api/agents_api/models/chat/gather_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from beartype import beartype
from fastapi import HTTPException
from pycozo.client import QueryException
from pycozo_async.client import QueryException
from pydantic import ValidationError

from ...autogen.Chat import ChatInput
Expand Down Expand Up @@ -42,7 +42,7 @@ async def gather_messages(
assert len(new_raw_messages) > 0

# Get the session history
history: History = get_history(
history: History = await get_history(
developer_id=developer.id,
session_id=session_id,
allowed_sources=["api_request", "api_response", "tool_response", "summarizer"],
Expand Down Expand Up @@ -75,7 +75,7 @@ async def gather_messages(
user_ids = [user.id for user in chat_context.users]
owners = [("user", user_id) for user_id in user_ids] + [("agent", active_agent_id)]

doc_references: list[DocReference] = search_docs_hybrid(
doc_references: list[DocReference] = await search_docs_hybrid(
developer_id=developer.id,
owners=owners,
query=query_text,
Expand Down
2 changes: 1 addition & 1 deletion agents-api/agents_api/models/chat/prepare_chat_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from beartype import beartype
from fastapi import HTTPException
from pycozo.client import QueryException
from pycozo_async.client import QueryException
from pydantic import ValidationError

from ...common.protocol.sessions import ChatContext, make_session
Expand Down
2 changes: 1 addition & 1 deletion agents-api/agents_api/models/developer/get_developer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from beartype import beartype
from fastapi import HTTPException
from pycozo.client import QueryException
from pycozo_async.client import QueryException
from pydantic import ValidationError

from ...common.protocol.developers import Developer
Expand Down
2 changes: 1 addition & 1 deletion agents-api/agents_api/models/docs/create_doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from beartype import beartype
from fastapi import HTTPException
from pycozo.client import QueryException
from pycozo_async.client import QueryException
from pydantic import ValidationError

from ...autogen.openapi_model import CreateDocRequest, Doc
Expand Down
2 changes: 1 addition & 1 deletion agents-api/agents_api/models/docs/delete_doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from beartype import beartype
from fastapi import HTTPException
from pycozo.client import QueryException
from pycozo_async.client import QueryException
from pydantic import ValidationError

from ...autogen.openapi_model import ResourceDeletedResponse
Expand Down
2 changes: 1 addition & 1 deletion agents-api/agents_api/models/docs/embed_snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from beartype import beartype
from fastapi import HTTPException
from pycozo.client import QueryException
from pycozo_async.client import QueryException
from pydantic import ValidationError

from ...autogen.openapi_model import ResourceUpdatedResponse
Expand Down
2 changes: 1 addition & 1 deletion agents-api/agents_api/models/docs/get_doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from beartype import beartype
from fastapi import HTTPException
from pycozo.client import QueryException
from pycozo_async.client import QueryException
from pydantic import ValidationError

from ...autogen.openapi_model import Doc
Expand Down
2 changes: 1 addition & 1 deletion agents-api/agents_api/models/docs/list_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from beartype import beartype
from fastapi import HTTPException
from pycozo.client import QueryException
from pycozo_async.client import QueryException
from pydantic import ValidationError

from ...autogen.openapi_model import Doc
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from beartype import beartype
from fastapi import HTTPException
from pycozo.client import QueryException
from pycozo_async.client import QueryException
from pydantic import ValidationError

from ...autogen.openapi_model import DocReference
Expand Down
2 changes: 1 addition & 1 deletion agents-api/agents_api/models/docs/search_docs_by_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from beartype import beartype
from fastapi import HTTPException
from pycozo.client import QueryException
from pycozo_async.client import QueryException
from pydantic import ValidationError

from ...autogen.openapi_model import DocReference
Expand Down
6 changes: 3 additions & 3 deletions agents-api/agents_api/models/docs/search_docs_hybrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def dbsf_fuse(


@beartype
def search_docs_hybrid(
async def search_docs_hybrid(
*,
developer_id: UUID,
owners: list[tuple[Literal["user", "agent"], UUID]],
Expand All @@ -103,7 +103,7 @@ def search_docs_hybrid(
metadata_filter: dict[str, Any] = {},
) -> list[DocReference]:
# TODO: We should probably parallelize these queries
text_results = search_docs_by_text(
text_results = await search_docs_by_text(
developer_id=developer_id,
owners=owners,
query=query,
Expand All @@ -112,7 +112,7 @@ def search_docs_hybrid(
**text_search_options,
)

embedding_results = search_docs_by_embedding(
embedding_results = await search_docs_by_embedding(
developer_id=developer_id,
owners=owners,
query_embedding=query_embedding,
Expand Down
2 changes: 1 addition & 1 deletion agents-api/agents_api/models/entry/create_entries.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from beartype import beartype
from fastapi import HTTPException
from pycozo.client import QueryException
from pycozo_async.client import QueryException
from pydantic import ValidationError

from ...autogen.openapi_model import CreateEntryRequest, Entry, Relation
Expand Down
2 changes: 1 addition & 1 deletion agents-api/agents_api/models/entry/delete_entries.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from beartype import beartype
from fastapi import HTTPException
from pycozo.client import QueryException
from pycozo_async.client import QueryException
from pydantic import ValidationError

from ...autogen.openapi_model import ResourceDeletedResponse
Expand Down
2 changes: 1 addition & 1 deletion agents-api/agents_api/models/entry/get_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from beartype import beartype
from fastapi import HTTPException
from pycozo.client import QueryException
from pycozo_async.client import QueryException
from pydantic import ValidationError

from ...autogen.openapi_model import History
Expand Down
2 changes: 1 addition & 1 deletion agents-api/agents_api/models/entry/list_entries.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from beartype import beartype
from fastapi import HTTPException
from pycozo.client import QueryException
from pycozo_async.client import QueryException
from pydantic import ValidationError

from ...autogen.openapi_model import Entry
Expand Down
2 changes: 1 addition & 1 deletion agents-api/agents_api/models/execution/create_execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from beartype import beartype
from fastapi import HTTPException
from pycozo.client import QueryException
from pycozo_async.client import QueryException
from pydantic import ValidationError

from ...autogen.openapi_model import CreateExecutionRequest, Execution
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from beartype import beartype
from fastapi import HTTPException
from pycozo.client import QueryException
from pycozo_async.client import QueryException
from pydantic import ValidationError

from ...autogen.openapi_model import (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from beartype import beartype
from fastapi import HTTPException
from pycozo.client import QueryException
from pycozo_async.client import QueryException
from pydantic import ValidationError
from temporalio.client import WorkflowHandle

Expand Down
2 changes: 1 addition & 1 deletion agents-api/agents_api/models/execution/get_execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from beartype import beartype
from fastapi import HTTPException
from pycozo.client import QueryException
from pycozo_async.client import QueryException
from pydantic import ValidationError

from ...autogen.openapi_model import Execution
Expand Down
Loading

0 comments on commit f81c1f9

Please sign in to comment.