Skip to content

Commit e6af6e0

Browse files
authored
fix: update custom tool example (#8109)
1 parent 248a3ee commit e6af6e0

File tree

1 file changed

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

1 file changed

+30
-26
lines changed

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

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export function getStaticProps(context) {
3030

3131

3232

33-
Tools allow LLMs to take action or query information so it can respond with up to date information. There are a few different ways to define LLM tools in the Amplify AI kit.
33+
Tools allow LLMs to take action or query information so it can respond with up to date information. There are a few different ways to define LLM tools in the Amplify AI kit.
3434

3535
1. Model tools
3636
2. Query tools
@@ -75,7 +75,7 @@ This will let the LLM list and filter `Post` records. Because the data schema ha
7575

7676
## Query tools
7777

78-
You can also give the LLM access to custom queries. You can define a custom query with a [Function](/[platform]/build-a-backend/functions/set-up-function/) handler and then reference that custom query as a tool.
78+
You can also give the LLM access to custom queries. You can define a custom query with a [Function](/[platform]/build-a-backend/functions/set-up-function/) handler and then reference that custom query as a tool.
7979

8080
```ts title="amplify/data/resource.ts"
8181
// highlight-start
@@ -117,7 +117,7 @@ const schema = a.schema({
117117
});
118118
```
119119

120-
Because the definition of the query itself has the shape of the inputs and outputs (arguments and returns), the Amplify data tool can automatically tell the LLM exactly how to call the custom query.
120+
Because the definition of the query itself has the shape of the inputs and outputs (arguments and returns), the Amplify data tool can automatically tell the LLM exactly how to call the custom query.
121121

122122
<Callout>
123123

@@ -191,6 +191,12 @@ backend.getWeather.resources.lambda.addToRolePolicy(
191191

192192
Conversation routes can also have completely custom tools defined in a Lambda handler.
193193

194+
### Install the backend-ai package
195+
196+
```bash
197+
npm install @aws-amplify/backend-ai
198+
```
199+
194200
### Create a custom conversation handler function
195201

196202
```ts title="amplify/data/resource.ts"
@@ -217,28 +223,16 @@ const schema = a.schema({
217223
### Implement the custom handler
218224

219225
```ts title="amplify/data/chatHandler.ts"
220-
221226
import {
222-
ConversationTurnEvent,
223-
ExecutableTool,
224-
handleConversationTurnEvent,
227+
ConversationTurnEvent,
228+
handleConversationTurnEvent,
225229
} from '@aws-amplify/ai-constructs/conversation/runtime';
226-
import { ToolResultContentBlock } from '@aws-sdk/client-bedrock-runtime';
230+
import { createExecutableTool } from '@aws-amplify/backend-ai/conversation/runtime';
227231

228-
const thermometer: ExecutableTool = {
229-
name: 'thermometer',
230-
description: 'Returns current temperature in a city',
231-
execute: (input): Promise<ToolResultContentBlock> => {
232-
if (input.city === 'Seattle') {
233-
return Promise.resolve({
234-
text: `75F`,
235-
});
236-
}
237-
return Promise.resolve({
238-
text: 'unknown'
239-
})
240-
},
241-
inputSchema: {
232+
const thermometer = createExecutableTool(
233+
'thermometer',
234+
'Returns current temperature in a city',
235+
{
242236
json: {
243237
type: 'object',
244238
'properties': {
@@ -249,12 +243,22 @@ const thermometer: ExecutableTool = {
249243
},
250244
required: ['city']
251245
}
252-
}
253-
};
246+
},
247+
(input) => {
248+
if (input.city === 'Seattle') {
249+
return Promise.resolve({
250+
text: '75F',
251+
});
252+
}
253+
return Promise.resolve({
254+
text: 'unknown'
255+
})
256+
},
257+
);
254258

255259
/**
256-
* Handler with simple tool.
257-
*/
260+
* Handler with simple tool.
261+
*/
258262
export const handler = async (event: ConversationTurnEvent) => {
259263
await handleConversationTurnEvent(event, {
260264
tools: [thermometer],

0 commit comments

Comments
 (0)