-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExample03_Agent_FunctionCall.cs
More file actions
93 lines (83 loc) · 3.81 KB
/
Example03_Agent_FunctionCall.cs
File metadata and controls
93 lines (83 loc) · 3.81 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
using System.ComponentModel;
using AutoGen.Core;
using AutoGen.SemanticKernel.Extension;
using FluentAssertions;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Connectors.OpenAI;
namespace AutoGen.BasicSamples;
public partial class Example03_Agent_FunctionCall
{
/// <summary>
/// upper case the message when asked.
/// </summary>
/// <param name="message"></param>
[KernelFunction]
[Description("upper case the message when asked.")]
public async Task<string> UpperCase(string message)
{
return message.ToUpper();
}
/// <summary>
/// Concatenate strings.
/// </summary>
/// <param name="strings">strings to concatenate</param>
[KernelFunction]
[Description("Concatenate strings.")]
public async Task<string> ConcatString(string[] strings)
{
return string.Join(" ", strings);
}
/// <summary>
/// calculate tax
/// </summary>
/// <param name="price">price, should be an integer</param>
/// <param name="taxRate">tax rate, should be in range (0, 1)</param>
[KernelFunction]
[Description("calculate tax.")]
public async Task<string> CalculateTax(int price, float taxRate)
{
return $"tax is {price * taxRate}";
}
public static async Task RunAsync(IKernelBuilder kernelBuilder, OpenAIPromptExecutionSettings settings)
{
var instance = new Example03_Agent_FunctionCall();
settings.ToolCallBehavior = ToolCallBehavior.AutoInvokeKernelFunctions;
var kernel = kernelBuilder.Build();
kernel.Plugins.AddFromObject(instance);
var agent = kernel
.ToSemanticKernelAgent(
name: "agent",
systemMessage: "You are a helpful AI assistant, if you get message with [TERMINATE], then terminate conversation.",
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();
// talk to the assistant agent
// var upperCase = await agent.SendAsync("convert to upper case: hello world");
// upperCase.GetContent()?.Should().Be("HELLO WORLD");
// upperCase.Should().BeOfType<AggregateMessage<ToolCallMessage, ToolCallResultMessage>>();
// upperCase.GetToolCalls().Should().HaveCount(1);
// upperCase.GetToolCalls().First().FunctionName.Should().Be(nameof(UpperCase));
// var concatString = await agent.SendAsync("concatenate strings: a, b, c, d, e");
// concatString.GetContent()?.Should().Be("a b c d e");
// concatString.Should().BeOfType<AggregateMessage<ToolCallMessage, ToolCallResultMessage>>();
// concatString.GetToolCalls().Should().HaveCount(1);
// concatString.GetToolCalls().First().FunctionName.Should().Be(nameof(ConcatString));
// var calculateTax = await agent.SendAsync("calculate tax: 100, 0.1");
// calculateTax.GetContent().Should().Be("tax is 10");
// calculateTax.Should().BeOfType<AggregateMessage<ToolCallMessage, ToolCallResultMessage>>();
// calculateTax.GetToolCalls().Should().HaveCount(1);
// calculateTax.GetToolCalls().First().FunctionName.Should().Be(nameof(CalculateTax));
var userProxy = new UserProxyAgent(name: "user", defaultReply: GroupChatExtension.TERMINATE, humanInputMode: HumanInputMode.ALWAYS)
.RegisterPrintMessage();
await userProxy.InitiateChatAsync(agent);
}
}