-
Notifications
You must be signed in to change notification settings - Fork 0
/
Example02_TwoAgent_MathChat.cs
72 lines (67 loc) · 2.69 KB
/
Example02_TwoAgent_MathChat.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
using AutoGen.Core;
using AutoGen.SemanticKernel.Extension;
using FluentAssertions;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Connectors.OpenAI;
namespace AutoGen.BasicSamples;
public static class Example02_TwoAgent_MathChat
{
public static async Task RunAsync(IKernelBuilder kernelBuilder, OpenAIPromptExecutionSettings settings)
{
#region code_snippet_1
// create teacher agent
// teacher agent will create math questions
var teacher = kernelBuilder.Build()
.ToSemanticKernelAgent(
name: "teacher",
systemMessage: @"You are a teacher that create pre-school math question for student and check answer.
If the answer is correct, you terminate conversation by saying [TERMINATE].
If the answer is wrong, you ask student to fix it.",
settings: settings)
.RegisterMessageConnector()
.RegisterMiddleware(async (messages, options, agent2, ct) =>
{
var reply = await agent2.GenerateReplyAsync(messages, options, ct);
if (reply.GetContent()?.ToLower().Contains("terminate") is true)
{
return new TextMessage(Role.Assistant, GroupChatExtension.TERMINATE, from: reply.From);
}
return reply;
})
.RegisterPrintMessage();
// create student agent
// student agent will answer the math questions
var student = kernelBuilder.Build()
.ToSemanticKernelAgent(
name: "student",
systemMessage: "You are a student that answer pre-school math question from teacher.",
settings: settings)
.RegisterMessageConnector()
.RegisterPrintMessage();
// start the conversation
var conversation = await student.InitiateChatAsync(
receiver: teacher,
message: "Hey teacher, please create math question for me.",
maxRound: 10);
// output
// Message from teacher
// --------------------
// content: Of course!Here's a math question for you:
//
// What is 2 + 3 ?
// --------------------
//
// Message from student
// --------------------
// content: The sum of 2 and 3 is 5.
// --------------------
//
// Message from teacher
// --------------------
// content: [GROUPCHAT_TERMINATE]
// --------------------
#endregion code_snippet_1
conversation.Count().Should().BeLessThan(10);
conversation.Last().IsGroupChatTerminateMessage().Should().BeTrue();
}
}