Skip to content

Commit

Permalink
fix: update custom tool example
Browse files Browse the repository at this point in the history
  • Loading branch information
atierian committed Nov 19, 2024
1 parent f4c9da3 commit b73a5cd
Showing 1 changed file with 30 additions and 26 deletions.
56 changes: 30 additions & 26 deletions src/pages/[platform]/ai/conversation/tools/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export function getStaticProps(context) {



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.
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.

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

## Query tools

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.
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.

```ts title="amplify/data/resource.ts"
// highlight-start
Expand Down Expand Up @@ -117,7 +117,7 @@ const schema = a.schema({
});
```

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.
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.

<Callout>

Expand Down Expand Up @@ -191,6 +191,12 @@ backend.getWeather.resources.lambda.addToRolePolicy(

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

### Install the backend-ai package

```bash
npm install @aws-amplify/backend-ai
```

### Create a custom conversation handler function

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

```ts title="amplify/data/chatHandler.ts"

import {
ConversationTurnEvent,
ExecutableTool,
handleConversationTurnEvent,
ConversationTurnEvent,
handleConversationTurnEvent,
} from '@aws-amplify/ai-constructs/conversation/runtime';
import { ToolResultContentBlock } from '@aws-sdk/client-bedrock-runtime';
import { createExecutableTool } from '@aws-amplify/backend-ai/conversation/runtime';

const thermometer: ExecutableTool = {
name: 'thermometer',
description: 'Returns current temperature in a city',
execute: (input): Promise<ToolResultContentBlock> => {
if (input.city === 'Seattle') {
return Promise.resolve({
text: `75F`,
});
}
return Promise.resolve({
text: 'unknown'
})
},
inputSchema: {
const thermometer = createExecutableTool(
'thermometer',
'Returns current temperature in a city',
{
json: {
type: 'object',
'properties': {
Expand All @@ -249,12 +243,22 @@ const thermometer: ExecutableTool = {
},
required: ['city']
}
}
};
},
(input) => {
if (input.city === 'Seattle') {
return Promise.resolve({
text: '75F',
});
}
return Promise.resolve({
text: 'unknown'
})
},
);

/**
* Handler with simple tool.
*/
* Handler with simple tool.
*/
export const handler = async (event: ConversationTurnEvent) => {
await handleConversationTurnEvent(event, {
tools: [thermometer],
Expand Down

0 comments on commit b73a5cd

Please sign in to comment.