Skip to content

Commit c2edaf1

Browse files
Address code review feedback
1 parent 9a9c992 commit c2edaf1

File tree

13 files changed

+112
-91
lines changed

13 files changed

+112
-91
lines changed
File renamed without changes.

dotnet/samples/Concepts/FunctionCalling/FunctionCalling.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -537,10 +537,7 @@ private static Kernel CreateKernel(ITestOutputHelper? output = null)
537537
// Create a logging handler to output HTTP requests and responses
538538
if (output is not null)
539539
{
540-
var handler = new LoggingHandler(new HttpClientHandler(), output);
541-
var httpClient = new HttpClient(handler);
542-
543-
builder.AddOpenAIChatCompletion(TestConfiguration.OpenAI.ChatModelId, TestConfiguration.OpenAI.ApiKey, httpClient: httpClient);
540+
builder.AddOpenAIChatCompletion(TestConfiguration.OpenAI.ChatModelId, TestConfiguration.OpenAI.ApiKey);
544541
}
545542
else
546543
{

dotnet/samples/GettingStartedWithAgents/AzureAIAgent/Step08_AzureAIAgent_Declarative.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,9 @@ namespace GettingStarted.AzureAgents;
1717
/// </summary>
1818
public class Step08_AzureAIAgent_Declarative : BaseAzureAgentTest
1919
{
20-
public Step08_AzureAIAgent_Declarative(ITestOutputHelper output) : base(output)
21-
{
22-
var builder = Kernel.CreateBuilder();
23-
builder.Services.AddSingleton<AIProjectClient>(this.Client);
24-
this._kernel = builder.Build();
25-
}
26-
20+
/// <summary>
21+
/// Demonstrates creating and using a Chat Completion Agent with a Kernel.
22+
/// </summary>
2723
[Fact]
2824
public async Task AzureAIAgentWithConfigurationAsync()
2925
{
@@ -410,6 +406,13 @@ public async Task AzureAIAgentWithTemplateAsync()
410406
}
411407
}
412408

409+
public Step08_AzureAIAgent_Declarative(ITestOutputHelper output) : base(output)
410+
{
411+
var builder = Kernel.CreateBuilder();
412+
builder.Services.AddSingleton<AIProjectClient>(this.Client);
413+
this._kernel = builder.Build();
414+
}
415+
413416
#region private
414417
private readonly Kernel _kernel;
415418

dotnet/samples/GettingStartedWithAgents/BedrockAgent/Step07_BedrockAgent_Declarative.cs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,9 @@ namespace GettingStarted.BedrockAgents;
1414
/// </summary>
1515
public class Step07_BedrockAgent_Declarative : BaseBedrockAgentTest
1616
{
17-
public Step07_BedrockAgent_Declarative(ITestOutputHelper output) : base(output)
18-
{
19-
var builder = Kernel.CreateBuilder();
20-
builder.Services.AddSingleton<AmazonBedrockAgentClient>(this.Client);
21-
this._kernel = builder.Build();
22-
}
23-
17+
/// <summary>
18+
/// Demonstrates creating and using a Bedrock Agent with using configuration settings.
19+
/// </summary>
2420
[Fact]
2521
public async Task BedrockAgentWithConfigurationAsync()
2622
{
@@ -43,6 +39,9 @@ public async Task BedrockAgentWithConfigurationAsync()
4339
await InvokeAgentAsync(agent!, "Cats and Dogs");
4440
}
4541

42+
/// <summary>
43+
/// Demonstrates creating and using a Bedrock Agent with a code interpreter.
44+
/// </summary>
4645
[Fact]
4746
public async Task BedrockAgentWithCodeInterpreterAsync()
4847
{
@@ -67,6 +66,9 @@ public async Task BedrockAgentWithCodeInterpreterAsync()
6766
await InvokeAgentAsync(agent!, "Use code to determine the values in the Fibonacci sequence that are less then the value of 101?");
6867
}
6968

69+
/// <summary>
70+
/// Demonstrates creating and using a Bedrock Agent with functions.
71+
/// </summary>
7072
[Fact]
7173
public async Task BedrockAgentWithFunctionsAsync()
7274
{
@@ -111,6 +113,9 @@ public async Task BedrockAgentWithFunctionsAsync()
111113
await InvokeAgentAsync(agent!, "What is the current weather in Seattle and what is the weather forecast in Seattle?");
112114
}
113115

116+
/// <summary>
117+
/// Demonstrates creating and using a Bedrock Agent with a knowledge base.
118+
/// </summary>
114119
[Fact]
115120
public async Task BedrockAgentWithKnowledgeBaseAsync()
116121
{
@@ -138,6 +143,13 @@ public async Task BedrockAgentWithKnowledgeBaseAsync()
138143
await InvokeAgentAsync(agent!, "What is Semantic Kernel?");
139144
}
140145

146+
public Step07_BedrockAgent_Declarative(ITestOutputHelper output) : base(output)
147+
{
148+
var builder = Kernel.CreateBuilder();
149+
builder.Services.AddSingleton<AmazonBedrockAgentClient>(this.Client);
150+
this._kernel = builder.Build();
151+
}
152+
141153
protected override async Task<BedrockAgent> CreateAgentAsync(string agentName)
142154
{
143155
// Create a new agent on the Bedrock Agent service and prepare it for use

dotnet/samples/GettingStartedWithAgents/OpenAIAssistant/Step07_Assistant_Declarative.cs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,9 @@ namespace GettingStarted.OpenAIAssistants;
1515
/// </summary>
1616
public class Step07_Assistant_Declarative : BaseAssistantTest
1717
{
18-
public Step07_Assistant_Declarative(ITestOutputHelper output) : base(output)
19-
{
20-
var builder = Kernel.CreateBuilder();
21-
builder.Services.AddSingleton<OpenAIClient>(this.Client);
22-
this._kernel = builder.Build();
23-
}
24-
18+
/// <summary>
19+
/// Demonstrates creating and using a OpenAI Assistant using configuration.
20+
/// </summary>
2521
[Fact]
2622
public async Task OpenAIAssistantAgentWithConfigurationForOpenAIAsync()
2723
{
@@ -44,6 +40,9 @@ public async Task OpenAIAssistantAgentWithConfigurationForOpenAIAsync()
4440
await InvokeAgentAsync(agent!, "Could you please create a bar chart for the operating profit using the following data and provide the file to me? Company A: $1.2 million, Company B: $2.5 million, Company C: $3.0 million, Company D: $1.8 million");
4541
}
4642

43+
/// <summary>
44+
/// Demonstrates creating and using a OpenAI Assistant using configuration for Azure OpenAI.
45+
/// </summary>
4746
[Fact]
4847
public async Task OpenAIAssistantAgentWithConfigurationForAzureOpenAIAsync()
4948
{
@@ -70,6 +69,9 @@ public async Task OpenAIAssistantAgentWithConfigurationForAzureOpenAIAsync()
7069
await InvokeAgentAsync(agent!, "Could you please create a bar chart for the operating profit using the following data and provide the file to me? Company A: $1.2 million, Company B: $2.5 million, Company C: $3.0 million, Company D: $1.8 million");
7170
}
7271

72+
/// <summary>
73+
/// Demonstrates creating and using a OpenAI Assistant using a Kernel.
74+
/// </summary>
7375
[Fact]
7476
public async Task OpenAIAssistantAgentWithKernelAsync()
7577
{
@@ -89,6 +91,9 @@ public async Task OpenAIAssistantAgentWithKernelAsync()
8991
await InvokeAgentAsync(agent!, "Cats and Dogs");
9092
}
9193

94+
/// <summary>
95+
/// Demonstrates creating and using a OpenAI Assistant with templated instructions.
96+
/// </summary>
9297
[Fact]
9398
public async Task OpenAIAssistantAgentWithTemplateAsync()
9499
{
@@ -151,6 +156,13 @@ public async Task OpenAIAssistantAgentWithTemplateAsync()
151156
}
152157
}
153158

159+
public Step07_Assistant_Declarative(ITestOutputHelper output) : base(output)
160+
{
161+
var builder = Kernel.CreateBuilder();
162+
builder.Services.AddSingleton<OpenAIClient>(this.Client);
163+
this._kernel = builder.Build();
164+
}
165+
154166
#region private
155167
private readonly Kernel _kernel;
156168

dotnet/samples/GettingStartedWithAgents/Step09_Declarative.cs

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ namespace GettingStarted;
1212
/// </summary>
1313
public class Step09_Declarative(ITestOutputHelper output) : BaseAgentsTest(output)
1414
{
15+
/// <summary>
16+
/// Demonstrates creating and using a Chat Completion Agent with a Kernel.
17+
/// </summary>
1518
[Fact]
1619
public async Task ChatCompletionAgentWithKernelAsync()
1720
{
@@ -24,38 +27,19 @@ public async Task ChatCompletionAgentWithKernelAsync()
2427
description: Story Telling Agent
2528
instructions: Tell a story suitable for children about the topic provided by the user.
2629
""";
27-
var kernelAgentFactory = new ChatCompletionAgentFactory();
30+
var agentFactory = new ChatCompletionAgentFactory();
2831

29-
var agent = await kernelAgentFactory.CreateAgentFromYamlAsync(text, new() { Kernel = kernel });
32+
var agent = await agentFactory.CreateAgentFromYamlAsync(text, new() { Kernel = kernel });
3033

3134
await foreach (ChatMessageContent response in agent!.InvokeAsync("Cats and Dogs"))
3235
{
3336
this.WriteAgentChatMessage(response);
3437
}
3538
}
3639

37-
[Fact]
38-
public async Task ChatCompletionAgentWithConfigurationAsync()
39-
{
40-
Kernel kernel = this.CreateKernelWithChatCompletion();
41-
42-
var text =
43-
"""
44-
type: chat_completion_agent
45-
name: StoryAgent
46-
description: Store Telling Agent
47-
instructions: Tell a story suitable for children about the topic provided by the user.
48-
""";
49-
var kernelAgentFactory = new ChatCompletionAgentFactory();
50-
var configuration = TestConfiguration.GetSection(this.UseOpenAIConfig ? "OpenAI" : "AzureOpenAI");
51-
var agent = await kernelAgentFactory.CreateAgentFromYamlAsync(text, configuration: configuration);
52-
53-
await foreach (ChatMessageContent response in agent!.InvokeAsync(new ChatMessageContent(AuthorRole.User, "Cats and Dogs")))
54-
{
55-
this.WriteAgentChatMessage(response);
56-
}
57-
}
58-
40+
/// <summary>
41+
/// Demonstrates creating and using a Chat Completion Agent with functions.
42+
/// </summary>
5943
[Fact]
6044
public async Task ChatCompletionAgentWithFunctionsAsync()
6145
{
@@ -78,16 +62,19 @@ public async Task ChatCompletionAgentWithFunctionsAsync()
7862
- id: MenuPlugin.GetItemPrice
7963
type: function
8064
""";
81-
var kernelAgentFactory = new ChatCompletionAgentFactory();
65+
var agentFactory = new ChatCompletionAgentFactory();
8266

83-
var agent = await kernelAgentFactory.CreateAgentFromYamlAsync(text, new() { Kernel = kernel });
67+
var agent = await agentFactory.CreateAgentFromYamlAsync(text, new() { Kernel = kernel });
8468

8569
await foreach (ChatMessageContent response in agent!.InvokeAsync(new ChatMessageContent(AuthorRole.User, "What is the special soup and how much does it cost?")))
8670
{
8771
this.WriteAgentChatMessage(response);
8872
}
8973
}
9074

75+
/// <summary>
76+
/// Demonstrates creating and using a Chat Completion Agent with templated instructions.
77+
/// </summary>
9178
[Fact]
9279
public async Task ChatCompletionAgentWithTemplateAsync()
9380
{
@@ -113,10 +100,10 @@ public async Task ChatCompletionAgentWithTemplateAsync()
113100
template:
114101
format: semantic-kernel
115102
""";
116-
var kernelAgentFactory = new ChatCompletionAgentFactory();
103+
var agentFactory = new ChatCompletionAgentFactory();
117104
var promptTemplateFactory = new KernelPromptTemplateFactory();
118105

119-
var agent = await kernelAgentFactory.CreateAgentFromYamlAsync(text, new() { Kernel = kernel, PromptTemplateFactory = promptTemplateFactory });
106+
var agent = await agentFactory.CreateAgentFromYamlAsync(text, new() { Kernel = kernel, PromptTemplateFactory = promptTemplateFactory });
120107
Assert.NotNull(agent);
121108

122109
var options = new AgentInvokeOptions()

dotnet/samples/GettingStartedWithAgents/Step10_MultiAgent_Declarative.cs

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,30 +18,9 @@ namespace GettingStarted;
1818
/// </summary>
1919
public class Step10_MultiAgent_Declarative : BaseAgentsTest
2020
{
21-
public Step10_MultiAgent_Declarative(ITestOutputHelper output) : base(output)
22-
{
23-
var openaiClient =
24-
this.UseOpenAIConfig ?
25-
OpenAIAssistantAgent.CreateOpenAIClient(new ApiKeyCredential(this.ApiKey ?? throw new ConfigurationNotFoundException("OpenAI:ApiKey"))) :
26-
!string.IsNullOrWhiteSpace(this.ApiKey) ?
27-
OpenAIAssistantAgent.CreateAzureOpenAIClient(new ApiKeyCredential(this.ApiKey), new Uri(this.Endpoint!)) :
28-
OpenAIAssistantAgent.CreateAzureOpenAIClient(new AzureCliCredential(), new Uri(this.Endpoint!));
29-
30-
var aiProjectClient = AzureAIAgent.CreateAzureAIClient(TestConfiguration.AzureAI.ConnectionString, new AzureCliCredential());
31-
32-
var builder = Kernel.CreateBuilder();
33-
builder.Services.AddSingleton<OpenAIClient>(openaiClient);
34-
builder.Services.AddSingleton<AIProjectClient>(aiProjectClient);
35-
AddChatCompletionToKernel(builder);
36-
this._kernel = builder.Build();
37-
38-
this._kernelAgentFactory = new AggregatorKernelAgentFactory(
39-
new ChatCompletionAgentFactory(),
40-
new OpenAIAssistantAgentFactory(),
41-
new AzureAIAgentFactory()
42-
);
43-
}
44-
21+
/// <summary>
22+
/// Demonstrates creating and using a Chat Completion Agent with a Kernel.
23+
/// </summary>
4524
[Fact]
4625
public async Task ChatCompletionAgentWithKernelAsync()
4726
{
@@ -63,6 +42,9 @@ public async Task ChatCompletionAgentWithKernelAsync()
6342
}
6443
}
6544

45+
/// <summary>
46+
/// Demonstrates creating and using an Azure AI Agent with a Kernel.
47+
/// </summary>
6648
[Fact]
6749
public async Task AzureAIAgentWithKernelAsync()
6850
{
@@ -106,6 +88,30 @@ public async Task AzureAIAgentWithKernelAsync()
10688
}
10789
}
10890

91+
public Step10_MultiAgent_Declarative(ITestOutputHelper output) : base(output)
92+
{
93+
var openaiClient =
94+
this.UseOpenAIConfig ?
95+
OpenAIAssistantAgent.CreateOpenAIClient(new ApiKeyCredential(this.ApiKey ?? throw new ConfigurationNotFoundException("OpenAI:ApiKey"))) :
96+
!string.IsNullOrWhiteSpace(this.ApiKey) ?
97+
OpenAIAssistantAgent.CreateAzureOpenAIClient(new ApiKeyCredential(this.ApiKey), new Uri(this.Endpoint!)) :
98+
OpenAIAssistantAgent.CreateAzureOpenAIClient(new AzureCliCredential(), new Uri(this.Endpoint!));
99+
100+
var aiProjectClient = AzureAIAgent.CreateAzureAIClient(TestConfiguration.AzureAI.ConnectionString, new AzureCliCredential());
101+
102+
var builder = Kernel.CreateBuilder();
103+
builder.Services.AddSingleton<OpenAIClient>(openaiClient);
104+
builder.Services.AddSingleton<AIProjectClient>(aiProjectClient);
105+
AddChatCompletionToKernel(builder);
106+
this._kernel = builder.Build();
107+
108+
this._kernelAgentFactory = new AggregatorKernelAgentFactory(
109+
new ChatCompletionAgentFactory(),
110+
new OpenAIAssistantAgentFactory(),
111+
new AzureAIAgentFactory()
112+
);
113+
}
114+
109115
#region private
110116
private readonly Kernel _kernel;
111117
private readonly AgentFactory _kernelAgentFactory;

dotnet/src/Agents/Abstractions/Definition/AgentDefinition.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public sealed class AgentDefinition
5454
/// <summary>
5555
/// Gets or sets the collection of inputs used by the agent.
5656
/// </summary>
57-
public Dictionary<string, AgentInput>? Inputs { get; set; }
57+
public IDictionary<string, AgentInput>? Inputs { get; set; }
5858

5959
/// <summary>
6060
/// Gets or sets the collection of outputs supported by the agent.

dotnet/src/Agents/Abstractions/Definition/AgentFactory.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ public abstract class AgentFactory
2424
/// Initializes a new instance of the <see cref="AgentFactory"/> class.
2525
/// </summary>
2626
/// <param name="types">Types of agent this factory can create</param>
27-
protected AgentFactory(string[] types)
27+
protected AgentFactory(IEnumerable<string> types)
2828
{
29-
this.Types = types;
29+
this.Types = [.. types];
3030
}
3131

3232
/// <summary>
@@ -52,7 +52,7 @@ public async Task<Agent> CreateAsync(Kernel kernel, AgentDefinition agentDefinit
5252
Verify.NotNull(agentDefinition);
5353

5454
var kernelAgent = await this.TryCreateAsync(kernel, agentDefinition, agentCreationOptions, cancellationToken).ConfigureAwait(false);
55-
return (Agent?)kernelAgent ?? throw new KernelException($"Agent type {agentDefinition.Type} is not supported.");
55+
return (Agent?)kernelAgent ?? throw new NotSupportedException($"Agent type {agentDefinition.Type} is not supported.");
5656
}
5757

5858
/// <summary>

dotnet/src/Agents/Abstractions/Extensions/AgentToolDefinitionExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ public static class AgentToolDefinitionExtensions
3434
{
3535
return (T?)Convert.ChangeType(value, typeof(T));
3636
}
37-
catch (InvalidCastException)
37+
catch (InvalidCastException ex)
3838
{
39-
throw new InvalidCastException($"The option key '{key}' value must be of type '{typeof(T?)}' but is '{value.GetType()}'.");
39+
throw new InvalidCastException($"The option key '{key}' value must be of type '{typeof(T?)}' but is '{value.GetType()}'.", ex);
4040
}
4141
}
4242

0 commit comments

Comments
 (0)