From e97b6fb093af41fe2e78dab433594a77220d9eed Mon Sep 17 00:00:00 2001 From: Diwank Tomer Date: Wed, 14 Aug 2024 20:56:49 -0400 Subject: [PATCH] fix(agents-api): Minor fixes Signed-off-by: Diwank Tomer --- agents-api/agents_api/activities/__init__.py | 10 +--- agents-api/agents_api/clients/temporal.py | 46 ------------------- .../agents_api/routers/docs/create_doc.py | 46 ++++++++++++++++++- agents-api/poetry.lock | 14 +++--- agents-api/tests/fixtures.py | 12 +++++ agents-api/tests/test_activities.py | 24 +++++----- agents-api/tests/test_docs_routes.py | 8 ++-- sdks/python/poetry.lock | 6 +-- 8 files changed, 84 insertions(+), 82 deletions(-) diff --git a/agents-api/agents_api/activities/__init__.py b/agents-api/agents_api/activities/__init__.py index 49722a7d5..c641ab7b9 100644 --- a/agents-api/agents_api/activities/__init__.py +++ b/agents-api/agents_api/activities/__init__.py @@ -1,13 +1,5 @@ """ -The `activities` module within the agents-api package is designed to facilitate various activities related to agent interactions. This includes handling memory management, generating insights from dialogues, summarizing relationships, and more. Each file within the module offers specific functionality: - -- `co_density.py`: Conducts cognitive density analysis to generate concise, entity-dense summaries. -- `dialog_insights.py`: Extracts insights from dialogues, identifying details that participants might find interesting. -- `mem_mgmt.py`: Manages memory by updating and incorporating new personality information from dialogues. -- `mem_rating.py`: Rates memories based on their poignancy and importance. -- `relationship_summary.py`: Summarizes the relationship between individuals based on provided statements. -- `salient_questions.py`: Identifies salient questions from a set of statements. -- `summarization.py`: Summarizes dialogues and updates memory based on the conversation context. +The `activities` module within the agents-api package is designed to facilitate various activities related to agent interactions. This includes handling memory management, generating insights from dialogues, summarizing relationships, and more. This module plays a crucial role in enhancing the capabilities of agents by providing them with the tools to understand and process information more effectively. """ diff --git a/agents-api/agents_api/clients/temporal.py b/agents-api/agents_api/clients/temporal.py index 72a5056c8..29ceedded 100644 --- a/agents-api/agents_api/clients/temporal.py +++ b/agents-api/agents_api/clients/temporal.py @@ -34,52 +34,6 @@ async def get_client( ) -async def run_summarization_task( - session_id: UUID, job_id: UUID, client: Client | None = None -): - client = client or (await get_client()) - - await client.execute_workflow( - "SummarizationWorkflow", - args=[str(session_id)], - task_queue="memory-task-queue", - id=str(job_id), - ) - - -async def run_embed_docs_task( - doc_id: UUID, - title: str, - content: list[str], - job_id: UUID, - client: Client | None = None, -): - client = client or (await get_client()) - - await client.execute_workflow( - "EmbedDocsWorkflow", - args=[str(doc_id), title, content], - task_queue="memory-task-queue", - id=str(job_id), - ) - - -async def run_truncation_task( - token_count_threshold: int, - session_id: UUID, - job_id: UUID, - client: Client | None = None, -): - client = client or (await get_client()) - - await client.execute_workflow( - "TruncationWorkflow", - args=[str(session_id), token_count_threshold], - task_queue="memory-task-queue", - id=str(job_id), - ) - - async def run_task_execution_workflow( execution_input: ExecutionInput, job_id: UUID, diff --git a/agents-api/agents_api/routers/docs/create_doc.py b/agents-api/agents_api/routers/docs/create_doc.py index 0b43fe8eb..645f82964 100644 --- a/agents-api/agents_api/routers/docs/create_doc.py +++ b/agents-api/agents_api/routers/docs/create_doc.py @@ -1,15 +1,35 @@ from typing import Annotated +from uuid import UUID, uuid4 from fastapi import Depends from pydantic import UUID4 from starlette.status import HTTP_201_CREATED +from temporalio.client import Client as TemporalClient from ...autogen.openapi_model import CreateDocRequest, ResourceCreatedResponse +from ...clients import temporal from ...dependencies.developer_id import get_developer_id from ...models.docs.create_doc import create_doc as create_doc_query from .router import router +async def run_embed_docs_task( + doc_id: UUID, + title: str, + content: list[str], + job_id: UUID, + client: TemporalClient | None = None, +): + client = client or (await temporal.get_client()) + + await client.execute_workflow( + "EmbedDocsWorkflow", + args=[str(doc_id), title, content], + task_queue="memory-task-queue", + id=str(job_id), + ) + + @router.post("/users/{user_id}/docs", status_code=HTTP_201_CREATED, tags=["docs"]) async def create_user_doc( user_id: UUID4, @@ -23,7 +43,18 @@ async def create_user_doc( data=data, ) - return ResourceCreatedResponse(id=doc.id, created_at=doc.created_at, jobs=[]) + embed_job_id = uuid4() + + await run_embed_docs_task( + doc_id=doc.id, + title=doc.title, + content=doc.content, + job_id=embed_job_id, + ) + + return ResourceCreatedResponse( + id=doc.id, created_at=doc.created_at, jobs=[embed_job_id] + ) @router.post("/agents/{agent_id}/docs", status_code=HTTP_201_CREATED, tags=["docs"]) @@ -39,4 +70,15 @@ async def create_agent_doc( data=data, ) - return ResourceCreatedResponse(id=doc.id, created_at=doc.created_at, jobs=[]) + embed_job_id = uuid4() + + await run_embed_docs_task( + doc_id=doc.id, + title=doc.title, + content=doc.content, + job_id=embed_job_id, + ) + + return ResourceCreatedResponse( + id=doc.id, created_at=doc.created_at, jobs=[embed_job_id] + ) diff --git a/agents-api/poetry.lock b/agents-api/poetry.lock index 6a29c9f4a..e4edb74b7 100644 --- a/agents-api/poetry.lock +++ b/agents-api/poetry.lock @@ -2103,18 +2103,18 @@ files = [ [[package]] name = "langchain" -version = "0.2.13" +version = "0.2.14" description = "Building applications with LLMs through composability" optional = false python-versions = "<4.0,>=3.8.1" files = [ - {file = "langchain-0.2.13-py3-none-any.whl", hash = "sha256:80f21e48cdada424dd2af9bbf42234fe095744cf181b31eeb63d1da7479e2783"}, - {file = "langchain-0.2.13.tar.gz", hash = "sha256:947e96ac3153a46aa6a0d8207e5f8b6794084c397f60a01bbf4bba78e6838fee"}, + {file = "langchain-0.2.14-py3-none-any.whl", hash = "sha256:eed76194ee7d9c081037a3df7868d4de90e0410b51fc1ca933a8379e464bf40c"}, + {file = "langchain-0.2.14.tar.gz", hash = "sha256:dc2aa5a58882054fb5d043c39ab8332ebd055f88f17839da68e1c7fd0a4fefe2"}, ] [package.dependencies] aiohttp = ">=3.8.3,<4.0.0" -langchain-core = ">=0.2.30,<0.3.0" +langchain-core = ">=0.2.32,<0.3.0" langchain-text-splitters = ">=0.2.0,<0.3.0" langsmith = ">=0.1.17,<0.2.0" numpy = {version = ">=1,<2", markers = "python_version < \"3.12\""} @@ -2149,13 +2149,13 @@ tenacity = ">=8.1.0,<8.4.0 || >8.4.0,<9.0.0" [[package]] name = "langchain-core" -version = "0.2.31" +version = "0.2.32" description = "Building applications with LLMs through composability" optional = false python-versions = "<4.0,>=3.8.1" files = [ - {file = "langchain_core-0.2.31-py3-none-any.whl", hash = "sha256:b4daf5ddc23c0c3d8c5fd1a6c118f95fb5d0f96067b43f2c5935e1cd572e4374"}, - {file = "langchain_core-0.2.31.tar.gz", hash = "sha256:afb2089d4c10842d2477dc5cfa9ae9feb415c1421c6ef9aa608fea879ee41769"}, + {file = "langchain_core-0.2.32-py3-none-any.whl", hash = "sha256:1f5584cf0034909e35ea17010a847d4079417e0ddcb5a9eb3fbb2bd55f3268c0"}, + {file = "langchain_core-0.2.32.tar.gz", hash = "sha256:d82cdc350bbbe74261330d87056b7d9f1fb567828e9e03f708d23a48b941819e"}, ] [package.dependencies] diff --git a/agents-api/tests/fixtures.py b/agents-api/tests/fixtures.py index 4fb53ffd7..437c95083 100644 --- a/agents-api/tests/fixtures.py +++ b/agents-api/tests/fixtures.py @@ -74,6 +74,18 @@ async def temporal_worker(wf_env=workflow_environment): await c +@fixture(scope="test") +def patch_temporal_get_client( + wf_env=workflow_environment, + temporal_worker=temporal_worker, +): + mock_client = wf_env.client + + with patch("agents_api.clients.temporal.get_client") as get_client: + get_client.return_value = mock_client + yield get_client + + @fixture(scope="global") def test_developer_id(cozo_client=cozo_client): developer_id = uuid4() diff --git a/agents-api/tests/test_activities.py b/agents-api/tests/test_activities.py index 29adb8110..ef9d4151b 100644 --- a/agents-api/tests/test_activities.py +++ b/agents-api/tests/test_activities.py @@ -16,7 +16,18 @@ # from agents_api.common.protocol.entries import Entry -@test("activity: embed_docs") +@test("activity: check that workflow environment and worker are started correctly") +async def _( + workflow_environment=workflow_environment, + worker=temporal_worker, +): + async with workflow_environment as wf_env: + assert wf_env is not None + assert worker is not None + assert worker.is_running + + +@test("activity: call direct embed_docs") async def _( cozo_client=cozo_client, developer_id=test_developer_id, @@ -41,17 +52,6 @@ async def _( embed.assert_called_once() -@test("activity: check that workflow environment and worker are started correctly") -async def _( - workflow_environment=workflow_environment, - worker=temporal_worker, -): - async with workflow_environment as wf_env: - assert wf_env is not None - assert worker is not None - assert worker.is_running - - # @test("get extra entries, do not strip system message") # def _(): # session_ids = [uuid.uuid4()] * 3 diff --git a/agents-api/tests/test_docs_routes.py b/agents-api/tests/test_docs_routes.py index 05f095f49..67e38a50e 100644 --- a/agents-api/tests/test_docs_routes.py +++ b/agents-api/tests/test_docs_routes.py @@ -25,6 +25,9 @@ def _(make_request=make_request, user=test_user): assert response.status_code == 201 + result = response.json() + assert len(result["jobs"]) > 0 + @test("route: create agent doc") def _(make_request=make_request, agent=test_agent): @@ -41,9 +44,8 @@ def _(make_request=make_request, agent=test_agent): assert response.status_code == 201 - # FIXME: Should create a job to process the document - # result = response.json() - # assert len(result["jobs"]) > 0 + result = response.json() + assert len(result["jobs"]) > 0 @test("route: delete doc") diff --git a/sdks/python/poetry.lock b/sdks/python/poetry.lock index fe5a1519d..bfef1a91f 100644 --- a/sdks/python/poetry.lock +++ b/sdks/python/poetry.lock @@ -848,13 +848,13 @@ test = ["flufl.flake8", "importlib-resources (>=1.3)", "jaraco.test (>=5.4)", "p [[package]] name = "importlib-resources" -version = "6.4.1" +version = "6.4.2" description = "Read resources from Python packages" optional = false python-versions = ">=3.8" files = [ - {file = "importlib_resources-6.4.1-py3-none-any.whl", hash = "sha256:8fbee7ba7376ca7c47ce8d31b96b93d8787349845f01ebdbee5ef90409035234"}, - {file = "importlib_resources-6.4.1.tar.gz", hash = "sha256:5ede8acf5d752abda46fb6922a4a6ab782b6d904dfd362bf2d8b857eee1759d9"}, + {file = "importlib_resources-6.4.2-py3-none-any.whl", hash = "sha256:8bba8c54a8a3afaa1419910845fa26ebd706dc716dd208d9b158b4b6966f5c5c"}, + {file = "importlib_resources-6.4.2.tar.gz", hash = "sha256:6cbfbefc449cc6e2095dd184691b7a12a04f40bc75dd4c55d31c34f174cdf57a"}, ] [package.dependencies]