-
Notifications
You must be signed in to change notification settings - Fork 5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
dotnet/sample/AutoGen.BasicSamples/GettingStart/Streaming_Tool_Call.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Streaming_Tool_Call.cs | ||
|
||
using AutoGen.Core; | ||
using AutoGen.OpenAI; | ||
using AutoGen.OpenAI.Extension; | ||
using Azure.AI.OpenAI; | ||
using FluentAssertions; | ||
|
||
namespace AutoGen.BasicSample.GettingStart; | ||
|
||
internal class Streaming_Tool_Call | ||
{ | ||
public static async Task RunAsync() | ||
{ | ||
#region Create_tools | ||
var tools = new Tools(); | ||
#endregion Create_tools | ||
|
||
#region Create_auto_invoke_middleware | ||
var autoInvokeMiddleware = new FunctionCallMiddleware( | ||
functions: [tools.GetWeatherFunctionContract], | ||
functionMap: new Dictionary<string, Func<string, Task<string>>>() | ||
{ | ||
{ tools.GetWeatherFunctionContract.Name, tools.GetWeatherWrapper }, | ||
}); | ||
#endregion Create_auto_invoke_middleware | ||
|
||
#region Create_Agent | ||
var apiKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY") ?? throw new Exception("Please set OPENAI_API_KEY environment variable."); | ||
var model = "gpt-4o"; | ||
var openaiClient = new OpenAIClient(apiKey); | ||
var agent = new OpenAIChatAgent( | ||
openAIClient: openaiClient, | ||
name: "agent", | ||
modelName: model, | ||
systemMessage: "You are a helpful AI assistant") | ||
.RegisterMessageConnector() | ||
.RegisterStreamingMiddleware(autoInvokeMiddleware) | ||
.RegisterPrintMessage(); | ||
#endregion Create_Agent | ||
|
||
IMessage finalReply = null; | ||
var question = new TextMessage(Role.User, "What's the weather in Seattle"); | ||
|
||
// In streaming function call | ||
// function can only be invoked untill all the chunks are collected | ||
// therefore, only one ToolCallAggregateMessage chunk will be return here. | ||
await foreach (var message in agent.GenerateStreamingReplyAsync([question])) | ||
{ | ||
finalReply = message; | ||
} | ||
|
||
finalReply?.GetContent().Should().Be("The weather in Seattle is sunny."); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters