| 
									
										
										
										
											2024-09-30 16:32:48 -07:00
										 |  |  | // Copyright (c) Microsoft Corporation. All rights reserved. | 
					
						
							| 
									
										
										
										
											2024-06-20 10:47:31 -07:00
										 |  |  | // Use_Tools_With_Agent.cs | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #region Using | 
					
						
							|  |  |  | using AutoGen.Core; | 
					
						
							| 
									
										
										
										
											2024-08-27 14:37:47 -07:00
										 |  |  | using AutoGen.OpenAI; | 
					
						
							|  |  |  | using AutoGen.OpenAI.Extension; | 
					
						
							| 
									
										
										
										
											2024-06-20 10:47:31 -07:00
										 |  |  | #endregion Using | 
					
						
							|  |  |  | using FluentAssertions; | 
					
						
							| 
									
										
										
										
											2024-08-27 14:37:47 -07:00
										 |  |  | using OpenAI; | 
					
						
							| 
									
										
										
										
											2024-06-20 10:47:31 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-28 17:13:36 -05:00
										 |  |  | namespace AutoGen.Basic.Sample; | 
					
						
							| 
									
										
										
										
											2024-06-20 10:47:31 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-25 09:23:24 -07:00
										 |  |  | #region Tools | 
					
						
							| 
									
										
										
										
											2024-06-20 10:47:31 -07:00
										 |  |  | public partial class Tools | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     /// <summary> | 
					
						
							|  |  |  |     /// Get the weather of the city. | 
					
						
							|  |  |  |     /// </summary> | 
					
						
							|  |  |  |     /// <param name="city"></param> | 
					
						
							|  |  |  |     [Function] | 
					
						
							|  |  |  |     public async Task<string> GetWeather(string city) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $"The weather in {city} is sunny."; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2024-06-25 09:23:24 -07:00
										 |  |  | #endregion Tools | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-20 10:47:31 -07:00
										 |  |  | public class Use_Tools_With_Agent | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     public static async Task RunAsync() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         #region Create_tools | 
					
						
							|  |  |  |         var tools = new Tools(); | 
					
						
							|  |  |  |         #endregion Create_tools | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-25 09:23:24 -07:00
										 |  |  |         #region Create_auto_invoke_middleware | 
					
						
							|  |  |  |         var autoInvokeMiddleware = new FunctionCallMiddleware( | 
					
						
							| 
									
										
										
										
											2024-06-20 10:47:31 -07:00
										 |  |  |             functions: [tools.GetWeatherFunctionContract], | 
					
						
							|  |  |  |             functionMap: new Dictionary<string, Func<string, Task<string>>>() | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 { tools.GetWeatherFunctionContract.Name!, tools.GetWeatherWrapper }, | 
					
						
							|  |  |  |             }); | 
					
						
							| 
									
										
										
										
											2024-06-25 09:23:24 -07:00
										 |  |  |         #endregion Create_auto_invoke_middleware | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         #region Create_no_invoke_middleware | 
					
						
							|  |  |  |         var noInvokeMiddleware = new FunctionCallMiddleware( | 
					
						
							|  |  |  |             functions: [tools.GetWeatherFunctionContract]); | 
					
						
							|  |  |  |         #endregion Create_no_invoke_middleware | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         #region Create_Agent | 
					
						
							|  |  |  |         var apiKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY") ?? throw new Exception("Please set OPENAI_API_KEY environment variable."); | 
					
						
							| 
									
										
										
										
											2024-08-27 14:37:47 -07:00
										 |  |  |         var model = "gpt-4o-mini"; | 
					
						
							| 
									
										
										
										
											2024-06-25 09:23:24 -07:00
										 |  |  |         var openaiClient = new OpenAIClient(apiKey); | 
					
						
							| 
									
										
										
										
											2024-06-20 10:47:31 -07:00
										 |  |  |         var agent = new OpenAIChatAgent( | 
					
						
							| 
									
										
										
										
											2024-08-27 14:37:47 -07:00
										 |  |  |             chatClient: openaiClient.GetChatClient(model), | 
					
						
							| 
									
										
										
										
											2024-06-20 10:47:31 -07:00
										 |  |  |             name: "agent", | 
					
						
							|  |  |  |             systemMessage: "You are a helpful AI assistant") | 
					
						
							| 
									
										
										
										
											2024-06-25 09:23:24 -07:00
										 |  |  |             .RegisterMessageConnector(); // convert OpenAI message to AutoGen message | 
					
						
							| 
									
										
										
										
											2024-06-20 10:47:31 -07:00
										 |  |  |         #endregion Create_Agent | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-25 09:23:24 -07:00
										 |  |  |         #region Single_Turn_Auto_Invoke | 
					
						
							|  |  |  |         var autoInvokeAgent = agent | 
					
						
							|  |  |  |             .RegisterMiddleware(autoInvokeMiddleware) // pass function definition to agent. | 
					
						
							|  |  |  |             .RegisterPrintMessage(); // print the message content | 
					
						
							| 
									
										
										
										
											2024-06-20 10:47:31 -07:00
										 |  |  |         var question = new TextMessage(Role.User, "What is the weather in Seattle?"); | 
					
						
							| 
									
										
										
										
											2024-06-25 09:23:24 -07:00
										 |  |  |         var reply = await autoInvokeAgent.SendAsync(question); | 
					
						
							|  |  |  |         reply.Should().BeOfType<ToolCallAggregateMessage>(); | 
					
						
							|  |  |  |         #endregion Single_Turn_Auto_Invoke | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         #region Single_Turn_No_Invoke | 
					
						
							|  |  |  |         var noInvokeAgent = agent | 
					
						
							|  |  |  |             .RegisterMiddleware(noInvokeMiddleware) // pass function definition to agent. | 
					
						
							|  |  |  |             .RegisterPrintMessage(); // print the message content | 
					
						
							| 
									
										
										
										
											2024-06-20 10:47:31 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-25 09:23:24 -07:00
										 |  |  |         question = new TextMessage(Role.User, "What is the weather in Seattle?"); | 
					
						
							|  |  |  |         reply = await noInvokeAgent.SendAsync(question); | 
					
						
							|  |  |  |         reply.Should().BeOfType<ToolCallMessage>(); | 
					
						
							|  |  |  |         #endregion Single_Turn_No_Invoke | 
					
						
							| 
									
										
										
										
											2024-06-20 10:47:31 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         #region Multi_Turn_Tool_Call | 
					
						
							| 
									
										
										
										
											2024-06-25 09:23:24 -07:00
										 |  |  |         var finalReply = await agent.SendAsync(chatHistory: [question, reply]); | 
					
						
							| 
									
										
										
										
											2024-06-20 10:47:31 -07:00
										 |  |  |         #endregion Multi_Turn_Tool_Call | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         #region verify_reply | 
					
						
							|  |  |  |         finalReply.Should().BeOfType<TextMessage>(); | 
					
						
							|  |  |  |         #endregion verify_reply | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         #region parallel_tool_call | 
					
						
							|  |  |  |         question = new TextMessage(Role.User, "What is the weather in Seattle, New York and Vancouver"); | 
					
						
							| 
									
										
										
										
											2024-06-25 09:23:24 -07:00
										 |  |  |         reply = await agent.SendAsync(question); | 
					
						
							| 
									
										
										
										
											2024-06-20 10:47:31 -07:00
										 |  |  |         #endregion parallel_tool_call | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         #region verify_parallel_tool_call_reply | 
					
						
							| 
									
										
										
										
											2024-06-25 09:23:24 -07:00
										 |  |  |         reply.Should().BeOfType<ToolCallAggregateMessage>(); | 
					
						
							| 
									
										
										
										
											2024-09-30 16:32:48 -07:00
										 |  |  |         (reply as ToolCallAggregateMessage)!.Message1.ToolCalls.Count.Should().Be(3); | 
					
						
							| 
									
										
										
										
											2024-06-20 10:47:31 -07:00
										 |  |  |         #endregion verify_parallel_tool_call_reply | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         #region Multi_Turn_Parallel_Tool_Call | 
					
						
							| 
									
										
										
										
											2024-06-25 09:23:24 -07:00
										 |  |  |         finalReply = await agent.SendAsync(chatHistory: [question, reply]); | 
					
						
							|  |  |  |         finalReply.Should().BeOfType<ToolCallAggregateMessage>(); | 
					
						
							| 
									
										
										
										
											2024-09-30 16:32:48 -07:00
										 |  |  |         (finalReply as ToolCallAggregateMessage)!.Message1.ToolCalls.Count.Should().Be(3); | 
					
						
							| 
									
										
										
										
											2024-06-20 10:47:31 -07:00
										 |  |  |         #endregion Multi_Turn_Parallel_Tool_Call | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2024-06-25 09:23:24 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-20 10:47:31 -07:00
										 |  |  | } |