Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add API call tool type #571

Merged
merged 20 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
167 changes: 165 additions & 2 deletions agents-api/agents_api/autogen/Tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -41,12 +148,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):
Expand Down Expand Up @@ -170,7 +291,7 @@ class NamedToolChoice(BaseModel):
)
type: Literal["function", "integration", "system", "api_call"]
"""
Whether this tool is a `function`, `api_call`, `system` etc. (Only `function` tool supported right now)
Whether this tool is a `function`
"""
function: FunctionCallOption | None = None

Expand All @@ -191,12 +312,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):
Expand Down Expand Up @@ -255,12 +390,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
Expand Down Expand Up @@ -299,12 +448,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):
Expand Down
3 changes: 2 additions & 1 deletion agents-api/agents_api/models/chat/prepare_chat_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ def prepare_execution_input(
"name",
"type",
"spec",
"description",
"created_at",
"updated_at",
)
Expand Down
8 changes: 5 additions & 3 deletions agents-api/agents_api/models/tools/create_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ def create_tools(
str(uuid4()),
tool.type,
tool.name,
tool.description,
getattr(tool, tool.type).dict(),
]
for tool in data
Expand All @@ -85,11 +86,11 @@ def create_tools(

# Datalog query for inserting new tool records into the 'agent_functions' 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,
Expand All @@ -102,6 +103,7 @@ def create_tools(
type,
name,
spec,
description,
}
:returning
"""
Expand Down
8 changes: 7 additions & 1 deletion agents-api/agents_api/models/tools/list_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
HamadaSalhab marked this conversation as resolved.
Show resolved Hide resolved
},
**d,
},
)
Expand Down Expand Up @@ -58,6 +62,7 @@ def list_tools(
name,
type,
spec,
description,
updated_at,
created_at,
] := input[agent_id],
Expand All @@ -67,6 +72,7 @@ def list_tools(
name,
type,
spec,
description,
updated_at,
created_at,
}}
Expand Down
3 changes: 3 additions & 0 deletions typespec/common/scalars.tsp
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,6 @@ alias integrationProvider = (
// | "webpage"
// | "requests"
);

/** A valid HTTP method */
alias httpMethod = "GET" | "POST" | "PUT" | "DELETE" | "PATCH" | "HEAD" | "OPTIONS" | "CONNECT" | "TRACE";
Loading
Loading