Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: update AI kit > conversation > custom tool example #8109

Merged
merged 1 commit into from
Nov 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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