forked from microsoft/semantic-kernel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBedrockAgentFixture.cs
148 lines (124 loc) · 6.21 KB
/
BedrockAgentFixture.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
// Copyright (c) Microsoft. All rights reserved.
using System;
using System.Threading.Tasks;
using Amazon.BedrockAgent;
using Amazon.BedrockAgent.Model;
using Amazon.BedrockAgentRuntime;
using Amazon.BedrockAgentRuntime.Model;
using Amazon.Runtime;
using Azure;
using Microsoft.Extensions.Configuration;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Agents;
using Microsoft.SemanticKernel.Agents.Bedrock;
using Microsoft.SemanticKernel.ChatCompletion;
using SemanticKernel.IntegrationTests.TestSettings;
using Xunit;
namespace SemanticKernel.IntegrationTests.Agents.CommonInterfaceConformance;
internal sealed class BedrockAgentFixture : AgentFixture, IAsyncDisposable
{
private readonly IConfigurationRoot _configuration = new ConfigurationBuilder()
.AddJsonFile(path: "testsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile(path: "testsettings.development.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables()
.AddUserSecrets<BedrockAgentTests>()
.Build();
private Amazon.BedrockAgent.Model.Agent? _bedrockAgent;
private BedrockAgent? _agent;
private BedrockAgentThread? _thread;
private BedrockAgentThread? _createdThread;
private BedrockAgentThread? _serviceFailingAgentThread;
private BedrockAgentThread? _createdServiceFailingAgentThread;
private AmazonBedrockAgentRuntimeClient? _serviceFailingAgentClient;
private readonly AmazonBedrockAgentClient _client = new();
private readonly AmazonBedrockAgentRuntimeClient _runtimeClient = new();
public override Microsoft.SemanticKernel.Agents.KernelAgent Agent => this._agent!;
public override AgentThread AgentThread => this._thread!;
public override AgentThread CreatedAgentThread => this._createdThread!;
public override AgentThread ServiceFailingAgentThread => this._serviceFailingAgentThread!;
public override AgentThread CreatedServiceFailingAgentThread => this._createdServiceFailingAgentThread!;
public override async Task DeleteThread(AgentThread thread)
{
await this._runtimeClient!.EndSessionAsync(new EndSessionRequest() { SessionIdentifier = thread.Id });
await this._runtimeClient.DeleteSessionAsync(new DeleteSessionRequest() { SessionIdentifier = thread.Id });
}
async ValueTask IAsyncDisposable.DisposeAsync()
{
await this.DisposeAsync();
}
public override async Task DisposeAsync()
{
if (this._thread!.Id is not null)
{
try
{
await this._runtimeClient!.EndSessionAsync(new EndSessionRequest() { SessionIdentifier = this._thread!.Id });
await this._runtimeClient!.DeleteSessionAsync(new DeleteSessionRequest() { SessionIdentifier = this._thread!.Id });
}
catch (RequestFailedException ex) when (ex.Status == 404)
{
}
}
if (this._createdThread!.Id is not null)
{
try
{
await this._runtimeClient!.EndSessionAsync(new EndSessionRequest() { SessionIdentifier = this._createdThread!.Id });
await this._runtimeClient!.DeleteSessionAsync(new DeleteSessionRequest() { SessionIdentifier = this._createdThread!.Id });
}
catch (RequestFailedException ex) when (ex.Status == 404)
{
}
}
if (this._createdServiceFailingAgentThread!.Id is not null)
{
try
{
await this._runtimeClient!.EndSessionAsync(new EndSessionRequest() { SessionIdentifier = this._createdServiceFailingAgentThread!.Id });
await this._runtimeClient!.DeleteSessionAsync(new DeleteSessionRequest() { SessionIdentifier = this._createdServiceFailingAgentThread!.Id });
}
catch (RequestFailedException ex) when (ex.Status == 404)
{
}
}
await this._client.DeleteAgentAsync(new DeleteAgentRequest() { AgentId = this._bedrockAgent!.AgentId });
this._serviceFailingAgentClient?.Dispose();
this._runtimeClient.Dispose();
this._client.Dispose();
}
public override Task<ChatHistory> GetChatHistory()
{
// The BedrockAgentThread cannot read messages from the thread. This is a limitation of Bedrock Sessions.
throw new NotImplementedException();
}
public override async Task InitializeAsync()
{
this._bedrockAgent = await this._client.CreateAndPrepareAgentAsync(this.GetCreateAgentRequest());
var kernelBuilder = Kernel.CreateBuilder();
Kernel kernel = kernelBuilder.Build();
this._agent = new BedrockAgent(this._bedrockAgent, this._client, this._runtimeClient) { Kernel = kernel };
this._thread = new BedrockAgentThread(this._runtimeClient);
this._createdThread = new BedrockAgentThread(this._runtimeClient);
await this._createdThread.CreateAsync();
this._serviceFailingAgentClient = new AmazonBedrockAgentRuntimeClient(new BasicAWSCredentials("", ""));
this._serviceFailingAgentThread = new BedrockAgentThread(this._serviceFailingAgentClient);
var createdFailingThreadResponse = await this._runtimeClient.CreateSessionAsync(new CreateSessionRequest(), default);
this._createdServiceFailingAgentThread = new BedrockAgentThread(this._serviceFailingAgentClient, createdFailingThreadResponse.SessionId);
}
private const string AgentName = "SKIntegrationTestAgent";
private const string AgentDescription = "A helpful assistant who helps users find information.";
private const string AgentInstruction = "You're a helpful assistant who helps users find information.";
private CreateAgentRequest GetCreateAgentRequest()
{
BedrockAgentConfiguration bedrockAgentSettings = this._configuration.GetSection("BedrockAgent").Get<BedrockAgentConfiguration>()!;
Assert.NotNull(bedrockAgentSettings);
return new()
{
AgentName = $"{AgentName}-{Guid.NewGuid():n}",
Description = AgentDescription,
Instruction = AgentInstruction,
AgentResourceRoleArn = bedrockAgentSettings.AgentResourceRoleArn,
FoundationModel = bedrockAgentSettings.FoundationModel,
};
}
}