Skip to content

Commit e83570d

Browse files
.Net: Function calling: Add function author role and name property (#3421)
### Motivation and Context Replaces #3302 Resolves #3017 and enables the full function calling scenario as described in the [OpenAI function calling documentation](https://platform.openai.com/docs/guides/gpt/function-calling). ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> Adds the "function" author role to allow adding function messages to chat history. Function messages require a "name" parameter, so this change also adds that to chat message model. ### Contribution Checklist <!-- Before submitting this PR, please make sure: --> - [x] The code builds clean without any errors or warnings - [x] The PR follows the [SK Contribution Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md) and the [pre-submission formatting script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts) raises no violations - [x] All unit tests pass, and I have added new tests where possible - [x] I didn't break anyone 😄 --------- Co-authored-by: Gina Triolo <[email protected]>
1 parent dd38e34 commit e83570d

8 files changed

Lines changed: 348 additions & 56 deletions

File tree

‎dotnet/samples/KernelSyntaxExamples/Example59_OpenAIFunctionCalling.cs‎

Lines changed: 45 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,14 @@ public static async Task RunAsync()
3737
// Set FunctionCall to the name of a specific function to force the model to use that function.
3838
requestSettings.FunctionCall = "TimePlugin-Date";
3939
await CompleteChatWithFunctionsAsync("What day is today?", chatHistory, chatCompletion, kernel, requestSettings);
40-
await StreamingCompleteChatWithFunctionsAsync("What day is today?", chatHistory, chatCompletion, kernel, requestSettings);
40+
41+
// Uncomment the samples and run them one at a time
42+
//await StreamingCompleteChatWithFunctionsAsync("What day is today?", chatHistory, chatCompletion, kernel, requestSettings);
4143

4244
// Set FunctionCall to auto to let the model choose the best function to use.
4345
requestSettings.FunctionCall = OpenAIRequestSettings.FunctionCallAuto;
44-
await CompleteChatWithFunctionsAsync("What computer tablets are available for under $200?", chatHistory, chatCompletion, kernel, requestSettings);
45-
await StreamingCompleteChatWithFunctionsAsync("What computer tablets are available for under $200?", chatHistory, chatCompletion, kernel, requestSettings);
46+
//await CompleteChatWithFunctionsAsync("What computer tablets are available for under $200?", chatHistory, chatCompletion, kernel, requestSettings);
47+
//await StreamingCompleteChatWithFunctionsAsync("What computer tablets are available for under $200?", chatHistory, chatCompletion, kernel, requestSettings);
4648
}
4749

4850
private static async Task<IKernel> InitializeKernelAsync()
@@ -66,56 +68,43 @@ private static async Task CompleteChatWithFunctionsAsync(string ask, ChatHistory
6668
Console.WriteLine($"User message: {ask}");
6769
chatHistory.AddUserMessage(ask);
6870

69-
// Send request
71+
// Send request and add response to chat history
7072
var chatResult = (await chatCompletion.GetChatCompletionsAsync(chatHistory, requestSettings))[0];
73+
chatHistory.AddAssistantMessage(chatResult);
7174

72-
// Check for message response
73-
var chatMessage = await chatResult.GetChatMessageAsync();
74-
if (!string.IsNullOrEmpty(chatMessage.Content))
75-
{
76-
Console.WriteLine(chatMessage.Content);
77-
78-
// Add the response to chat history
79-
chatHistory.AddAssistantMessage(chatMessage.Content);
80-
}
75+
await PrintChatResultAsync(chatResult);
8176

8277
// Check for function response
8378
OpenAIFunctionResponse? functionResponse = chatResult.GetOpenAIFunctionResponse();
8479
if (functionResponse is not null)
8580
{
86-
// Print function response details
87-
Console.WriteLine("Function name: " + functionResponse.FunctionName);
88-
Console.WriteLine("Plugin name: " + functionResponse.PluginName);
89-
Console.WriteLine("Arguments: ");
90-
foreach (var parameter in functionResponse.Parameters)
91-
{
92-
Console.WriteLine($"- {parameter.Key}: {parameter.Value}");
93-
}
94-
9581
// If the function returned by OpenAI is an SKFunction registered with the kernel,
9682
// you can invoke it using the following code.
9783
if (kernel.Functions.TryGetFunctionAndContext(functionResponse, out ISKFunction? func, out ContextVariables? context))
9884
{
99-
var kernelResult = await kernel.RunAsync(func, context);
85+
var result = (await kernel.RunAsync(func, context)).GetValue<object>();
10086

101-
var result = kernelResult.GetValue<object>();
102-
103-
string? resultMessage = null;
87+
string? resultContent = null;
10488
if (result is RestApiOperationResponse apiResponse)
10589
{
106-
resultMessage = apiResponse.Content?.ToString();
90+
resultContent = apiResponse.Content?.ToString();
10791
}
10892
else if (result is string str)
10993
{
110-
resultMessage = str;
94+
resultContent = str;
11195
}
11296

113-
if (!string.IsNullOrEmpty(resultMessage))
97+
if (!string.IsNullOrEmpty(resultContent))
11498
{
115-
Console.WriteLine(resultMessage);
116-
11799
// Add the function result to chat history
118-
chatHistory.AddAssistantMessage(resultMessage);
100+
chatHistory.AddFunctionMessage(resultContent, functionResponse.FullyQualifiedName);
101+
102+
// Get another completion
103+
requestSettings.FunctionCall = OpenAIRequestSettings.FunctionCallNone;
104+
chatResult = (await chatCompletion.GetChatCompletionsAsync(chatHistory, requestSettings))[0];
105+
chatHistory.AddAssistantMessage(chatResult);
106+
107+
await PrintChatResultAsync(chatResult);
119108
}
120109
}
121110
else
@@ -125,6 +114,30 @@ private static async Task CompleteChatWithFunctionsAsync(string ask, ChatHistory
125114
}
126115
}
127116

117+
private static async Task PrintChatResultAsync(IChatResult chatResult)
118+
{
119+
// Check for message response
120+
var chatMessage = await chatResult.GetChatMessageAsync();
121+
if (!string.IsNullOrEmpty(chatMessage.Content))
122+
{
123+
Console.WriteLine($"Assistant response: {chatMessage.Content}");
124+
}
125+
126+
// Check for function response
127+
OpenAIFunctionResponse? functionResponse = chatResult.GetOpenAIFunctionResponse();
128+
if (functionResponse is not null)
129+
{
130+
// Print function response details
131+
Console.WriteLine("Function name: " + functionResponse.FunctionName);
132+
Console.WriteLine("Plugin name: " + functionResponse.PluginName);
133+
Console.WriteLine("Arguments: ");
134+
foreach (var parameter in functionResponse.Parameters)
135+
{
136+
Console.WriteLine($"- {parameter.Key}: {parameter.Value}");
137+
}
138+
}
139+
}
140+
128141
private static async Task StreamingCompleteChatWithFunctionsAsync(string ask, ChatHistory chatHistory, IChatCompletion chatCompletion, IKernel kernel, OpenAIRequestSettings requestSettings)
129142
{
130143
Console.WriteLine($"User message: {ask}");

‎dotnet/src/Connectors/Connectors.AI.OpenAI/AzureSdk/ClientBase.cs‎

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ namespace Microsoft.SemanticKernel.Connectors.AI.OpenAI.AzureSdk;
2929
public abstract class ClientBase
3030
{
3131
private const int MaxResultsPerPrompt = 128;
32+
private const string NameProperty = "Name";
33+
private const string ArgumentsProperty = "Arguments";
3234

3335
// Prevent external inheritors
3436
private protected ClientBase(ILoggerFactory? loggerFactory = null)
@@ -407,25 +409,22 @@ private static ChatCompletionsOptions CreateChatCompletionsOptions(OpenAIRequest
407409

408410
foreach (var message in chatHistory)
409411
{
410-
var validRole = GetValidChatRole(message.Role);
411-
options.Messages.Add(new ChatMessage(validRole, message.Content));
412-
}
412+
var azureMessage = new ChatMessage(new ChatRole(message.Role.Label), message.Content);
413413

414-
return options;
415-
}
414+
if (message.AdditionalProperties?.TryGetValue(NameProperty, out string? name) is true)
415+
{
416+
azureMessage.Name = name;
416417

417-
private static ChatRole GetValidChatRole(AuthorRole role)
418-
{
419-
var validRole = new ChatRole(role.Label);
418+
if (message.AdditionalProperties?.TryGetValue(ArgumentsProperty, out string? arguments) is true)
419+
{
420+
azureMessage.FunctionCall = new FunctionCall(name, arguments);
421+
}
422+
}
420423

421-
if (validRole != ChatRole.User &&
422-
validRole != ChatRole.System &&
423-
validRole != ChatRole.Assistant)
424-
{
425-
throw new ArgumentException($"Invalid chat message author role: {role}");
424+
options.Messages.Add(azureMessage);
426425
}
427426

428-
return validRole;
427+
return options;
429428
}
430429

431430
private static void ValidateMaxTokens(int? maxTokens)

‎dotnet/src/Connectors/Connectors.AI.OpenAI/AzureSdk/OpenAIFunctionResponse.cs‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Collections.Generic;
55
using System.Text.Json;
66
using Azure.AI.OpenAI;
7+
using Microsoft.SemanticKernel.Text;
78

89
namespace Microsoft.SemanticKernel.Connectors.AI.OpenAI.AzureSdk;
910

@@ -27,6 +28,14 @@ public class OpenAIFunctionResponse
2728
/// </summary>
2829
public Dictionary<string, object> Parameters { get; set; } = new();
2930

31+
/// <summary>
32+
/// Fully qualified name of the function. This is the concatenation of the plugin name and the function name,
33+
/// separated by the value of <see cref="OpenAIFunction.NameSeparator"/>.
34+
/// If there is no plugin name, this is the same as the function name.
35+
/// </summary>
36+
public string FullyQualifiedName =>
37+
this.PluginName.IsNullOrEmpty() ? this.FunctionName : $"{this.PluginName}{OpenAIFunction.NameSeparator}{this.FunctionName}";
38+
3039
/// <summary>
3140
/// Parses the function call and parameter information generated by the model.
3241
/// </summary>
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
3+
#pragma warning disable IDE0130
4+
using System.Collections.Generic;
5+
using Microsoft.SemanticKernel.Text;
6+
7+
namespace Microsoft.SemanticKernel.AI.ChatCompletion;
8+
#pragma warning restore IDE0130
9+
10+
/// <summary>
11+
/// OpenAI-specific extensions to the <see cref="ChatHistory"/> class.
12+
/// </summary>
13+
public static class ChatHistoryExtensions
14+
{
15+
/// <summary>
16+
/// Add a function message to the chat history
17+
/// </summary>
18+
/// <param name="chatHistory">Chat history</param>
19+
/// <param name="message">Message content</param>
20+
/// <param name="functionName">Name of the function that generated the content</param>
21+
public static void AddFunctionMessage(this ChatHistory chatHistory, string message, string functionName)
22+
{
23+
chatHistory.AddMessage(AuthorRole.Function, message, new Dictionary<string, string> { { "Name", functionName } });
24+
}
25+
26+
/// <summary>
27+
/// Add an assistant message to the chat history.
28+
/// </summary>
29+
/// <param name="chatHistory">Chat history</param>
30+
/// <param name="chatResult">Chat result from the model</param>
31+
public static void AddAssistantMessage(this ChatHistory chatHistory, IChatResult chatResult)
32+
{
33+
var chatMessage = chatResult.ModelResult.GetOpenAIChatResult().Choice.Message;
34+
35+
if (!chatMessage.Content.IsNullOrEmpty())
36+
{
37+
chatHistory.AddAssistantMessage(chatMessage.Content);
38+
}
39+
40+
var functionCall = chatMessage.FunctionCall;
41+
if (functionCall is not null)
42+
{
43+
chatHistory.AddMessage(AuthorRole.Assistant, string.Empty, new Dictionary<string, string>
44+
{
45+
{ "Name", functionCall.Name },
46+
{ "Arguments", functionCall.Arguments }
47+
});
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)