forked from microsoft/semantic-kernel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAzureAIAgentFixture.cs
118 lines (96 loc) · 4.41 KB
/
AzureAIAgentFixture.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
// Copyright (c) Microsoft. All rights reserved.
using System.Threading.Tasks;
using Azure;
using Azure.Identity;
using Microsoft.Extensions.Configuration;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Agents;
using Microsoft.SemanticKernel.Agents.AzureAI;
using Microsoft.SemanticKernel.ChatCompletion;
using SemanticKernel.IntegrationTests.TestSettings;
using AAIP = Azure.AI.Projects;
namespace SemanticKernel.IntegrationTests.Agents.CommonInterfaceConformance;
public class AzureAIAgentFixture : AgentFixture
{
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<AzureAIAgentFixture>()
.Build();
private AAIP.AgentsClient? _agentsClient;
private AAIP.Agent? _aiAgent;
private AzureAIAgent? _agent;
private AzureAIAgentThread? _thread;
private AzureAIAgentThread? _createdThread;
private AzureAIAgentThread? _serviceFailingAgentThread;
private AzureAIAgentThread? _createdServiceFailingAgentThread;
public override 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<ChatHistory> GetChatHistory()
{
var chatHistory = new ChatHistory();
await foreach (var existingMessage in this._thread!.GetMessagesAsync(AAIP.ListSortOrder.Ascending).ConfigureAwait(false))
{
chatHistory.Add(existingMessage);
}
return chatHistory;
}
public override Task DeleteThread(AgentThread thread)
{
return this._agentsClient!.DeleteThreadAsync(thread.Id);
}
public override async Task DisposeAsync()
{
if (this._thread!.Id is not null)
{
try
{
await this._agentsClient!.DeleteThreadAsync(this._thread!.Id);
}
catch (RequestFailedException ex) when (ex.Status == 404)
{
}
}
try
{
await this._agentsClient!.DeleteThreadAsync(this._createdThread!.Id);
}
catch (RequestFailedException ex) when (ex.Status == 404)
{
}
try
{
await this._agentsClient!.DeleteThreadAsync(this._createdServiceFailingAgentThread!.Id);
}
catch (RequestFailedException ex) when (ex.Status == 404)
{
}
await this._agentsClient!.DeleteAgentAsync(this._aiAgent!.Id);
}
public override async Task InitializeAsync()
{
AzureAIConfiguration configuration = this._configuration.GetSection("AzureAI").Get<AzureAIConfiguration>()!;
var client = AzureAIAgent.CreateAzureAIClient(configuration.ConnectionString!, new AzureCliCredential());
this._agentsClient = client.GetAgentsClient();
this._aiAgent =
await this._agentsClient.CreateAgentAsync(
configuration.ChatModelId,
name: "HelpfulAssistant",
description: "Helpful Assistant",
instructions: "You are a helpful assistant.");
var kernelBuilder = Kernel.CreateBuilder();
Kernel kernel = kernelBuilder.Build();
this._agent = new AzureAIAgent(this._aiAgent, this._agentsClient) { Kernel = kernel };
this._thread = new AzureAIAgentThread(this._agentsClient);
this._createdThread = new AzureAIAgentThread(this._agentsClient);
await this._createdThread.CreateAsync();
var serviceFailingClient = AzureAIAgent.CreateAzureAIClient("swedencentral.api.azureml.ms;<subscription_id>;<resource_group_name>;<project_name>", new AzureCliCredential());
this._serviceFailingAgentThread = new AzureAIAgentThread(serviceFailingClient.GetAgentsClient());
var createdFailingThreadResponse = await this._agentsClient.CreateThreadAsync();
this._createdServiceFailingAgentThread = new AzureAIAgentThread(serviceFailingClient.GetAgentsClient(), createdFailingThreadResponse.Value.Id);
}
}