55using System . Linq ;
66using System . Threading . Tasks ;
77using Microsoft . SemanticKernel . Experimental . Agents ;
8+ using Xunit ;
9+ using Xunit . Abstractions ;
10+
11+ namespace Examples ;
812
913// ReSharper disable once InconsistentNaming
1014/// <summary>
1115/// Showcase usage of code_interpreter and retrieval tools.
1216/// </summary>
13- public static class Example75_AgentTools
17+ public sealed class Example75_AgentTools : BaseTest
1418{
1519 /// <summary>
1620 /// Specific model is required that supports agents and parallel function calling.
@@ -19,18 +23,19 @@ public static class Example75_AgentTools
1923 private const string OpenAIFunctionEnabledModel = "gpt-4-1106-preview" ;
2024
2125 // Track agents for clean-up
22- private static readonly List < IAgent > s_agents = new ( ) ;
26+ private readonly List < IAgent > _agents = new ( ) ;
2327
2428 /// <summary>
2529 /// Show how to utilize code_interpreter and retrieval tools.
2630 /// </summary>
27- public static async Task RunAsync ( )
31+ [ Fact ]
32+ public async Task RunAsync ( )
2833 {
29- Console . WriteLine ( "======== Example75_AgentTools ========" ) ;
34+ this . WriteLine ( "======== Example75_AgentTools ========" ) ;
3035
3136 if ( TestConfiguration . OpenAI . ApiKey == null )
3237 {
33- Console . WriteLine ( "OpenAI apiKey not found. Skipping example." ) ;
38+ this . WriteLine ( "OpenAI apiKey not found. Skipping example." ) ;
3439 return ;
3540 }
3641
@@ -41,9 +46,9 @@ public static async Task RunAsync()
4146 await RunRetrievalToolAsync ( ) ;
4247 }
4348
44- private static async Task RunCodeInterpreterToolAsync ( )
49+ private async Task RunCodeInterpreterToolAsync ( )
4550 {
46- Console . WriteLine ( "======== Run:CodeInterpreterTool ========" ) ;
51+ this . WriteLine ( "======== Run:CodeInterpreterTool ========" ) ;
4752
4853 var builder =
4954 new AgentBuilder ( )
@@ -68,13 +73,13 @@ await ChatAsync(
6873 }
6974 finally
7075 {
71- await Task . WhenAll ( s_agents . Select ( a => a . DeleteAsync ( ) ) ) ;
76+ await Task . WhenAll ( this . _agents . Select ( a => a . DeleteAsync ( ) ) ) ;
7277 }
7378 }
7479
75- private static async Task RunRetrievalToolAsync ( )
80+ private async Task RunRetrievalToolAsync ( )
7681 {
77- Console . WriteLine ( "======== Run:RunRetrievalTool ========" ) ;
82+ this . WriteLine ( "======== Run:RunRetrievalTool ========" ) ;
7883
7984 // REQUIRED:
8085 //
@@ -111,29 +116,29 @@ await ChatAsync(
111116 }
112117 finally
113118 {
114- await Task . WhenAll ( s_agents . Select ( a => a . DeleteAsync ( ) ) ) ;
119+ await Task . WhenAll ( this . _agents . Select ( a => a . DeleteAsync ( ) ) ) ;
115120 }
116121 }
117122
118123 /// <summary>
119124 /// Common chat loop used for: RunCodeInterpreterToolAsync and RunRetrievalToolAsync.
120125 /// Processes each question for both "default" and "enabled" agents.
121126 /// </summary>
122- private static async Task ChatAsync (
127+ private async Task ChatAsync (
123128 IAgent defaultAgent ,
124129 IAgent enabledAgent ,
125130 params string [ ] questions )
126131 {
127132 foreach ( var question in questions )
128133 {
129- Console . WriteLine ( "\n DEFAULT AGENT:" ) ;
134+ this . WriteLine ( "\n DEFAULT AGENT:" ) ;
130135 await InvokeAgentAsync ( defaultAgent , question ) ;
131136
132- Console . WriteLine ( "\n TOOL ENABLED AGENT:" ) ;
137+ this . WriteLine ( "\n TOOL ENABLED AGENT:" ) ;
133138 await InvokeAgentAsync ( enabledAgent , question ) ;
134139 }
135140
136- static async Task InvokeAgentAsync ( IAgent agent , string question )
141+ async Task InvokeAgentAsync ( IAgent agent , string question )
137142 {
138143 await foreach ( var message in agent . InvokeAsync ( question ) )
139144 {
@@ -143,26 +148,28 @@ static async Task InvokeAgentAsync(IAgent agent, string question)
143148 content = content . Replace ( annotation . Label , string . Empty , StringComparison . Ordinal ) ;
144149 }
145150
146- Console . WriteLine ( $ "# { message . Role } : { content } ") ;
151+ this . WriteLine ( $ "# { message . Role } : { content } ") ;
147152
148153 if ( message . Annotations . Count > 0 )
149154 {
150- Console . WriteLine ( "\n # files:" ) ;
155+ this . WriteLine ( "\n # files:" ) ;
151156 foreach ( var annotation in message . Annotations )
152157 {
153- Console . WriteLine ( $ "* { annotation . FileId } ") ;
158+ this . WriteLine ( $ "* { annotation . FileId } ") ;
154159 }
155160 }
156161 }
157162
158- Console . WriteLine ( ) ;
163+ this . WriteLine ( ) ;
159164 }
160165 }
161166
162- private static IAgent Track ( IAgent agent )
167+ private IAgent Track ( IAgent agent )
163168 {
164- s_agents . Add ( agent ) ;
169+ this . _agents . Add ( agent ) ;
165170
166171 return agent ;
167172 }
173+
174+ public Example75_AgentTools ( ITestOutputHelper output ) : base ( output ) { }
168175}
0 commit comments