Skip to content

Commit d073678

Browse files
add streaming tool call example (#3167)
1 parent bd735d0 commit d073678

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
}

dotnet/test/AutoGen.Tests/Orchestrator/RolePlayOrchestratorTests.cs

+31
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,37 @@ public async Task GPT_3_5_CoderReviewerRunnerTestAsync()
228228
await CoderReviewerRunnerTestAsync(openAIChatAgent);
229229
}
230230

231+
[ApiKeyFact("OPENAI_API_KEY")]
232+
public async Task GPT_4o_CoderReviewerRunnerTestAsync()
233+
{
234+
var apiKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY");
235+
var model = "gpt-4o";
236+
var openaiClient = new OpenAIClient(apiKey);
237+
var openAIChatAgent = new OpenAIChatAgent(
238+
openAIClient: openaiClient,
239+
name: "assistant",
240+
modelName: model)
241+
.RegisterMessageConnector();
242+
243+
await CoderReviewerRunnerTestAsync(openAIChatAgent);
244+
}
245+
246+
[ApiKeyFact("OPENAI_API_KEY")]
247+
public async Task GPT_4o_mini_CoderReviewerRunnerTestAsync()
248+
{
249+
var apiKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY");
250+
var model = "gpt-4o-mini";
251+
var openaiClient = new OpenAIClient(apiKey);
252+
var openAIChatAgent = new OpenAIChatAgent(
253+
openAIClient: openaiClient,
254+
name: "assistant",
255+
modelName: model)
256+
.RegisterMessageConnector();
257+
258+
await CoderReviewerRunnerTestAsync(openAIChatAgent);
259+
}
260+
261+
231262
[ApiKeyFact("GOOGLE_GEMINI_API_KEY")]
232263
public async Task GoogleGemini_1_5_flash_001_CoderReviewerRunnerTestAsync()
233264
{

0 commit comments

Comments
 (0)