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
You must create an Amazon Bedrock Agent with at least one action group. Each action group can contain up to 5 tools, which in turn need to match the ones defined in your Lambda function. Bedrock must have permission to invoke your Lambda function.
Description: 'Get the airport code for a given city'
134
+
Parameters:
135
+
city:
136
+
Type: string
137
+
Description: 'The name of the city to get the airport code for'
138
+
Required: true
139
+
```
73
140
74
141
## Basic Usage
75
142
76
143
To create an agent, use the `BedrockAgentFunctionResolver` to register your tools and handle the requests. The resolver will automatically parse the request, route it to the appropriate function, and return a well-formed response that includes the tool's output and any existing session attributes.
When the Bedrock Agent invokes your Lambda function with a request to use the "GetWeather" tool and a parameter for "city", the resolver automatically extracts the parameter, passes it to your function, and formats the response.
113
210
211
+
## How It Works with Amazon Bedrock Agents
212
+
213
+
1. When a user interacts with a Bedrock Agent, the agent identifies when it needs to call an action to fulfill the user's request.
214
+
2. The agent determines which function to call and what parameters are needed.
215
+
3. Bedrock sends a request to your Lambda function with the function name and parameters.
216
+
4. The BedrockAgentFunctionResolver automatically:
217
+
- Finds the registered handler for the requested function
218
+
- Extracts and converts parameters to the correct types
219
+
- Invokes your handler with the parameters
220
+
- Formats the response in the way Bedrock Agents expect
221
+
5. The agent receives the response and uses it to continue the conversation with the user
222
+
114
223
## Advanced Usage
115
224
116
-
### Functions with Descriptions
225
+
### Custom type serialization
117
226
118
-
Add descriptive information to your tool functions:
227
+
You can have your own custom types as arguments to the tool function. The library will automatically handle serialization and deserialization of these types. In this case, you need to ensure that your custom type is serializable to JSON, if serialization fails, the object will be null.
119
228
120
-
```csharp
121
-
_resolver.Tool(
122
-
"CheckInventory",
123
-
"Checks if a product is available in inventory",
124
-
(stringproductId, boolcheckWarehouse) =>
229
+
```csharp hl_lines="4"
230
+
resolver.Tool(
231
+
name: "PriceCalculator",
232
+
description: "Calculate total price with tax",
233
+
handler: (MyCustomTypemyCustomType) =>
125
234
{
126
-
returncheckWarehouse
127
-
?$"Product {productId} has 15 units in warehouse"
128
-
:$"Product {productId} has 5 units in store";
129
-
});
235
+
varwithTax=myCustomType.Price*1.2m;
236
+
return$"Total price with tax: {withTax.ToString("F2", CultureInfo.InvariantCulture)}";
237
+
}
238
+
);
239
+
```
240
+
241
+
### Custom type serialization native AOT
242
+
243
+
For native AOT compilation, you can use JsonSerializerContext and pass it to `BedrockAgentFunctionResolver`. This allows the library to generate the necessary serialization code at compile time, ensuring compatibility with AOT.
When Bedrock Agents invoke your Lambda function, it can pass session attributes that you can use to store information across multiple interactions with the user. You can access these attributes in your handler function and modify them as needed.
@@ -254,7 +386,7 @@ Access the raw Bedrock Agent request:
254
386
_resolver.Tool(
255
387
"ProcessRawRequest",
256
388
"Processes the raw Bedrock Agent request",
257
-
(ActionGroupInvocationInputinput) =>
389
+
(BedrockFunctionRequestinput) =>
258
390
{
259
391
varfunctionName=input.Function;
260
392
varparameterCount=input.Parameters.Count;
@@ -288,30 +420,6 @@ resolver.Tool(
288
420
});
289
421
```
290
422
291
-
## How It Works with Amazon Bedrock Agents
292
-
293
-
1. When a user interacts with a Bedrock Agent, the agent identifies when it needs to call an action to fulfill the user's request.
294
-
2. The agent determines which function to call and what parameters are needed.
295
-
3. Bedrock sends a request to your Lambda function with the function name and parameters.
296
-
4. The BedrockAgentFunctionResolver automatically:
297
-
- Finds the registered handler for the requested function
298
-
- Extracts and converts parameters to the correct types
299
-
- Invokes your handler with the parameters
300
-
- Formats the response in the way Bedrock Agents expect
301
-
5. The agent receives the response and uses it to continue the conversation with the user
302
-
303
-
## Supported Parameter Types
304
-
305
-
-`string`
306
-
-`int`
307
-
-`number`
308
-
-`bool`
309
-
-`enum` types
310
-
-`ILambdaContext` (for accessing Lambda context)
311
-
-`ActionGroupInvocationInput` (for accessing raw request)
312
-
- Any service registered in dependency injection
313
-
314
-
315
423
## Using Attributes to Define Tools
316
424
317
425
You can define Bedrock Agent functions using attributes instead of explicit registration. This approach provides a clean, declarative way to organize your tools into classes:
@@ -361,6 +469,9 @@ var resolver = serviceProvider.GetRequiredService<BedrockAgentFunctionResolver>(
361
469
362
470
## Complete Example with Dependency Injection
363
471
472
+
You can find examples in the [Powertools for AWS Lambda (.NET) GitHub repository](https://github.com/aws-powertools/powertools-lambda-dotnet/tree/develop/examples/Event%20Handler/BedrockAgentFunction).
0 commit comments