|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Streaming_Tool_Call.cs |
| 3 | + |
| 4 | +using AutoGen.Core; |
| 5 | +using AutoGen.OpenAI; |
| 6 | +using AutoGen.OpenAI.Extension; |
| 7 | +using Azure.AI.OpenAI; |
| 8 | +using FluentAssertions; |
| 9 | + |
| 10 | +namespace AutoGen.BasicSample.GettingStart; |
| 11 | + |
| 12 | +internal class Streaming_Tool_Call |
| 13 | +{ |
| 14 | + public static async Task RunAsync() |
| 15 | + { |
| 16 | + #region Create_tools |
| 17 | + var tools = new Tools(); |
| 18 | + #endregion Create_tools |
| 19 | + |
| 20 | + #region Create_auto_invoke_middleware |
| 21 | + var autoInvokeMiddleware = new FunctionCallMiddleware( |
| 22 | + functions: [tools.GetWeatherFunctionContract], |
| 23 | + functionMap: new Dictionary<string, Func<string, Task<string>>>() |
| 24 | + { |
| 25 | + { tools.GetWeatherFunctionContract.Name, tools.GetWeatherWrapper }, |
| 26 | + }); |
| 27 | + #endregion Create_auto_invoke_middleware |
| 28 | + |
| 29 | + #region Create_Agent |
| 30 | + var apiKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY") ?? throw new Exception("Please set OPENAI_API_KEY environment variable."); |
| 31 | + var model = "gpt-4o"; |
| 32 | + var openaiClient = new OpenAIClient(apiKey); |
| 33 | + var agent = new OpenAIChatAgent( |
| 34 | + openAIClient: openaiClient, |
| 35 | + name: "agent", |
| 36 | + modelName: model, |
| 37 | + systemMessage: "You are a helpful AI assistant") |
| 38 | + .RegisterMessageConnector() |
| 39 | + .RegisterStreamingMiddleware(autoInvokeMiddleware) |
| 40 | + .RegisterPrintMessage(); |
| 41 | + #endregion Create_Agent |
| 42 | + |
| 43 | + IMessage finalReply = null; |
| 44 | + var question = new TextMessage(Role.User, "What's the weather in Seattle"); |
| 45 | + |
| 46 | + // In streaming function call |
| 47 | + // function can only be invoked untill all the chunks are collected |
| 48 | + // therefore, only one ToolCallAggregateMessage chunk will be return here. |
| 49 | + await foreach (var message in agent.GenerateStreamingReplyAsync([question])) |
| 50 | + { |
| 51 | + finalReply = message; |
| 52 | + } |
| 53 | + |
| 54 | + finalReply?.GetContent().Should().Be("The weather in Seattle is sunny."); |
| 55 | + } |
| 56 | +} |
0 commit comments