Skip to content

Commit 15c1f4c

Browse files
committed
use as const in custom lambda tool example
1 parent a28e962 commit 15c1f4c

File tree

1 file changed

+26
-24
lines changed
  • src/pages/[platform]/ai/conversation/tools

1 file changed

+26
-24
lines changed

src/pages/[platform]/ai/conversation/tools/index.mdx

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -258,35 +258,37 @@ import {
258258
handleConversationTurnEvent
259259
} from '@aws-amplify/backend-ai/conversation/runtime';
260260

261+
const jsonSchema = {
262+
json: {
263+
type: 'object',
264+
properties: {
265+
'operator': {
266+
'type': 'string',
267+
'enum': ['+', '-', '*', '/'],
268+
'description': 'The arithmetic operator to use'
269+
},
270+
'operands': {
271+
'type': 'array',
272+
'items': {
273+
'type': 'number'
274+
},
275+
'minItems': 2,
276+
'maxItems': 2,
277+
'description': 'Two numbers to perform the operation on'
278+
}
279+
},
280+
required: ['operator', 'operands']
281+
}
282+
} as const;
283+
// declare as const to allow the input type to be derived from the JSON schema in the tool handler definition.
284+
261285
const calculator = createExecutableTool(
262286
'calculator',
263287
'Returns the result of a simple calculation',
264-
{
265-
json: {
266-
type: 'object',
267-
properties: {
268-
'operator': {
269-
'type': 'string',
270-
'enum': ['+', '-', '*', '/'],
271-
'description': 'The arithmetic operator to use'
272-
},
273-
'operands': {
274-
'type': 'array',
275-
'items': {
276-
'type': 'number'
277-
},
278-
'minItems': 2,
279-
'maxItems': 2,
280-
'description': 'Two numbers to perform the operation on'
281-
}
282-
},
283-
required: ['operator', 'operands']
284-
}
285-
},
288+
jsonSchema,
286289
// input type is derived from the JSON schema
287290
(input) => {
288-
const [a, b] = input.operands as [number, number];
289-
291+
const [a, b] = input.operands;
290292
switch (input.operator) {
291293
case '+': return Promise.resolve({ text: (a + b).toString() });
292294
case '-': return Promise.resolve({ text: (a - b).toString() });

0 commit comments

Comments
 (0)