From 34fc2e5df07cb48fece3d606b24255078329039d Mon Sep 17 00:00:00 2001 From: HamadaSalhab Date: Sat, 5 Oct 2024 21:17:24 +0300 Subject: [PATCH 1/6] Bug fixes in base_evaluate --- .../activities/task_steps/base_evaluate.py | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/agents-api/agents_api/activities/task_steps/base_evaluate.py b/agents-api/agents_api/activities/task_steps/base_evaluate.py index 3fcbf2f73..c6b83ba89 100644 --- a/agents-api/agents_api/activities/task_steps/base_evaluate.py +++ b/agents-api/agents_api/activities/task_steps/base_evaluate.py @@ -46,29 +46,29 @@ async def base_evaluate( evaluator = get_evaluator(names=values, extra_functions=extra_lambdas) try: + result = None match exprs: case str(): - return evaluator.eval(exprs) - + result = evaluator.eval(exprs) case list(): - return [evaluator.eval(expr) for expr in exprs] - - case dict() as d if all(isinstance(v, dict) for v in d.values()): - return { + result = [evaluator.eval(expr) for expr in exprs] + case dict() as d if all( + isinstance(v, dict) or isinstance(v, str) for v in d.values() + ): + result = { k: {ik: evaluator.eval(iv) for ik, iv in v.items()} + if isinstance(v, dict) + else evaluator.eval(v) for k, v in d.items() } - - case dict(): - return {k: evaluator.eval(v) for k, v in exprs.items()} - case _: raise ValueError(f"Invalid expression: {exprs}") + return result + except BaseException as e: if activity.in_activity(): activity.logger.error(f"Error in base_evaluate: {e}") - raise From 9198f361b84f817555e30b3d0cb909836e21186d Mon Sep 17 00:00:00 2001 From: HamadaSalhab Date: Sat, 5 Oct 2024 21:40:42 +0300 Subject: [PATCH 2/6] Fix create user doc system tool --- agents-api/agents_api/activities/execute_system.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/agents-api/agents_api/activities/execute_system.py b/agents-api/agents_api/activities/execute_system.py index 8e4d71274..88ef6fc38 100644 --- a/agents-api/agents_api/activities/execute_system.py +++ b/agents-api/agents_api/activities/execute_system.py @@ -4,6 +4,8 @@ from beartype import beartype from temporalio import activity +from agents_api.autogen.Docs import CreateDocRequest + from ..autogen.Tools import SystemDef from ..common.protocol.tasks import StepContext from ..env import testing @@ -102,7 +104,11 @@ async def execute_system( if system.operation == "list": return list_docs_query(**user_doc_args) elif system.operation == "create": - return create_doc_query(**user_doc_args) + # Create a CreateDocRequest object from the "data" field in the arguments + if "data" in user_doc_args: + doc_data = user_doc_args.pop("data") + create_doc_request = CreateDocRequest(**doc_data) + return create_doc_query(**user_doc_args, data=create_doc_request) elif system.operation == "delete": return delete_doc_query(**user_doc_args) From 96cb3e365dcc14a86c1c3c13fd47b39d233d1c22 Mon Sep 17 00:00:00 2001 From: Diwank Singh Tomer Date: Sat, 5 Oct 2024 15:15:31 -0400 Subject: [PATCH 3/6] fix(agents-api): Do not include docs without embeddings Signed-off-by: Diwank Singh Tomer --- agents-api/agents_api/models/docs/list_docs.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/agents-api/agents_api/models/docs/list_docs.py b/agents-api/agents_api/models/docs/list_docs.py index 4dad7ec06..8f8d8c7a0 100644 --- a/agents-api/agents_api/models/docs/list_docs.py +++ b/agents-api/agents_api/models/docs/list_docs.py @@ -50,6 +50,7 @@ def list_docs( sort_by: Literal["created_at"] = "created_at", direction: Literal["asc", "desc"] = "desc", metadata_filter: dict[str, Any] = {}, + include_without_embeddings: bool = False, ) -> tuple[list[str], dict]: # Transforms the metadata_filter dictionary into a string representation for the datalog query. metadata_filter_str = ", ".join( @@ -70,6 +71,7 @@ def list_docs( content, embedding, }}, + {"" if include_without_embeddings else "not is_null(embedding),"} snippet_data = [index, content, embedding] ?[ From 38f8a1d7e93859196f1c425c659ed9075de6042d Mon Sep 17 00:00:00 2001 From: HamadaSalhab Date: Mon, 7 Oct 2024 14:18:40 +0300 Subject: [PATCH 4/6] Fix(agents-api): Fix docs tools --- .../agents_api/activities/execute_system.py | 48 +++++++++++++++---- 1 file changed, 38 insertions(+), 10 deletions(-) diff --git a/agents-api/agents_api/activities/execute_system.py b/agents-api/agents_api/activities/execute_system.py index 88ef6fc38..4549d5c9a 100644 --- a/agents-api/agents_api/activities/execute_system.py +++ b/agents-api/agents_api/activities/execute_system.py @@ -2,10 +2,10 @@ from uuid import UUID from beartype import beartype +from fastapi.background import BackgroundTasks from temporalio import activity -from agents_api.autogen.Docs import CreateDocRequest - +from ..autogen.Docs import CreateDocRequest from ..autogen.Tools import SystemDef from ..common.protocol.tasks import StepContext from ..env import testing @@ -33,6 +33,8 @@ from ..models.user.get_user import get_user as get_user_query from ..models.user.list_users import list_users as list_users_query from ..models.user.update_user import update_user as update_user_query +from ..routers.docs.create_doc import create_agent_doc, create_user_doc +from ..routers.docs.search_docs import search_agent_docs, search_user_docs @beartype @@ -65,17 +67,32 @@ async def execute_system( agent_doc_args = { **{ "owner_type": "agent", - "owner_id": arguments.pop("agent_id"), + "owner_id": arguments["agent_id"], }, **arguments, } + agent_doc_args.pop("agent_id") + if system.operation == "list": return list_docs_query(**agent_doc_args) + elif system.operation == "create": - return create_doc_query(**agent_doc_args) + # The `create_agent_doc` function requires `x_developer_id` instead of `developer_id`. + arguments["x_developer_id"] = arguments.pop("developer_id") + return await create_agent_doc( + data=CreateDocRequest(**arguments.pop("data")), + background_tasks=BackgroundTasks(), + **arguments, + ) + elif system.operation == "delete": return delete_doc_query(**agent_doc_args) + elif system.operation == "search": + # The `search_agent_docs` function requires `x_developer_id` instead of `developer_id`. + arguments["x_developer_id"] = arguments.pop("developer_id") + return await search_agent_docs(**arguments) + # NO SUBRESOURCE elif system.subresource == None: if system.operation == "list": @@ -97,21 +114,32 @@ async def execute_system( user_doc_args = { **{ "owner_type": "user", - "owner_id": arguments.pop("user_id"), + "owner_id": arguments["user_id"], }, **arguments, } + user_doc_args.pop("user_id") + if system.operation == "list": return list_docs_query(**user_doc_args) + elif system.operation == "create": - # Create a CreateDocRequest object from the "data" field in the arguments - if "data" in user_doc_args: - doc_data = user_doc_args.pop("data") - create_doc_request = CreateDocRequest(**doc_data) - return create_doc_query(**user_doc_args, data=create_doc_request) + # The `create_user_doc` function requires `x_developer_id` instead of `developer_id`. + arguments["x_developer_id"] = arguments.pop("developer_id") + return await create_user_doc( + data=CreateDocRequest(**arguments.pop("data")), + background_tasks=BackgroundTasks(), + **arguments, + ) + elif system.operation == "delete": return delete_doc_query(**user_doc_args) + elif system.operation == "search": + # The `search_user_docs` function requires `x_developer_id` instead of `developer_id`. + arguments["x_developer_id"] = arguments.pop("developer_id") + return await search_user_docs(**arguments) + # NO SUBRESOURCE elif system.subresource == None: if system.operation == "list": From 8432e9584eb68dfd4f474386933271b6e1f601ca Mon Sep 17 00:00:00 2001 From: HamadaSalhab Date: Mon, 7 Oct 2024 14:30:14 +0300 Subject: [PATCH 5/6] fix docs search tool params --- .../agents_api/activities/execute_system.py | 51 +++++++++++++++++-- 1 file changed, 48 insertions(+), 3 deletions(-) diff --git a/agents-api/agents_api/activities/execute_system.py b/agents-api/agents_api/activities/execute_system.py index 4549d5c9a..28e85ac71 100644 --- a/agents-api/agents_api/activities/execute_system.py +++ b/agents-api/agents_api/activities/execute_system.py @@ -5,7 +5,7 @@ from fastapi.background import BackgroundTasks from temporalio import activity -from ..autogen.Docs import CreateDocRequest +from ..autogen.Docs import CreateDocRequest, HybridDocSearchRequest, TextOnlyDocSearchRequest, VectorDocSearchRequest from ..autogen.Tools import SystemDef from ..common.protocol.tasks import StepContext from ..env import testing @@ -91,7 +91,29 @@ async def execute_system( elif system.operation == "search": # The `search_agent_docs` function requires `x_developer_id` instead of `developer_id`. arguments["x_developer_id"] = arguments.pop("developer_id") - return await search_agent_docs(**arguments) + + if "text" in arguments and "vector" in arguments: + search_params = HybridDocSearchRequest( + text=arguments.pop("text"), + vector=arguments.pop("vector"), + limit=arguments.get("limit", 10), + ) + + elif "text" in arguments: + search_params = TextOnlyDocSearchRequest( + text=arguments.pop("text"), + limit=arguments.get("limit", 10), + ) + elif "vector" in arguments: + search_params = VectorDocSearchRequest( + vector=arguments.pop("vector"), + limit=arguments.get("limit", 10), + ) + + return await search_agent_docs( + search_params=search_params, + **arguments, + ) # NO SUBRESOURCE elif system.subresource == None: @@ -138,7 +160,30 @@ async def execute_system( elif system.operation == "search": # The `search_user_docs` function requires `x_developer_id` instead of `developer_id`. arguments["x_developer_id"] = arguments.pop("developer_id") - return await search_user_docs(**arguments) + + + if "text" in arguments and "vector" in arguments: + search_params = HybridDocSearchRequest( + text=arguments.pop("text"), + vector=arguments.pop("vector"), + limit=arguments.get("limit", 10), + ) + + elif "text" in arguments: + search_params = TextOnlyDocSearchRequest( + text=arguments.pop("text"), + limit=arguments.get("limit", 10), + ) + elif "vector" in arguments: + search_params = VectorDocSearchRequest( + vector=arguments.pop("vector"), + limit=arguments.get("limit", 10), + ) + + return await search_user_docs( + search_params=search_params, + **arguments, + ) # NO SUBRESOURCE elif system.subresource == None: From 436d5110e5f0b5927a5e79019293d32e9fc02ffa Mon Sep 17 00:00:00 2001 From: HamadaSalhab Date: Mon, 7 Oct 2024 11:30:52 +0000 Subject: [PATCH 6/6] refactor: Lint agents-api (CI) --- agents-api/agents_api/activities/execute_system.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/agents-api/agents_api/activities/execute_system.py b/agents-api/agents_api/activities/execute_system.py index 28e85ac71..8ffb85814 100644 --- a/agents-api/agents_api/activities/execute_system.py +++ b/agents-api/agents_api/activities/execute_system.py @@ -5,7 +5,12 @@ from fastapi.background import BackgroundTasks from temporalio import activity -from ..autogen.Docs import CreateDocRequest, HybridDocSearchRequest, TextOnlyDocSearchRequest, VectorDocSearchRequest +from ..autogen.Docs import ( + CreateDocRequest, + HybridDocSearchRequest, + TextOnlyDocSearchRequest, + VectorDocSearchRequest, +) from ..autogen.Tools import SystemDef from ..common.protocol.tasks import StepContext from ..env import testing @@ -91,7 +96,7 @@ async def execute_system( elif system.operation == "search": # The `search_agent_docs` function requires `x_developer_id` instead of `developer_id`. arguments["x_developer_id"] = arguments.pop("developer_id") - + if "text" in arguments and "vector" in arguments: search_params = HybridDocSearchRequest( text=arguments.pop("text"), @@ -161,7 +166,6 @@ async def execute_system( # The `search_user_docs` function requires `x_developer_id` instead of `developer_id`. arguments["x_developer_id"] = arguments.pop("developer_id") - if "text" in arguments and "vector" in arguments: search_params = HybridDocSearchRequest( text=arguments.pop("text"),