Skip to content

Commit b288e26

Browse files
authored
fix: add external tools (#319)
* fix: add external tools * fix: add external tools * fix: add external tools * fix: fixes * fix: remove redundant field * fix: remove redundant field * fix: remove default values
1 parent a5a298a commit b288e26

File tree

4 files changed

+77
-22
lines changed

4 files changed

+77
-22
lines changed

ai21/clients/common/maestro/run.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
OutputOptions,
1212
Requirement,
1313
RunResponse,
14-
Tool,
1514
ToolResources,
15+
ToolDefinition,
1616
)
1717
from ai21.types import NOT_GIVEN, NotGiven
1818
from ai21.utils.typing import remove_not_given
@@ -26,7 +26,7 @@ def _create_body(
2626
*,
2727
input: str | List[MaestroMessage],
2828
models: List[str] | NotGiven,
29-
tools: List[Tool] | NotGiven,
29+
tools: List[ToolDefinition] | NotGiven,
3030
tool_resources: ToolResources | NotGiven,
3131
requirements: List[Requirement] | NotGiven,
3232
budget: Budget | NotGiven,
@@ -54,7 +54,7 @@ def create(
5454
*,
5555
input: str | List[MaestroMessage],
5656
models: List[str] | NotGiven = NOT_GIVEN,
57-
tools: List[Tool] | NotGiven = NOT_GIVEN,
57+
tools: List[ToolDefinition] | NotGiven = NOT_GIVEN,
5858
tool_resources: ToolResources | NotGiven = NOT_GIVEN,
5959
requirements: List[Requirement] | NotGiven = NOT_GIVEN,
6060
budget: Budget | NotGiven = NOT_GIVEN,
@@ -78,7 +78,7 @@ def create_and_poll(
7878
*,
7979
input: str | List[MaestroMessage],
8080
models: List[str] | NotGiven = NOT_GIVEN,
81-
tools: List[Tool] | NotGiven = NOT_GIVEN,
81+
tools: List[ToolDefinition] | NotGiven = NOT_GIVEN,
8282
tool_resources: ToolResources | NotGiven = NOT_GIVEN,
8383
requirements: List[Requirement] | NotGiven = NOT_GIVEN,
8484
budget: Budget | NotGiven = NOT_GIVEN,

ai21/clients/studio/resources/maestro/run.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
Requirement,
1616
RunResponse,
1717
TERMINATED_RUN_STATUSES,
18-
Tool,
1918
ToolResources,
19+
ToolDefinition,
2020
)
2121
from ai21.types import NotGiven, NOT_GIVEN
2222

@@ -27,7 +27,7 @@ def create(
2727
*,
2828
input: str | List[MaestroMessage],
2929
models: List[str] | NotGiven = NOT_GIVEN,
30-
tools: List[Tool] | NotGiven = NOT_GIVEN,
30+
tools: List[ToolDefinition] | NotGiven = NOT_GIVEN,
3131
tool_resources: ToolResources | NotGiven = NOT_GIVEN,
3232
requirements: List[Requirement] | NotGiven = NOT_GIVEN,
3333
budget: Budget | NotGiven = NOT_GIVEN,
@@ -74,7 +74,7 @@ def create_and_poll(
7474
*,
7575
input: str | List[MaestroMessage],
7676
models: List[str] | NotGiven = NOT_GIVEN,
77-
tools: List[Tool] | NotGiven = NOT_GIVEN,
77+
tools: List[ToolDefinition] | NotGiven = NOT_GIVEN,
7878
tool_resources: ToolResources | NotGiven = NOT_GIVEN,
7979
requirements: List[Requirement] | NotGiven = NOT_GIVEN,
8080
budget: Budget | NotGiven = NOT_GIVEN,
@@ -107,7 +107,7 @@ async def create(
107107
*,
108108
input: str | List[MaestroMessage],
109109
models: List[str] | NotGiven = NOT_GIVEN,
110-
tools: List[Tool] | NotGiven = NOT_GIVEN,
110+
tools: List[ToolDefinition] | NotGiven = NOT_GIVEN,
111111
tool_resources: ToolResources | NotGiven = NOT_GIVEN,
112112
requirements: List[Requirement] | NotGiven = NOT_GIVEN,
113113
budget: Budget | NotGiven = NOT_GIVEN,
@@ -154,7 +154,7 @@ async def create_and_poll(
154154
*,
155155
input: str | List[MaestroMessage],
156156
models: List[str] | NotGiven = NOT_GIVEN,
157-
tools: List[Tool] | NotGiven = NOT_GIVEN,
157+
tools: List[ToolDefinition] | NotGiven = NOT_GIVEN,
158158
tool_resources: ToolResources | NotGiven = NOT_GIVEN,
159159
requirements: List[Requirement] | NotGiven = NOT_GIVEN,
160160
budget: Budget | NotGiven = NOT_GIVEN,

ai21/models/maestro/__init__.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,14 @@
55
MaestroMessage,
66
OutputOptions,
77
Requirement,
8-
Tool,
8+
ToolDefinition,
99
ToolResources,
1010
WebSearchResult,
11+
HttpTool,
12+
McpTool,
13+
Function,
14+
Endpoint,
15+
Parameters,
1116
)
1217

1318
__all__ = [
@@ -17,7 +22,12 @@
1722
"MaestroMessage",
1823
"OutputOptions",
1924
"Requirement",
20-
"Tool",
25+
"ToolDefinition",
2126
"ToolResources",
2227
"WebSearchResult",
28+
"HttpTool",
29+
"McpTool",
30+
"Function",
31+
"Endpoint",
32+
"Parameters",
2333
]

ai21/models/maestro/run.py

Lines changed: 56 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
from typing import Literal, List, Optional, Any, Set, Dict, Type, Union
2-
from typing_extensions import TypedDict
3-
from pydantic import BaseModel
2+
from typing_extensions import TypedDict, Required, Annotated
3+
from pydantic import BaseModel, Field
44

55
from ai21.models.ai21_base_model import AI21BaseModel
66

77
Budget = Literal["low", "medium", "high"]
88
Role = Literal["user", "assistant"]
99
RunStatus = Literal["completed", "failed", "in_progress", "requires_action"]
10-
ToolType = Literal["file_search", "web_search"]
1110
OutputOptions = Literal["data_sources", "requirements_result"]
1211
PrimitiveTypes = Union[Type[str], Type[int], Type[float], Type[bool]]
1312
PrimitiveLists = Type[List[PrimitiveTypes]]
@@ -23,11 +22,45 @@ class MaestroMessage(TypedDict):
2322
content: str
2423

2524

26-
class Tool(TypedDict):
27-
type: ToolType
25+
class HTTPToolFunctionParamProperties(TypedDict):
26+
type: str
27+
description: str
28+
29+
30+
class Parameters(TypedDict, total=False):
31+
type: Literal["object"]
32+
properties: Dict[str, HTTPToolFunctionParamProperties]
33+
required: List[str]
34+
additional_properties: Optional[bool]
35+
36+
37+
class Function(TypedDict):
38+
name: str
39+
description: str
40+
parameters: Parameters
41+
42+
43+
class Endpoint(TypedDict, total=False):
44+
url: str
45+
headers: Optional[dict]
46+
47+
48+
class HttpTool(TypedDict):
49+
type: Required[Literal["http"]]
50+
function: Function
51+
endpoint: Endpoint
2852

2953

30-
class FileSearchToolResource(TypedDict, total=False):
54+
class McpTool(TypedDict, total=False):
55+
type: Required[Literal["mcp"]]
56+
server_label: str
57+
server_url: str
58+
headers: Optional[dict]
59+
allowed_tools: Optional[List[str]]
60+
61+
62+
class FileSearch(TypedDict, total=False):
63+
type: Literal["file_search"]
3164
retrieval_similarity_threshold: Optional[float]
3265
labels: Optional[List[str]]
3366
labels_filter_mode: Optional[Literal["AND", "OR"]]
@@ -37,24 +70,36 @@ class FileSearchToolResource(TypedDict, total=False):
3770
max_neighbors: Optional[int]
3871

3972

40-
class WebSearchToolResource(TypedDict, total=False):
73+
class WebSearch(TypedDict, total=False):
74+
type: Literal["web_search"]
4175
urls: Optional[List[str]]
4276

4377

4478
class ToolResources(TypedDict, total=False):
45-
file_search: Optional[FileSearchToolResource]
46-
web_search: Optional[WebSearchToolResource]
79+
file_search: Optional[FileSearch]
80+
web_search: Optional[WebSearch]
81+
82+
83+
ToolDefinition = Annotated[
84+
Union[
85+
HttpTool,
86+
McpTool,
87+
FileSearch,
88+
WebSearch,
89+
],
90+
Field(discriminator="type"),
91+
]
4792

4893

4994
class Requirement(TypedDict, total=False):
5095
name: str
5196
description: str
52-
is_mandatory: bool = False
97+
is_mandatory: bool
5398

5499

55100
class RequirementResultItem(Requirement, total=False):
56101
score: float
57-
reason: Optional[str] = None
102+
reason: Optional[str]
58103

59104

60105
class RequirementsResult(TypedDict, total=False):

0 commit comments

Comments
 (0)