You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/pages/[platform]/ai/conversation/tools/index.mdx
+141-77
Original file line number
Diff line number
Diff line change
@@ -29,24 +29,41 @@ export function getStaticProps(context) {
29
29
}
30
30
31
31
32
+
Tools allow LLMs to query information to respond with current and relevant information. They are invoked only if the LLM requests to use one based on the user's message and the tool's description.
32
33
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.
34
+
There are a few different ways to define LLM tools in the Amplify AI kit.
34
35
35
36
1. Model tools
36
37
2. Query tools
37
38
3. Lambda tools
38
39
39
-
The easiest way you can define tools for the LLM to use is with data models and custom queries in your data schema. When you define tools in your data schema, Amplify will take care of all of the heavy lifting required to properly implement such as:
40
+
The easiest way to define tools for your conversation route is with `a.ai.dataTool()` for data models and custom queries in your data schema. When you define a tool for a conversation route, Amplify takes care of the heavy lifting:
40
41
41
-
***Describing the tools to the LLM:**because each tool is a custom query or data model that is defined in the schema, Amplify knows the input shape needed for that tool
42
-
***Invoking the tool with the right parameters:**after the LLM responds it wants to call a tool, the code that initially called the LLM needs to then run that code.
43
-
***Maintaining the caller identity and authorization:**we don't want users to have access to more data through the LLM than they normally would, so when the LLM wants to invoke a tool we will call it with the user's identity. For example, if the LLM wanted to invoke a query to list Todos, it would only return the todos of the user and not everyone's todos.
42
+
***Describing the tools to the LLM:**Each tool definition is an Amplify model query or custom query that is defined in the schema. Amplify knows the input parameters needed for that tool and describes them to the LLM.
43
+
***Invoking the tool with the right parameters:**After the LLM requests to use a tool with necessary input parameters, the conversation handler Lambda function invokes the tool, returns the result to the LLM, and continues the conversation.
44
+
***Maintaining the caller identity and authorization:**Through tools, the LLM can only access data that the application user has access to. When the LLM requests to invoke a tool, we will call it with the user's identity. For example, if the LLM wanted to invoke a query to list Todos, it would only return the todos that user has access to.
44
45
45
46
## Model tools
46
47
47
-
You can give the LLM access to your data models by referencing them in an `a.ai.dataTool()` with a reference to a model in your data schema.
48
+
You can give the LLM access to your data models by referencing them in an `a.ai.dataTool()` with a reference to a model in your data schema. This requires that the model uses at least one of the following authorization strategies:
49
+
50
+
**[Per user data access](https://docs.amplify.aws/react/build-a-backend/data/customize-authz/per-user-per-owner-data-access/)**
51
+
-`owner()`
52
+
-`ownerDefinedIn()`
53
+
-`ownersDefinedIn()`
54
+
55
+
**[Any signed-in user data access](https://docs.amplify.aws/react/build-a-backend/data/customize-authz/signed-in-user-data-access/)**
56
+
-`authenticated()`
57
+
58
+
**[Per user group data access](https://docs.amplify.aws/react/build-a-backend/data/customize-authz/user-group-based-data-access/)**
59
+
-`group()`
60
+
-`groupsDefinedIn()`
61
+
-`groups()`
62
+
-`groupsDefinedIn()`
63
+
64
+
```ts title="amplify/data/resource.ts"
65
+
import { typeClientSchema, a, defineData } from"@aws-amplify/backend";
48
66
49
-
```ts
50
67
const schema =a.schema({
51
68
Post: a.model({
52
69
title: a.string(),
@@ -59,9 +76,14 @@ const schema = a.schema({
59
76
systemPrompt: 'Hello, world!',
60
77
tools: [
61
78
a.ai.dataTool({
79
+
// The name of the tool as it will be referenced in the message to the LLM
62
80
name: 'PostQuery',
81
+
// The description of the tool provided to the LLM.
82
+
// Use this to help the LLM understand when to use the tool.
63
83
description: 'Searches for Post records',
84
+
// A reference to the `a.model()` that the tool will use
64
85
model: a.ref('Post'),
86
+
// The operation to perform on the model
65
87
modelOperation: 'list',
66
88
}),
67
89
],
@@ -71,26 +93,29 @@ const schema = a.schema({
71
93
72
94
This will let the LLM list and filter `Post` records. Because the data schema has all the information about the shape of a `Post` record, the data tool will provide that information to the LLM so you don't have to. Also, the Amplify AI kit handles authorizing the tool use requests based on the caller's identity. This means if you have an owner-based model, the LLM will only be able to query the user's records.
73
95
74
-
*The only supported model operation currently is 'list'*
96
+
<Callouttype="info">
97
+
98
+
The only supported model operation is `'list'`.
99
+
100
+
</Callout>
75
101
76
102
## Query tools
77
103
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.
104
+
You can also give the LLM access to custom queries defined in your data schema. To do so, define a custom query with a [function or custom handler](https://docs.amplify.aws/react/build-a-backend/data/custom-business-logic/) and then reference that custom query as a tool. This requires that the custom query uses the `allow.authenticated()` authorization strategy.
79
105
80
106
```ts title="amplify/data/resource.ts"
81
-
// highlight-start
82
107
import { typeClientSchema, a, defineData, defineFunction } from"@aws-amplify/backend";
83
-
// highlight-end
84
108
85
-
// highlight-start
86
109
exportconst getWeather =defineFunction({
87
110
name: 'getWeather',
88
-
entry: 'getWeather.ts'
111
+
entry: './getWeather.ts',
112
+
environment: {
113
+
API_ENDPOINT: 'MY_API_ENDPOINT',
114
+
API_KEY: secret('MY_API_KEY'),
115
+
},
89
116
});
90
-
// highlight-end
91
117
92
118
const schema =a.schema({
93
-
// highlight-start
94
119
getWeather: a.query()
95
120
.arguments({ city: a.string() })
96
121
.returns(a.customType({
@@ -99,68 +124,73 @@ const schema = a.schema({
99
124
}))
100
125
.handler(a.handler.function(getWeather))
101
126
.authorization((allow) =>allow.authenticated()),
102
-
// highlight-end
103
127
104
128
chat: a.conversation({
105
129
aiModel: a.ai.model('Claude 3 Haiku'),
106
130
systemPrompt: 'You are a helpful assistant',
107
-
// highlight-start
108
131
tools: [
109
132
a.ai.dataTool({
110
-
name: 'getWeather',
133
+
// The name of the tool as it will be referenced in the LLM prompt
134
+
name: 'get_weather',
135
+
// The description of the tool provided to the LLM.
136
+
// Use this to help the LLM understand when to use the tool.
111
137
description: 'Gets the weather for a given city',
138
+
// A reference to the `a.query()` that the tool will invoke.
112
139
query: a.ref('getWeather'),
113
140
}),
114
141
]
115
-
// highlight-end
116
-
}),
142
+
})
143
+
.authorization((allow) =>allow.owner()),
117
144
});
118
145
```
119
146
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.
121
-
122
-
<Callout>
147
+
The Amplify data tool takes care of specifying the necessary input parameters to the LLM based on the query definition.
123
148
124
-
The description of the tool is very important to help the LLM know when to use that tool. The more descriptive you are about what the tool does, the better.
125
-
126
-
</Callout>
127
-
128
-
Here is an example Lambda function handler for our `getWeather` query:
149
+
Below is an illustrative example of a Lambda function handler for the `getWeather` query.
You can connect to any AWS service by defining a custom query and calling that service in the function handler. Then you will need to provide the Lambda with the proper permissions to call the AWS service.
193
+
You can connect to any AWS service by defining a custom query and calling that service in the function handler. To properly authorize the custom query function to call the AWS service, you will need to provide the Lambda with the proper permissions.
Conversation routes can also have completely custom tools defined in a Lambda handler.
220
+
You can also define a tool that executes in the conversation handler AWS Lambda function. This is useful if you want to define a tool that is not related to your data schema or that does simple tasks within the Lambda function runtime.
193
221
194
-
### Install the backend-ai package
222
+
First install the `@aws-amplify/backend-ai` package.
195
223
196
-
```bash
224
+
```bash title="Terminal"
197
225
npm install @aws-amplify/backend-ai
198
226
```
199
227
200
-
### Create a custom conversation handler function
228
+
Define a custom conversation handler function in your data schema and reference the function in the `handler` property of the `a.conversation()` definition.
201
229
202
230
```ts title="amplify/data/resource.ts"
203
231
import { typeClientSchema, a, defineData } from'@aws-amplify/backend';
Define the executable tool(s) and handler. Below is an illustrative example of a custom conversation handler function that defines a `calculator` tool.
Note that we throw an error in the `calculator` tool example above if the input is invalid. This error is surfaced to the LLM by the conversation handler function. Depending on the error message, the LLM may try to use the tool again with different input or completing its response with test for the user.
313
+
314
+
Lastly, update your backend definition to include the newly defined `chatHandler` function.
- Validate and sanitize any input from the LLM before using it in your application, e.g. don't use it directly in a database query or use `eval()` to execute it.
331
+
- Handle errors gracefully and provide meaningful error messages.
332
+
- Log and monitor tool usage to detect potential misuse or issues.
0 commit comments