forked from microsoft/semantic-kernel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBedrockAgent.cs
729 lines (641 loc) · 33.8 KB
/
BedrockAgent.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
// Copyright (c) Microsoft. All rights reserved.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using Amazon.BedrockAgent;
using Amazon.BedrockAgentRuntime;
using Amazon.BedrockAgentRuntime.Model;
using Microsoft.SemanticKernel.Agents.Extensions;
using Microsoft.SemanticKernel.ChatCompletion;
using Microsoft.SemanticKernel.Diagnostics;
namespace Microsoft.SemanticKernel.Agents.Bedrock;
/// <summary>
/// Provides a specialized <see cref="Agent"/> for the Bedrock Agent service.
/// </summary>
public sealed class BedrockAgent : Agent
{
/// <summary>
/// The client used to interact with the Bedrock Agent service.
/// </summary>
public AmazonBedrockAgentClient Client { get; }
/// <summary>
/// The client used to interact with the Bedrock Agent runtime service.
/// </summary>
public AmazonBedrockAgentRuntimeClient RuntimeClient { get; }
internal readonly Amazon.BedrockAgent.Model.Agent AgentModel;
/// <summary>
/// There is a default alias created by Bedrock for the working draft version of the agent.
/// https://docs.aws.amazon.com/bedrock/latest/userguide/agents-deploy.html
/// </summary>
public static readonly string WorkingDraftAgentAlias = "TSTALIASID";
/// <summary>
/// Initializes a new instance of the <see cref="BedrockAgent"/> class.
/// Unlike other types of agents in Semantic Kernel, prompt templates are not supported for Bedrock agents,
/// since Bedrock agents don't support using an alternative instruction in runtime.
/// </summary>
/// <param name="agentModel">The agent model of an agent that exists on the Bedrock Agent service.</param>
/// <param name="client">A client used to interact with the Bedrock Agent service.</param>
/// <param name="runtimeClient">A client used to interact with the Bedrock Agent runtime service.</param>
public BedrockAgent(
Amazon.BedrockAgent.Model.Agent agentModel,
AmazonBedrockAgentClient client,
AmazonBedrockAgentRuntimeClient runtimeClient)
{
this.AgentModel = agentModel;
this.Client = client;
this.RuntimeClient = runtimeClient;
this.Id = agentModel.AgentId;
this.Name = agentModel.AgentName;
this.Description = agentModel.Description;
this.Instructions = agentModel.Instruction;
}
#region static methods
/// <summary>
/// Convenient method to create an unique session id.
/// </summary>
public static string CreateSessionId()
{
return Guid.NewGuid().ToString();
}
#endregion
#region public methods
#region InvokeAsync
/// <summary>
/// Invoke the agent with the provided message and arguments.
/// </summary>
/// <param name="messages">The messages to pass to the agent.</param>
/// <param name="thread">The conversation thread to continue with this invocation. If not provided, creates a new thread.</param>
/// <param name="options">Optional instance of <see cref="BedrockAgentInvokeOptions"/> for agent invocation.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
/// <returns>An async list of response items that each contain a <see cref="ChatMessageContent"/> and an <see cref="AgentThread"/>.</returns>
/// <remarks>
/// To continue this thread in the future, use an <see cref="AgentThread"/> returned in one of the response items.
/// </remarks>
public IAsyncEnumerable<AgentResponseItem<ChatMessageContent>> InvokeAsync(
ICollection<ChatMessageContent> messages,
AgentThread? thread = null,
BedrockAgentInvokeOptions? options = null,
CancellationToken cancellationToken = default)
{
return this.InvokeAsync(messages, thread, options as AgentInvokeOptions, cancellationToken);
}
/// <inheritdoc/>
public override async IAsyncEnumerable<AgentResponseItem<ChatMessageContent>> InvokeAsync(
ICollection<ChatMessageContent> messages,
AgentThread? thread = null,
AgentInvokeOptions? options = null,
[EnumeratorCancellation] CancellationToken cancellationToken = default)
{
Verify.NotNull(messages, nameof(messages));
if (messages.Count == 0)
{
throw new InvalidOperationException("The Bedrock agent requires a message to be invoked.");
}
// Create a thread if needed
var bedrockThread = await this.EnsureThreadExistsWithMessagesAsync(
messages,
thread,
() => new BedrockAgentThread(this.RuntimeClient),
cancellationToken).ConfigureAwait(false);
// Ensure that the last message provided is a user message
string? message = this.ExtractUserMessage(messages.Last());
// Build session state with conversation history if needed
SessionState sessionState = this.ExtractSessionState(messages);
// Configure the agent request with the provided options
var invokeAgentRequest = this.ConfigureAgentRequest(options, () =>
{
return new InvokeAgentRequest
{
SessionState = sessionState,
AgentId = this.Id,
SessionId = bedrockThread.Id,
InputText = message,
};
});
// Invoke the agent
var invokeResults = this.InvokeInternalAsync(invokeAgentRequest, options?.KernelArguments, cancellationToken);
// Return the results to the caller in AgentResponseItems.
await foreach (var result in invokeResults.ConfigureAwait(false))
{
await this.NotifyThreadOfNewMessage(bedrockThread, result, cancellationToken).ConfigureAwait(false);
yield return new(result, bedrockThread);
}
}
/// <summary>
/// Invoke the Bedrock agent with the given request. Use this method when you want to customize the request.
/// The provided thread is used to continue the conversation. If the thread is not provided and the session id is provided,
/// a new thread is created with the provided session id. If neither is provided, a new thread is created.
/// </summary>
/// <param name="invokeAgentRequest">The request to send to the agent.</param>
/// <param name="thread">The conversation thread to continue with this invocation. If not provided, creates a new thread.</param>
/// <param name="options">Optional parameter of type <see cref="BedrockAgentInvokeOptions"/> for agent invocation.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
public IAsyncEnumerable<AgentResponseItem<ChatMessageContent>> InvokeAsync(
InvokeAgentRequest invokeAgentRequest,
AgentThread? thread = null,
BedrockAgentInvokeOptions? options = null,
CancellationToken cancellationToken = default)
{
return this.InvokeAsync(invokeAgentRequest, thread, options as AgentInvokeOptions, cancellationToken);
}
/// <summary>
/// Invoke the Bedrock agent with the given request. Use this method when you want to customize the request.
/// The provided thread is used to continue the conversation. If the thread is not provided and the session id is provided,
/// a new thread is created with the provided session id. If neither is provided, a new thread is created.
/// </summary>
/// <param name="invokeAgentRequest">The request to send to the agent.</param>
/// <param name="thread">The conversation thread to continue with this invocation. If not provided, creates a new thread.</param>
/// <param name="options">Optional parameters for agent invocation.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
public async IAsyncEnumerable<AgentResponseItem<ChatMessageContent>> InvokeAsync(
InvokeAgentRequest invokeAgentRequest,
AgentThread? thread = null,
AgentInvokeOptions? options = null,
[EnumeratorCancellation] CancellationToken cancellationToken = default)
{
// The provided thread is used to continue the conversation. If the thread is not provided and the session id is provided,
// a new thread is created with the provided session id. If neither is provided, a new thread is created.
if (thread is null && invokeAgentRequest.SessionId is not null)
{
thread = new BedrockAgentThread(this.RuntimeClient, invokeAgentRequest.SessionId);
}
var bedrockThread = await this.EnsureThreadExistsWithMessagesAsync(
[],
thread,
() => new BedrockAgentThread(this.RuntimeClient),
cancellationToken).ConfigureAwait(false);
// Configure the agent request with the provided options
invokeAgentRequest.SessionId = bedrockThread.Id;
invokeAgentRequest = this.ConfigureAgentRequest(options, () => invokeAgentRequest);
// Invoke the agent
var invokeResults = this.InvokeInternalAsync(invokeAgentRequest, options?.KernelArguments, cancellationToken);
// Return the results to the caller in AgentResponseItems.
await foreach (var result in invokeResults.ConfigureAwait(false))
{
await this.NotifyThreadOfNewMessage(bedrockThread, result, cancellationToken).ConfigureAwait(false);
yield return new(result, bedrockThread);
}
}
#region Obsolete
/// <summary>
/// Invoke the Bedrock agent with the given request. Use this method when you want to customize the request.
/// </summary>
/// <param name="invokeAgentRequest">The request to send to the agent.</param>
/// <param name="arguments">The arguments to use when invoking the agent.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
[Obsolete("Use InvokeAsync with AgentThread instead.")]
public IAsyncEnumerable<ChatMessageContent> InvokeAsync(
InvokeAgentRequest invokeAgentRequest,
KernelArguments? arguments,
CancellationToken cancellationToken = default)
{
return invokeAgentRequest.StreamingConfigurations != null && (invokeAgentRequest.StreamingConfigurations.StreamFinalResponse ?? false)
? throw new ArgumentException("The streaming configuration must be null for non-streaming responses.")
: ActivityExtensions.RunWithActivityAsync(
() => ModelDiagnostics.StartAgentInvocationActivity(this.Id, this.GetDisplayName(), this.Description),
InvokeInternal,
cancellationToken);
// Collect all responses from the agent and return them as a single chat message content since this
// is a non-streaming API.
// The Bedrock Agent API streams beck different types of responses, i.e. text, files, metadata, etc.
// The Bedrock Agent API also won't stream back any content when it needs to call a function. It will
// only start streaming back content after the function has been called and the response is ready.
async IAsyncEnumerable<ChatMessageContent> InvokeInternal()
{
ChatMessageContentItemCollection items = [];
string content = "";
Dictionary<string, object?> metadata = [];
List<object?> innerContents = [];
await foreach (var message in this.InternalInvokeAsync(invokeAgentRequest, arguments, cancellationToken).ConfigureAwait(false))
{
items.AddRange(message.Items);
content += message.Content ?? "";
if (message.Metadata != null)
{
foreach (var key in message.Metadata.Keys)
{
metadata[key] = message.Metadata[key];
}
}
innerContents.Add(message.InnerContent);
}
if (content.Length == 0)
{
throw new KernelException("No content was returned from the agent.");
}
var chatMessageContent = new ChatMessageContent(AuthorRole.Assistant, content)
{
AuthorName = this.GetDisplayName(),
Items = items,
ModelId = this.AgentModel.FoundationModel,
Metadata = metadata,
InnerContent = innerContents,
};
yield return chatMessageContent;
}
}
/// <summary>
/// Invoke the Bedrock agent with the given message.
/// </summary>
/// <param name="sessionId">The session id.</param>
/// <param name="message">The message to send to the agent.</param>
/// <param name="arguments">The arguments to use when invoking the agent.</param>
/// <param name="agentAliasId">The alias id of the agent to use. The default is the working draft alias id.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
/// <returns>An <see cref="IAsyncEnumerable{T}"/> of <see cref="ChatMessageContent"/>.</returns>
[Obsolete("Use InvokeAsync with AgentThread instead.")]
public IAsyncEnumerable<ChatMessageContent> InvokeAsync(
string sessionId,
string message,
KernelArguments? arguments,
string? agentAliasId = null,
CancellationToken cancellationToken = default)
{
var invokeAgentRequest = new InvokeAgentRequest
{
AgentAliasId = agentAliasId ?? WorkingDraftAgentAlias,
AgentId = this.Id,
SessionId = sessionId,
InputText = message,
};
return this.InvokeAsync(invokeAgentRequest, arguments, cancellationToken);
}
#endregion
#endregion
#region InvokeStreamingAsync
/// <summary>
/// Invoke the agent with the provided message and arguments.
/// </summary>
/// <param name="messages">The messages to pass to the agent.</param>
/// <param name="thread">The conversation thread to continue with this invocation. If not provided, creates a new thread.</param>
/// <param name="options">Optional parameters of type <see cref="BedrockAgentInvokeOptions"/> for agent invocation.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
/// <returns>An async list of response items that each contain a <see cref="ChatMessageContent"/> and an <see cref="AgentThread"/>.</returns>
/// <remarks>
/// To continue this thread in the future, use an <see cref="AgentThread"/> returned in one of the response items.
/// </remarks>
public IAsyncEnumerable<AgentResponseItem<StreamingChatMessageContent>> InvokeStreamingAsync(
ICollection<ChatMessageContent> messages,
AgentThread? thread = null,
BedrockAgentInvokeOptions? options = null,
CancellationToken cancellationToken = default)
{
return this.InvokeStreamingAsync(messages, thread, options as AgentInvokeOptions, cancellationToken);
}
/// <inheritdoc/>
public override async IAsyncEnumerable<AgentResponseItem<StreamingChatMessageContent>> InvokeStreamingAsync(
ICollection<ChatMessageContent> messages,
AgentThread? thread = null,
AgentInvokeOptions? options = null,
[EnumeratorCancellation] CancellationToken cancellationToken = default)
{
Verify.NotNull(messages, nameof(messages));
if (messages.Count == 0)
{
throw new InvalidOperationException("The Bedrock agent requires a message to be invoked.");
}
// Create a thread if needed
var bedrockThread = await this.EnsureThreadExistsWithMessagesAsync(
messages,
thread,
() => new BedrockAgentThread(this.RuntimeClient),
cancellationToken).ConfigureAwait(false);
// Ensure that the last message provided is a user message
string? message = this.ExtractUserMessage(messages.Last());
// Build session state with conversation history if needed
SessionState sessionState = this.ExtractSessionState(messages);
// Configure the agent request with the provided options
var invokeAgentRequest = this.ConfigureAgentRequest(options, () =>
{
return new InvokeAgentRequest
{
SessionState = sessionState,
AgentId = this.Id,
SessionId = bedrockThread.Id,
InputText = message,
};
});
// Invoke the agent
var invokeResults = this.InvokeStreamingInternalAsync(invokeAgentRequest, bedrockThread, options?.KernelArguments, cancellationToken);
// Return the results to the caller in AgentResponseItems.
await foreach (var result in invokeResults.ConfigureAwait(false))
{
yield return new(result, bedrockThread);
}
}
/// <summary>
/// Invoke the Bedrock agent with the given request. Use this method when you want to customize the request.
/// The provided thread is used to continue the conversation. If the thread is not provided and the session id is provided,
/// a new thread is created with the provided session id. If neither is provided, a new thread is created.
/// </summary>
/// <param name="invokeAgentRequest">The request to send to the agent.</param>
/// <param name="thread">The conversation thread to continue with this invocation. If not provided, creates a new thread.</param>
/// <param name="options">Optional parameters of type <see cref="BedrockAgentInvokeOptions"/> for agent invocation.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
/// <returns>An <see cref="IAsyncEnumerable{T}"/> of <see cref="StreamingChatMessageContent"/>.</returns>
public IAsyncEnumerable<AgentResponseItem<StreamingChatMessageContent>> InvokeStreamingAsync(
InvokeAgentRequest invokeAgentRequest,
AgentThread? thread = null,
BedrockAgentInvokeOptions? options = null,
CancellationToken cancellationToken = default)
{
return this.InvokeStreamingAsync(invokeAgentRequest, thread, options as AgentInvokeOptions, cancellationToken);
}
/// <summary>
/// Invoke the Bedrock agent with the given request. Use this method when you want to customize the request.
/// The provided thread is used to continue the conversation. If the thread is not provided and the session id is provided,
/// a new thread is created with the provided session id. If neither is provided, a new thread is created.
/// </summary>
/// <param name="invokeAgentRequest">The request to send to the agent.</param>
/// <param name="thread">The conversation thread to continue with this invocation. If not provided, creates a new thread.</param>
/// <param name="options">Optional parameters for agent invocation.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
/// <returns>An <see cref="IAsyncEnumerable{T}"/> of <see cref="StreamingChatMessageContent"/>.</returns>
public async IAsyncEnumerable<AgentResponseItem<StreamingChatMessageContent>> InvokeStreamingAsync(
InvokeAgentRequest invokeAgentRequest,
AgentThread? thread = null,
AgentInvokeOptions? options = null,
[EnumeratorCancellation] CancellationToken cancellationToken = default)
{
// The provided thread is used to continue the conversation. If the thread is not provided and the session id is provided,
// a new thread is created with the provided session id. If neither is provided, a new thread is created.
if (thread is null && invokeAgentRequest.SessionId is not null)
{
thread = new BedrockAgentThread(this.RuntimeClient, invokeAgentRequest.SessionId);
}
var bedrockThread = await this.EnsureThreadExistsWithMessagesAsync(
[],
thread,
() => new BedrockAgentThread(this.RuntimeClient),
cancellationToken).ConfigureAwait(false);
// Configure the agent request with the provided options
invokeAgentRequest.SessionId = bedrockThread.Id;
invokeAgentRequest = this.ConfigureAgentRequest(options, () => invokeAgentRequest);
var invokeResults = this.InvokeStreamingInternalAsync(invokeAgentRequest, bedrockThread, options?.KernelArguments, cancellationToken);
// The Bedrock agent service has the same API for both streaming and non-streaming responses.
// We are invoking the same method as the non-streaming response with the streaming configuration set,
// and converting the chat message content to streaming chat message content.
await foreach (StreamingChatMessageContent chatMessageContent in invokeResults.ConfigureAwait(false))
{
yield return new(
message: new StreamingChatMessageContent(chatMessageContent.Role, chatMessageContent.Content)
{
AuthorName = chatMessageContent.AuthorName,
ModelId = chatMessageContent.ModelId,
InnerContent = chatMessageContent.InnerContent,
Metadata = chatMessageContent.Metadata,
},
thread: bedrockThread);
}
}
#region Obsolete
/// <summary>
/// Invoke the Bedrock agent with the given request and streaming response.
/// </summary>
/// <param name="sessionId">The session id.</param>
/// <param name="message">The message to send to the agent.</param>
/// <param name="arguments">The arguments to use when invoking the agent.</param>
/// <param name="agentAliasId">The alias id of the agent to use. The default is the working draft alias id.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
/// <returns>An <see cref="IAsyncEnumerable{T}"/> of <see cref="ChatMessageContent"/>.</returns>
[Obsolete("Use InvokeStreamingAsync with AgentThread instead.")]
public IAsyncEnumerable<StreamingChatMessageContent> InvokeStreamingAsync(
string sessionId,
string message,
KernelArguments? arguments,
string? agentAliasId = null,
CancellationToken cancellationToken = default)
{
var invokeAgentRequest = new InvokeAgentRequest
{
AgentAliasId = agentAliasId ?? WorkingDraftAgentAlias,
AgentId = this.Id,
SessionId = sessionId,
InputText = message,
StreamingConfigurations = new()
{
StreamFinalResponse = true,
},
};
return this.InvokeStreamingAsync(invokeAgentRequest, arguments, cancellationToken);
}
/// <summary>
/// Invoke the Bedrock agent with the given request and streaming response. Use this method when you want to customize the request.
/// </summary>
/// <param name="invokeAgentRequest">The request to send to the agent.</param>
/// <param name="arguments">The arguments to use when invoking the agent.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
/// <returns>An <see cref="IAsyncEnumerable{T}"/> of <see cref="StreamingChatMessageContent"/>.</returns>
[Obsolete("Use InvokeStreamingAsync with AgentThread instead.")]
public IAsyncEnumerable<StreamingChatMessageContent> InvokeStreamingAsync(
InvokeAgentRequest invokeAgentRequest,
KernelArguments? arguments,
CancellationToken cancellationToken = default)
{
if (invokeAgentRequest.StreamingConfigurations == null)
{
invokeAgentRequest.StreamingConfigurations = new()
{
StreamFinalResponse = true,
};
}
else if (!(invokeAgentRequest.StreamingConfigurations.StreamFinalResponse ?? false))
{
throw new ArgumentException("The streaming configuration must have StreamFinalResponse set to true.");
}
return ActivityExtensions.RunWithActivityAsync(
() => ModelDiagnostics.StartAgentInvocationActivity(this.Id, this.GetDisplayName(), this.Description),
InvokeInternal,
cancellationToken);
async IAsyncEnumerable<StreamingChatMessageContent> InvokeInternal()
{
// The Bedrock agent service has the same API for both streaming and non-streaming responses.
// We are invoking the same method as the non-streaming response with the streaming configuration set,
// and converting the chat message content to streaming chat message content.
await foreach (var chatMessageContent in this.InternalInvokeAsync(invokeAgentRequest, arguments, cancellationToken).ConfigureAwait(false))
{
yield return new StreamingChatMessageContent(chatMessageContent.Role, chatMessageContent.Content)
{
AuthorName = chatMessageContent.AuthorName,
ModelId = chatMessageContent.ModelId,
InnerContent = chatMessageContent.InnerContent,
Metadata = chatMessageContent.Metadata,
};
}
}
}
#endregion
#endregion
#endregion
/// <inheritdoc/>
protected override IEnumerable<string> GetChannelKeys()
{
// Return the channel keys for the BedrockAgent
yield return typeof(BedrockAgentChannel).FullName!;
}
/// <inheritdoc/>
protected override Task<AgentChannel> CreateChannelAsync(CancellationToken cancellationToken)
{
// Create and return a new BedrockAgentChannel
return Task.FromResult<AgentChannel>(new BedrockAgentChannel());
}
/// <inheritdoc/>
protected override Task<AgentChannel> RestoreChannelAsync(string channelState, CancellationToken cancellationToken)
{
// Restore and return a BedrockAgentChannel from the given state
return Task.FromResult<AgentChannel>(new BedrockAgentChannel());
}
#region internal methods
internal string CodeInterpreterActionGroupSignature { get => $"{this.GetDisplayName()}_CodeInterpreter"; }
internal string KernelFunctionActionGroupSignature { get => $"{this.GetDisplayName()}_KernelFunctions"; }
internal string UseInputActionGroupSignature { get => $"{this.GetDisplayName()}_UserInput"; }
#endregion
#region private methods
private IAsyncEnumerable<ChatMessageContent> InvokeInternalAsync(
InvokeAgentRequest invokeAgentRequest,
KernelArguments? arguments,
CancellationToken cancellationToken = default)
{
return invokeAgentRequest.StreamingConfigurations != null && (invokeAgentRequest.StreamingConfigurations.StreamFinalResponse ?? false)
? throw new ArgumentException("The streaming configuration must be null for non-streaming responses.")
: ActivityExtensions.RunWithActivityAsync(
() => ModelDiagnostics.StartAgentInvocationActivity(this.Id, this.GetDisplayName(), this.Description),
InvokeInternal,
cancellationToken);
// Collect all responses from the agent and return them as a single chat message content since this
// is a non-streaming API.
// The Bedrock Agent API streams beck different types of responses, i.e. text, files, metadata, etc.
// The Bedrock Agent API also won't stream back any content when it needs to call a function. It will
// only start streaming back content after the function has been called and the response is ready.
async IAsyncEnumerable<ChatMessageContent> InvokeInternal()
{
ChatMessageContentItemCollection items = [];
string content = "";
Dictionary<string, object?> metadata = [];
List<object?> innerContents = [];
await foreach (var message in this.InternalInvokeAsync(invokeAgentRequest, arguments, cancellationToken).ConfigureAwait(false))
{
items.AddRange(message.Items);
content += message.Content ?? "";
if (message.Metadata != null)
{
foreach (var key in message.Metadata.Keys)
{
metadata[key] = message.Metadata[key];
}
}
innerContents.Add(message.InnerContent);
}
var chatMessageContent = new ChatMessageContent(AuthorRole.Assistant, content)
{
AuthorName = this.GetDisplayName(),
Items = items,
ModelId = this.AgentModel.FoundationModel,
Metadata = metadata,
InnerContent = innerContents,
};
yield return chatMessageContent;
}
}
private IAsyncEnumerable<StreamingChatMessageContent> InvokeStreamingInternalAsync(
InvokeAgentRequest invokeAgentRequest,
AgentThread thread,
KernelArguments? arguments,
CancellationToken cancellationToken = default)
{
if (invokeAgentRequest.StreamingConfigurations == null)
{
invokeAgentRequest.StreamingConfigurations = new()
{
StreamFinalResponse = true,
};
}
else if (!(invokeAgentRequest.StreamingConfigurations.StreamFinalResponse ?? false))
{
throw new ArgumentException("The streaming configuration must have StreamFinalResponse set to true.");
}
return ActivityExtensions.RunWithActivityAsync(
() => ModelDiagnostics.StartAgentInvocationActivity(this.Id, this.GetDisplayName(), this.Description),
InvokeInternal,
cancellationToken);
async IAsyncEnumerable<StreamingChatMessageContent> InvokeInternal()
{
// The Bedrock agent service has the same API for both streaming and non-streaming responses.
// We are invoking the same method as the non-streaming response with the streaming configuration set,
// and converting the chat message content to streaming chat message content.
await foreach (var chatMessageContent in this.InternalInvokeAsync(invokeAgentRequest, arguments, cancellationToken).ConfigureAwait(false))
{
await this.NotifyThreadOfNewMessage(thread, chatMessageContent, cancellationToken).ConfigureAwait(false);
yield return new StreamingChatMessageContent(chatMessageContent.Role, chatMessageContent.Content)
{
AuthorName = chatMessageContent.AuthorName,
ModelId = chatMessageContent.ModelId,
InnerContent = chatMessageContent.InnerContent,
Metadata = chatMessageContent.Metadata,
};
}
}
}
private InvokeAgentRequest ConfigureAgentRequest(AgentInvokeOptions? options, Func<InvokeAgentRequest> createRequest)
{
string agentAlias = WorkingDraftAgentAlias;
bool enableTrace = false;
if (options is BedrockAgentInvokeOptions bedrockOption)
{
agentAlias = bedrockOption.AgentAliasId ?? WorkingDraftAgentAlias;
enableTrace = bedrockOption.EnableTrace;
}
var invokeRequest = createRequest();
invokeRequest.AgentAliasId = agentAlias;
invokeRequest.EnableTrace = enableTrace;
return invokeRequest;
}
private string ExtractUserMessage(ChatMessageContent chatMessageContent)
{
if (!chatMessageContent.Role.Equals(AuthorRole.User))
{
throw new InvalidOperationException("Bedrock agents must be invoked with a user message");
}
return chatMessageContent.Content ?? "";
}
private SessionState ExtractSessionState(ICollection<ChatMessageContent> messages)
{
// If there is more than one message provided, add all but the last message to the session state
SessionState sessionState = new();
if (messages.Count > 1)
{
List<Amazon.BedrockAgentRuntime.Model.Message> messageHistory = [];
for (int i = 0; i < messages.Count - 1; i++)
{
var currentMessage = messages.ElementAt(i);
messageHistory.Add(this.ToBedrockMessage(currentMessage));
}
sessionState.ConversationHistory = new ConversationHistory() { Messages = messageHistory };
}
return sessionState;
}
private Amazon.BedrockAgentRuntime.Model.Message ToBedrockMessage(ChatMessageContent chatMessageContent)
{
return new Amazon.BedrockAgentRuntime.Model.Message()
{
Role = this.MapBedrockAgentUser(chatMessageContent.Role),
Content = [new() { Text = chatMessageContent.Content }]
};
}
private Amazon.BedrockAgentRuntime.ConversationRole MapBedrockAgentUser(AuthorRole authorRole)
{
if (authorRole == AuthorRole.User)
{
return Amazon.BedrockAgentRuntime.ConversationRole.User;
}
if (authorRole == AuthorRole.Assistant)
{
return Amazon.BedrockAgentRuntime.ConversationRole.Assistant;
}
throw new ArgumentOutOfRangeException($"Invalid role: {authorRole}");
}
#endregion
}