diff --git a/agents-api/agents_api/activities/excecute_api_call.py b/agents-api/agents_api/activities/excecute_api_call.py new file mode 100644 index 000000000..88fabce89 --- /dev/null +++ b/agents-api/agents_api/activities/excecute_api_call.py @@ -0,0 +1,60 @@ +from typing import Annotated, Any, Optional, TypedDict, Union + +import httpx +from beartype import beartype +from pydantic import Field +from temporalio import activity + +from ..autogen.openapi_model import ApiCallDef + +# from ..clients import integrations +from ..common.protocol.tasks import StepContext +from ..env import testing + +# from ..models.tools import get_tool_args_from_metadata + + +class RequestArgs(TypedDict): + content: Optional[str] + data: Optional[dict[str, Any]] + json_: Optional[dict[str, Any]] + cookies: Optional[dict[str, str]] + params: Optional[Union[str, dict[str, Any]]] + + +@beartype +async def execute_api_call( + api_call: ApiCallDef, + request_args: RequestArgs, +) -> Any: + try: + async with httpx.AsyncClient() as client: + response = await client.request( + method=api_call.method, + url=str(api_call.url), + headers=api_call.headers, + follow_redirects=api_call.follow_redirects, + **request_args, + ) + + response_dict = { + "status_code": response.status_code, + "headers": dict(response.headers), + "content": response.content, + "json": response.json(), + } + + return response_dict + + except BaseException as e: + if activity.in_activity(): + activity.logger.error(f"Error in execute_api_call: {e}") + + raise + + +mock_execute_api_call = execute_api_call + +execute_api_call = activity.defn(name="execute_api_call")( + execute_api_call if not testing else mock_execute_api_call +) 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 0263345ec..3fcbf2f73 100644 --- a/agents-api/agents_api/activities/task_steps/base_evaluate.py +++ b/agents-api/agents_api/activities/task_steps/base_evaluate.py @@ -12,7 +12,7 @@ @beartype async def base_evaluate( - exprs: str | list[str] | dict[str, str], + exprs: str | list[str] | dict[str, str] | dict[str, dict[str, str]], values: dict[str, Any] = {}, extra_lambda_strs: dict[str, str] | None = None, ) -> Any | list[Any] | dict[str, Any]: @@ -53,9 +53,18 @@ async def base_evaluate( 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 { + k: {ik: evaluator.eval(iv) for ik, iv in v.items()} + 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}") + except BaseException as e: if activity.in_activity(): activity.logger.error(f"Error in base_evaluate: {e}") diff --git a/agents-api/agents_api/autogen/Tasks.py b/agents-api/agents_api/autogen/Tasks.py index 9dd531c47..83fde00da 100644 --- a/agents-api/agents_api/autogen/Tasks.py +++ b/agents-api/agents_api/autogen/Tasks.py @@ -944,7 +944,7 @@ class ToolCallStep(BaseModel): """ The tool to run """ - arguments: dict[str, str] | Literal["_"] = "_" + arguments: dict[str, dict[str, str] | str] | Literal["_"] = "_" """ The input parameters for the tool (defaults to last step output) """ diff --git a/agents-api/agents_api/autogen/Tools.py b/agents-api/agents_api/autogen/Tools.py index 8227e5759..07bcbea43 100644 --- a/agents-api/agents_api/autogen/Tools.py +++ b/agents-api/agents_api/autogen/Tools.py @@ -6,7 +6,114 @@ from typing import Annotated, Any, Literal from uuid import UUID -from pydantic import AwareDatetime, BaseModel, ConfigDict, Field +from pydantic import AnyUrl, AwareDatetime, BaseModel, ConfigDict, Field, StrictBool + + +class ApiCallDef(BaseModel): + """ + API call definition + """ + + model_config = ConfigDict( + populate_by_name=True, + ) + method: Literal[ + "GET", "POST", "PUT", "DELETE", "PATCH", "HEAD", "OPTIONS", "CONNECT", "TRACE" + ] + """ + The HTTP method to use + """ + url: AnyUrl + """ + The URL to call + """ + headers: dict[str, str] | None = None + """ + The headers to send with the request + """ + content: str | None = None + """ + The content as base64 to send with the request + """ + data: dict[str, str] | None = None + """ + The data to send as form data + """ + json_: Annotated[dict[str, Any] | None, Field(None, alias="json")] + """ + JSON body to send with the request + """ + cookies: dict[str, str] | None = None + """ + Cookies + """ + params: str | dict[str, Any] | None = None + """ + The parameters to send with the request + """ + follow_redirects: StrictBool | None = None + """ + Follow redirects + """ + + +class ApiCallDefUpdate(BaseModel): + """ + API call definition + """ + + model_config = ConfigDict( + populate_by_name=True, + ) + method: ( + Literal[ + "GET", + "POST", + "PUT", + "DELETE", + "PATCH", + "HEAD", + "OPTIONS", + "CONNECT", + "TRACE", + ] + | None + ) = None + """ + The HTTP method to use + """ + url: AnyUrl | None = None + """ + The URL to call + """ + headers: dict[str, str] | None = None + """ + The headers to send with the request + """ + content: str | None = None + """ + The content as base64 to send with the request + """ + data: dict[str, str] | None = None + """ + The data to send as form data + """ + json_: Annotated[dict[str, Any] | None, Field(None, alias="json")] + """ + JSON body to send with the request + """ + cookies: dict[str, str] | None = None + """ + Cookies + """ + params: str | dict[str, Any] | None = None + """ + The parameters to send with the request + """ + follow_redirects: StrictBool | None = None + """ + Follow redirects + """ class ChosenToolCall(BaseModel): @@ -37,12 +144,26 @@ class CreateToolRequest(BaseModel): """ Name of the tool (must be unique for this agent and a valid python identifier string ) """ + description: str | None = None + """ + Description of the tool + """ function: FunctionDef | None = None """ The function to call """ integration: IntegrationDef | None = None + """ + The integration to call + """ system: SystemDef | None = None + """ + The system to call + """ + api_call: ApiCallDef | None = None + """ + The API call to make + """ class FunctionCallOption(BaseModel): @@ -104,10 +225,6 @@ class IntegrationDef(BaseModel): """ The specific method of the integration to call """ - description: str | None = None - """ - Optional description of the integration - """ setup: dict[str, Any] | None = None """ The setup parameters the integration accepts @@ -146,10 +263,6 @@ class IntegrationDefUpdate(BaseModel): """ The specific method of the integration to call """ - description: str | None = None - """ - Optional description of the integration - """ setup: dict[str, Any] | None = None """ The setup parameters the integration accepts @@ -179,12 +292,26 @@ class PatchToolRequest(BaseModel): """ Name of the tool (must be unique for this agent and a valid python identifier string ) """ + description: str | None = None + """ + Description of the tool + """ function: FunctionDef | None = None """ The function to call """ integration: IntegrationDefUpdate | None = None + """ + The integration to call + """ system: SystemDefUpdate | None = None + """ + The system to call + """ + api_call: ApiCallDefUpdate | None = None + """ + The API call to make + """ class SystemDef(BaseModel): @@ -199,10 +326,6 @@ class SystemDef(BaseModel): """ The name of the system call """ - description: str | None = None - """ - Optional description of the system call - """ arguments: dict[str, Any] | None = None """ The arguments to pre-apply to the system call @@ -221,10 +344,6 @@ class SystemDefUpdate(BaseModel): """ The name of the system call """ - description: str | None = None - """ - Optional description of the system call - """ arguments: dict[str, Any] | None = None """ The arguments to pre-apply to the system call @@ -239,12 +358,26 @@ class Tool(BaseModel): """ Name of the tool (must be unique for this agent and a valid python identifier string ) """ + description: str | None = None + """ + Description of the tool + """ function: FunctionDef | None = None """ The function to call """ integration: IntegrationDef | None = None + """ + The integration to call + """ system: SystemDef | None = None + """ + The system to call + """ + api_call: ApiCallDef | None = None + """ + The API call to make + """ created_at: Annotated[AwareDatetime, Field(json_schema_extra={"readOnly": True})] """ When this resource was created as UTC date-time @@ -279,12 +412,26 @@ class UpdateToolRequest(BaseModel): """ Name of the tool (must be unique for this agent and a valid python identifier string ) """ + description: str | None = None + """ + Description of the tool + """ function: FunctionDef | None = None """ The function to call """ integration: IntegrationDef | None = None + """ + The integration to call + """ system: SystemDef | None = None + """ + The system to call + """ + api_call: ApiCallDef | None = None + """ + The API call to make + """ class ChosenFunctionCall(ChosenToolCall): diff --git a/agents-api/agents_api/models/chat/prepare_chat_context.py b/agents-api/agents_api/models/chat/prepare_chat_context.py index 9be2d64aa..f77686d7a 100644 --- a/agents-api/agents_api/models/chat/prepare_chat_context.py +++ b/agents-api/agents_api/models/chat/prepare_chat_context.py @@ -90,12 +90,13 @@ def prepare_chat_context( participant_type: "agent", }, - *tools { agent_id, tool_id, name, type, spec, updated_at, created_at }, + *tools { agent_id, tool_id, name, type, spec, description, updated_at, created_at }, tool = { "id": tool_id, "name": name, "type": type, "spec": spec, + "description": description, "updated_at": updated_at, "created_at": created_at, } diff --git a/agents-api/agents_api/models/execution/prepare_execution_input.py b/agents-api/agents_api/models/execution/prepare_execution_input.py index 5f30e7f83..513c44a16 100644 --- a/agents-api/agents_api/models/execution/prepare_execution_input.py +++ b/agents-api/agents_api/models/execution/prepare_execution_input.py @@ -149,6 +149,7 @@ def prepare_execution_input( "name", "type", "spec", + "description", "created_at", "updated_at", ) diff --git a/agents-api/agents_api/models/task/create_or_update_task.py b/agents-api/agents_api/models/task/create_or_update_task.py index af7e258d9..d787d78b5 100644 --- a/agents-api/agents_api/models/task/create_or_update_task.py +++ b/agents-api/agents_api/models/task/create_or_update_task.py @@ -64,7 +64,9 @@ def create_or_update_task( data.metadata = data.metadata or {} data.input_schema = data.input_schema or {} - task_data = task_to_spec(data).model_dump(exclude_none=True, exclude_unset=True) + task_data = task_to_spec(data).model_dump( + exclude_none=True, exclude_unset=True, mode="json" + ) task_data.pop("task_id", None) task_data["created_at"] = utcnow().timestamp() diff --git a/agents-api/agents_api/models/task/create_task.py b/agents-api/agents_api/models/task/create_task.py index a44146c34..9affe0ead 100644 --- a/agents-api/agents_api/models/task/create_task.py +++ b/agents-api/agents_api/models/task/create_task.py @@ -55,7 +55,7 @@ def create_task( # Prepares the update data by filtering out None values and adding user_id and developer_id. columns, values = cozo_process_mutate_data( { - **task_spec.model_dump(exclude_none=True, exclude_unset=True), + **task_spec.model_dump(exclude_none=True, exclude_unset=True, mode="json"), "task_id": str(task_id), "agent_id": str(agent_id), } diff --git a/agents-api/agents_api/models/tools/create_tools.py b/agents-api/agents_api/models/tools/create_tools.py index fe7e28228..b98a751d0 100644 --- a/agents-api/agents_api/models/tools/create_tools.py +++ b/agents-api/agents_api/models/tools/create_tools.py @@ -65,32 +65,35 @@ def create_tools( tool.type, tool.name, getattr(tool, tool.type).dict(), + tool.description if hasattr(tool, "description") else None, ] for tool in data ] ensure_tool_name_unique_query = """ - input[agent_id, tool_id, type, name, spec] <- $records + input[agent_id, tool_id, type, name, spec, description] <- $records ?[tool_id] := - input[agent_id, _, type, name, _], + input[agent_id, _, type, name, _, _], *tools{ agent_id: to_uuid(agent_id), tool_id, type, name, + spec, + description, } :limit 1 :assert none """ - # Datalog query for inserting new tool records into the 'agent_functions' relation + # Datalog query for inserting new tool records into the 'tools' relation create_query = """ - input[agent_id, tool_id, type, name, spec] <- $records + input[agent_id, tool_id, type, name, spec, description] <- $records # Do not add duplicate - ?[agent_id, tool_id, type, name, spec] := - input[agent_id, tool_id, type, name, spec], + ?[agent_id, tool_id, type, name, spec, description] := + input[agent_id, tool_id, type, name, spec, description], not *tools{ agent_id: to_uuid(agent_id), type, @@ -103,6 +106,7 @@ def create_tools( type, name, spec, + description, } :returning """ diff --git a/agents-api/agents_api/models/tools/list_tools.py b/agents-api/agents_api/models/tools/list_tools.py index 931ca3ca9..727bf8028 100644 --- a/agents-api/agents_api/models/tools/list_tools.py +++ b/agents-api/agents_api/models/tools/list_tools.py @@ -30,7 +30,11 @@ @wrap_in_class( Tool, transform=lambda d: { - d["type"]: {**d.pop("spec"), "name": d["name"]}, + d["type"]: { + **d.pop("spec"), + "name": d["name"], + "description": d["description"], + }, **d, }, ) @@ -58,6 +62,7 @@ def list_tools( name, type, spec, + description, updated_at, created_at, ] := input[agent_id], @@ -67,6 +72,7 @@ def list_tools( name, type, spec, + description, updated_at, created_at, }} diff --git a/agents-api/agents_api/routers/agents/create_agent.py b/agents-api/agents_api/routers/agents/create_agent.py index a662bef15..2e1c4df0a 100644 --- a/agents-api/agents_api/routers/agents/create_agent.py +++ b/agents-api/agents_api/routers/agents/create_agent.py @@ -4,13 +4,12 @@ from fastapi import Depends from starlette.status import HTTP_201_CREATED -import agents_api.models as models - from ...autogen.openapi_model import ( CreateAgentRequest, ResourceCreatedResponse, ) from ...dependencies.developer_id import get_developer_id +from ...models.agent.create_agent import create_agent as create_agent_query from .router import router @@ -20,7 +19,7 @@ async def create_agent( data: CreateAgentRequest, ) -> ResourceCreatedResponse: # TODO: Validate model name - agent = models.agent.create_agent( + agent = create_agent_query( developer_id=x_developer_id, data=data, ) diff --git a/agents-api/agents_api/worker/worker.py b/agents-api/agents_api/worker/worker.py index 77698364d..08322772f 100644 --- a/agents-api/agents_api/worker/worker.py +++ b/agents-api/agents_api/worker/worker.py @@ -15,6 +15,7 @@ def create_worker(client: Client) -> Any: from ..activities import task_steps from ..activities.demo import demo_activity from ..activities.embed_docs import embed_docs + from ..activities.excecute_api_call import execute_api_call from ..activities.execute_integration import execute_integration from ..activities.mem_mgmt import mem_mgmt from ..activities.mem_rating import mem_rating @@ -52,6 +53,7 @@ def create_worker(client: Client) -> Any: demo_activity, embed_docs, execute_integration, + execute_api_call, mem_mgmt, mem_rating, summarization, diff --git a/agents-api/agents_api/workflows/task_execution/__init__.py b/agents-api/agents_api/workflows/task_execution/__init__.py index 2ca7e6ade..6023f6f25 100644 --- a/agents-api/agents_api/workflows/task_execution/__init__.py +++ b/agents-api/agents_api/workflows/task_execution/__init__.py @@ -11,8 +11,10 @@ # Import necessary modules and types with workflow.unsafe.imports_passed_through(): from ...activities import task_steps + from ...activities.excecute_api_call import execute_api_call from ...activities.execute_integration import execute_integration from ...autogen.openapi_model import ( + ApiCallDef, EmbedStep, ErrorWorkflowStep, EvaluateStep, @@ -505,6 +507,44 @@ async def run( state = PartialTransition(output=tool_call_response) + case ToolCallStep(), StepOutcome(output=tool_call) if tool_call[ + "type" + ] == "api_call": + call = tool_call["api_call"] + tool_name = call["name"] + arguments = call["arguments"] + apicall_spec = next( + (t for t in context.tools if t.name == tool_name), None + ) + + if apicall_spec is None: + raise ApplicationError(f"Integration {tool_name} not found") + + api_call = ApiCallDef( + method=apicall_spec.spec["method"], + url=apicall_spec.spec["url"], + headers=apicall_spec.spec["headers"], + follow_redirects=apicall_spec.spec["follow_redirects"], + ) + + if "json_" in arguments: + arguments["json"] = arguments["json_"] + del arguments["json_"] + + # Execute the API call using the `execute_api_call` function + tool_call_response = await workflow.execute_activity( + execute_api_call, + args=[ + api_call, + arguments, + ], + schedule_to_close_timeout=timedelta( + seconds=30 if debug or testing else 600 + ), + ) + + state = PartialTransition(output=tool_call_response) + case ToolCallStep(), StepOutcome(output=_): # FIXME: Handle system/api_call tool_calls raise ApplicationError("Not implemented") diff --git a/agents-api/migrations/migrate_1727922523_add_description_to_tools.py b/agents-api/migrations/migrate_1727922523_add_description_to_tools.py new file mode 100644 index 000000000..1d6724090 --- /dev/null +++ b/agents-api/migrations/migrate_1727922523_add_description_to_tools.py @@ -0,0 +1,64 @@ +# /usr/bin/env python3 + +MIGRATION_ID = "add_description_to_tools" +CREATED_AT = 1727922523.283493 + + +add_description_to_tools = dict( + up=""" + ?[agent_id, tool_id, type, name, description, spec, updated_at, created_at] := *tools { + agent_id, tool_id, type, name, spec, updated_at, created_at + }, description = null + + :replace tools { + agent_id: Uuid, + tool_id: Uuid, + => + type: String, + name: String, + description: String?, + spec: Json, + + updated_at: Float default now(), + created_at: Float default now(), + } + """, + down=""" + ?[agent_id, tool_id, type, name, spec, updated_at, created_at] := *tools { + agent_id, tool_id, type, name, spec, updated_at, created_at + } + + :replace tools { + agent_id: Uuid, + tool_id: Uuid, + => + type: String, + name: String, + spec: Json, + + updated_at: Float default now(), + created_at: Float default now(), + } + """, +) + + +queries_to_run = [ + add_description_to_tools, +] + + +def run(client, *queries): + joiner = "}\n\n{" + + query = joiner.join(queries) + query = f"{{\n{query}\n}}" + client.run(query) + + +def up(client): + run(client, *[q["up"] for q in queries_to_run]) + + +def down(client): + run(client, *[q["down"] for q in reversed(queries_to_run)]) diff --git a/agents-api/tests/test_docs_queries.py b/agents-api/tests/test_docs_queries.py index fcf7f9bd6..b0f886c4f 100644 --- a/agents-api/tests/test_docs_queries.py +++ b/agents-api/tests/test_docs_queries.py @@ -41,6 +41,7 @@ def _( ) +# TODO: Execute embedding workflow to fix this test and other docs tests @test("model: get docs") def _(client=cozo_client, doc=test_doc, developer_id=test_developer_id): get_doc( diff --git a/agents-api/tests/test_execution_workflow.py b/agents-api/tests/test_execution_workflow.py index 3df23e5cd..2a4fd7f75 100644 --- a/agents-api/tests/test_execution_workflow.py +++ b/agents-api/tests/test_execution_workflow.py @@ -440,6 +440,65 @@ async def _( assert result["hello"] == data.input["test"] +@test("workflow: tool call api_call") +async def _( + client=cozo_client, + developer_id=test_developer_id, + agent=test_agent, +): + data = CreateExecutionRequest(input={"test": "input"}) + + task = create_task( + developer_id=developer_id, + agent_id=agent.id, + data=CreateTaskRequest( + **{ + "name": "test task", + "description": "test task about", + "input_schema": {"type": "object", "additionalProperties": True}, + "tools": [ + { + "type": "api_call", + "name": "hello", + "api_call": { + "method": "GET", + "url": "https://httpbin.org/get", + }, + } + ], + "main": [ + { + "tool": "hello", + "arguments": { + "params": {"test": "_.test"}, + }, + }, + { + "evaluate": {"hello": "_.json.args.test"}, + }, + ], + } + ), + client=client, + ) + + async with patch_testing_temporal() as (_, mock_run_task_execution_workflow): + execution, handle = await start_execution( + developer_id=developer_id, + task_id=task.id, + data=data, + client=client, + ) + + assert handle is not None + assert execution.task_id == task.id + assert execution.input == data.input + mock_run_task_execution_workflow.assert_called_once() + + result = await handle.result() + assert result["hello"] == data.input["test"] + + @test("workflow: tool call integration dummy") async def _( client=cozo_client, diff --git a/typespec/common/scalars.tsp b/typespec/common/scalars.tsp index 76ccef2d3..8dc07cbbc 100644 --- a/typespec/common/scalars.tsp +++ b/typespec/common/scalars.tsp @@ -69,3 +69,6 @@ alias integrationProvider = ( // | "webpage" // | "requests" ); + +/** A valid HTTP method */ +alias httpMethod = "GET" | "POST" | "PUT" | "DELETE" | "PATCH" | "HEAD" | "OPTIONS" | "CONNECT" | "TRACE"; diff --git a/typespec/tasks/steps.tsp b/typespec/tasks/steps.tsp index 2267ae320..7a5f6d5b9 100644 --- a/typespec/tasks/steps.tsp +++ b/typespec/tasks/steps.tsp @@ -82,7 +82,7 @@ model ToolCallStepDef { tool: validPythonIdentifier; /** The input parameters for the tool (defaults to last step output) */ - arguments: ExpressionObject | "_" = "_"; + arguments: NestedExpressionObject | "_" = "_"; } model PromptStep extends BaseWorkflowStep<"prompt"> { diff --git a/typespec/tools/models.tsp b/typespec/tools/models.tsp index 8a8cead44..94243d20f 100644 --- a/typespec/tools/models.tsp +++ b/typespec/tools/models.tsp @@ -50,9 +50,6 @@ model IntegrationDef { /** The specific method of the integration to call */ method?: string; - /** Optional description of the integration */ - description?: string; - /** The setup parameters the integration accepts */ setup?: FunctionParameters; @@ -65,23 +62,59 @@ model SystemDef { /** The name of the system call */ call: string; - /** Optional description of the system call */ - description?: string; - /** The arguments to pre-apply to the system call */ arguments?: FunctionParameters; } -// TODO: We should use this model for all tools, not just functions and discriminate on the type +/** API call definition */ +model ApiCallDef { + /** The HTTP method to use */ + method: httpMethod; + + /** The URL to call */ + url: url; + + /** The headers to send with the request */ + headers?: Record; + + /** The content as base64 to send with the request */ + content?: string; + + /** The data to send as form data */ + data?: Record; + + /** JSON body to send with the request */ + json?: Record; + + /** Cookies */ + cookies?: Record; + + /** The parameters to send with the request */ + params?: string | Record; + + /** Follow redirects */ + follow_redirects?: boolean; +} + + model Tool { /** Name of the tool (must be unique for this agent and a valid python identifier string )*/ name: validPythonIdentifier; + /** Description of the tool */ + description?: string; + /** The function to call */ function?: FunctionDef; + + /** The integration to call */ integration?: IntegrationDef; + + /** The system to call */ system?: SystemDef; - api_call?: never; // TODO: Implement + + /** The API call to make */ + api_call?: ApiCallDef; ...HasTimestamps; ...HasId; @@ -94,9 +127,9 @@ model FunctionCallOption { model NamedToolChoice { function?: FunctionCallOption; - integration?: never; // TODO: Implement - system?: never; // TODO: Implement - api_call?: never; // TODO: Implement + integration?: never; + system?: never; + api_call?: never; } model ToolResponse { @@ -128,9 +161,9 @@ model ChosenToolCall { type: ToolType; function?: FunctionCallOption; - integration?: never; // TODO: Implement - system?: never; // TODO: Implement - api_call?: never; // TODO: Implement + integration?: never; + system?: never; + api_call?: never; ...HasId; }