-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathapi_request_tool.py
More file actions
358 lines (304 loc) · 10.9 KB
/
api_request_tool.py
File metadata and controls
358 lines (304 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# This file was auto-generated by Fern from our API Definition.
from __future__ import annotations
import datetime as dt
import typing
import pydantic
import typing_extensions
from ..core.pydantic_utilities import IS_PYDANTIC_V2, update_forward_refs
from ..core.serialization import FieldMetadata
from ..core.unchecked_base_model import UncheckedBaseModel
from .api_request_tool_messages_item import ApiRequestToolMessagesItem
from .api_request_tool_method import ApiRequestToolMethod
from .backoff_plan import BackoffPlan
from .json_schema import JsonSchema
from .tool_rejection_plan import ToolRejectionPlan
from .variable_extraction_plan import VariableExtractionPlan
class ApiRequestTool(UncheckedBaseModel):
messages: typing.Optional[typing.List[ApiRequestToolMessagesItem]] = pydantic.Field(default=None)
"""
These are the messages that will be spoken to the user as the tool is running.
For some tools, this is auto-filled based on special fields like `tool.destinations`. For others like the function tool, these can be custom configured.
"""
type: typing.Literal["apiRequest"] = "apiRequest"
method: ApiRequestToolMethod
timeout_seconds: typing_extensions.Annotated[typing.Optional[float], FieldMetadata(alias="timeoutSeconds")] = (
pydantic.Field(default=None)
)
"""
This is the timeout in seconds for the request. Defaults to 20 seconds.
@default 20
"""
credential_id: typing_extensions.Annotated[typing.Optional[str], FieldMetadata(alias="credentialId")] = (
pydantic.Field(default=None)
)
"""
The credential ID for API request authentication
"""
id: str = pydantic.Field()
"""
This is the unique identifier for the tool.
"""
org_id: typing_extensions.Annotated[str, FieldMetadata(alias="orgId")] = pydantic.Field()
"""
This is the unique identifier for the organization that this tool belongs to.
"""
created_at: typing_extensions.Annotated[dt.datetime, FieldMetadata(alias="createdAt")] = pydantic.Field()
"""
This is the ISO 8601 date-time string of when the tool was created.
"""
updated_at: typing_extensions.Annotated[dt.datetime, FieldMetadata(alias="updatedAt")] = pydantic.Field()
"""
This is the ISO 8601 date-time string of when the tool was last updated.
"""
rejection_plan: typing_extensions.Annotated[
typing.Optional[ToolRejectionPlan], FieldMetadata(alias="rejectionPlan")
] = pydantic.Field(default=None)
"""
This is the plan to reject a tool call based on the conversation state.
// Example 1: Reject endCall if user didn't say goodbye
```json
{
conditions: [{
type: 'regex',
regex: '(?i)\\\\b(bye|goodbye|farewell|see you later|take care)\\\\b',
target: { position: -1, role: 'user' },
negate: true // Reject if pattern does NOT match
}]
}
```
// Example 2: Reject transfer if user is actually asking a question
```json
{
conditions: [{
type: 'regex',
regex: '\\\\?',
target: { position: -1, role: 'user' }
}]
}
```
// Example 3: Reject transfer if user didn't mention transfer recently
```json
{
conditions: [{
type: 'liquid',
liquid: `{% assign recentMessages = messages | last: 5 %}
{% assign userMessages = recentMessages | where: 'role', 'user' %}
{% assign mentioned = false %}
{% for msg in userMessages %}
{% if msg.content contains 'transfer' or msg.content contains 'connect' or msg.content contains 'speak to' %}
{% assign mentioned = true %}
{% break %}
{% endif %}
{% endfor %}
{% if mentioned %}
false
{% else %}
true
{% endif %}`
}]
}
```
// Example 4: Reject endCall if the bot is looping and trying to exit
```json
{
conditions: [{
type: 'liquid',
liquid: `{% assign recentMessages = messages | last: 6 %}
{% assign userMessages = recentMessages | where: 'role', 'user' | reverse %}
{% if userMessages.size < 3 %}
false
{% else %}
{% assign msg1 = userMessages[0].content | downcase %}
{% assign msg2 = userMessages[1].content | downcase %}
{% assign msg3 = userMessages[2].content | downcase %}
{% comment %} Check for repetitive messages {% endcomment %}
{% if msg1 == msg2 or msg1 == msg3 or msg2 == msg3 %}
true
{% comment %} Check for common loop phrases {% endcomment %}
{% elsif msg1 contains 'cool thanks' or msg2 contains 'cool thanks' or msg3 contains 'cool thanks' %}
true
{% elsif msg1 contains 'okay thanks' or msg2 contains 'okay thanks' or msg3 contains 'okay thanks' %}
true
{% elsif msg1 contains 'got it' or msg2 contains 'got it' or msg3 contains 'got it' %}
true
{% else %}
false
{% endif %}
{% endif %}`
}]
}
```
"""
name: typing.Optional[str] = pydantic.Field(default=None)
"""
This is the name of the tool. This will be passed to the model.
Must be a-z, A-Z, 0-9, or contain underscores and dashes, with a maximum length of 40.
"""
description: typing.Optional[str] = pydantic.Field(default=None)
"""
This is the description of the tool. This will be passed to the model.
"""
url: str = pydantic.Field()
"""
This is where the request will be sent.
"""
body: typing.Optional[JsonSchema] = pydantic.Field(default=None)
"""
This is the body of the request.
"""
headers: typing.Optional[JsonSchema] = pydantic.Field(default=None)
"""
These are the headers to send with the request.
"""
backoff_plan: typing_extensions.Annotated[typing.Optional[BackoffPlan], FieldMetadata(alias="backoffPlan")] = (
pydantic.Field(default=None)
)
"""
This is the backoff plan if the request fails. Defaults to undefined (the request will not be retried).
@default undefined (the request will not be retried)
"""
variable_extraction_plan: typing_extensions.Annotated[
typing.Optional[VariableExtractionPlan], FieldMetadata(alias="variableExtractionPlan")
] = pydantic.Field(default=None)
"""
This is the plan to extract variables from the tool's response. These will be accessible during the call and stored in `call.artifact.variableValues` after the call.
Usage:
1. Use `aliases` to extract variables from the tool's response body. (Most common case)
```json
{
"aliases": [
{
"key": "customerName",
"value": "{{customer.name}}"
},
{
"key": "customerAge",
"value": "{{customer.age}}"
}
]
}
```
The tool response body is made available to the liquid template.
2. Use `aliases` to extract variables from the tool's response body if the response is an array.
```json
{
"aliases": [
{
"key": "customerName",
"value": "{{$[0].name}}"
},
{
"key": "customerAge",
"value": "{{$[0].age}}"
}
]
}
```
$ is a shorthand for the tool's response body. `$[0]` is the first item in the array. `$[n]` is the nth item in the array. Note, $ is available regardless of the response body type (both object and array).
3. Use `aliases` to extract variables from the tool's response headers.
```json
{
"aliases": [
{
"key": "customerName",
"value": "{{tool.response.headers.customer-name}}"
},
{
"key": "customerAge",
"value": "{{tool.response.headers.customer-age}}"
}
]
}
```
`tool.response` is made available to the liquid template. Particularly, both `tool.response.headers` and `tool.response.body` are available. Note, `tool.response` is available regardless of the response body type (both object and array).
4. Use `schema` to extract a large portion of the tool's response body.
4.1. If you hit example.com and it returns `{"name": "John", "age": 30}`, then you can specify the schema as:
```json
{
"schema": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"age": {
"type": "number"
}
}
}
}
```
These will be extracted as `{{ name }}` and `{{ age }}` respectively. To emphasize, object properties are extracted as direct global variables.
4.2. If you hit example.com and it returns `{"name": {"first": "John", "last": "Doe"}}`, then you can specify the schema as:
```json
{
"schema": {
"type": "object",
"properties": {
"name": {
"type": "object",
"properties": {
"first": {
"type": "string"
},
"last": {
"type": "string"
}
}
}
}
}
}
```
These will be extracted as `{{ name }}`. And, `{{ name.first }}` and `{{ name.last }}` will be accessible.
4.3. If you hit example.com and it returns `["94123", "94124"]`, then you can specify the schema as:
```json
{
"schema": {
"type": "array",
"title": "zipCodes",
"items": {
"type": "string"
}
}
}
```
This will be extracted as `{{ zipCodes }}`. To access the array items, you can use `{{ zipCodes[0] }}` and `{{ zipCodes[1] }}`.
4.4. If you hit example.com and it returns `[{"name": "John", "age": 30, "zipCodes": ["94123", "94124"]}, {"name": "Jane", "age": 25, "zipCodes": ["94125", "94126"]}]`, then you can specify the schema as:
```json
{
"schema": {
"type": "array",
"title": "people",
"items": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"age": {
"type": "number"
},
"zipCodes": {
"type": "array",
"items": {
"type": "string"
}
}
}
}
}
}
```
This will be extracted as `{{ people }}`. To access the array items, you can use `{{ people[n].name }}`, `{{ people[n].age }}`, `{{ people[n].zipCodes }}`, `{{ people[n].zipCodes[0] }}` and `{{ people[n].zipCodes[1] }}`.
Note: Both `aliases` and `schema` can be used together.
"""
if IS_PYDANTIC_V2:
model_config: typing.ClassVar[pydantic.ConfigDict] = pydantic.ConfigDict(extra="allow", frozen=True) # type: ignore # Pydantic v2
else:
class Config:
frozen = True
smart_union = True
extra = pydantic.Extra.allow
from .group_condition import GroupCondition # noqa: E402, F401, I001
update_forward_refs(ApiRequestTool)