forked from microsoft/semantic-kernel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
.Net: Function calling: Add function author role and name property (m…
…icrosoft#3421) ### Motivation and Context Replaces microsoft#3302 Resolves microsoft#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]>
- Loading branch information
1 parent
dd38e34
commit e83570d
Showing
8 changed files
with
348 additions
and
56 deletions.
There are no files selected for viewing
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
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
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
50 changes: 50 additions & 0 deletions
50
dotnet/src/Connectors/Connectors.AI.OpenAI/ChatCompletion/ChatHistoryExtensions.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,50 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
#pragma warning disable IDE0130 | ||
using System.Collections.Generic; | ||
using Microsoft.SemanticKernel.Text; | ||
|
||
namespace Microsoft.SemanticKernel.AI.ChatCompletion; | ||
#pragma warning restore IDE0130 | ||
|
||
/// <summary> | ||
/// OpenAI-specific extensions to the <see cref="ChatHistory"/> class. | ||
/// </summary> | ||
public static class ChatHistoryExtensions | ||
{ | ||
/// <summary> | ||
/// Add a function message to the chat history | ||
/// </summary> | ||
/// <param name="chatHistory">Chat history</param> | ||
/// <param name="message">Message content</param> | ||
/// <param name="functionName">Name of the function that generated the content</param> | ||
public static void AddFunctionMessage(this ChatHistory chatHistory, string message, string functionName) | ||
{ | ||
chatHistory.AddMessage(AuthorRole.Function, message, new Dictionary<string, string> { { "Name", functionName } }); | ||
} | ||
|
||
/// <summary> | ||
/// Add an assistant message to the chat history. | ||
/// </summary> | ||
/// <param name="chatHistory">Chat history</param> | ||
/// <param name="chatResult">Chat result from the model</param> | ||
public static void AddAssistantMessage(this ChatHistory chatHistory, IChatResult chatResult) | ||
{ | ||
var chatMessage = chatResult.ModelResult.GetOpenAIChatResult().Choice.Message; | ||
|
||
if (!chatMessage.Content.IsNullOrEmpty()) | ||
{ | ||
chatHistory.AddAssistantMessage(chatMessage.Content); | ||
} | ||
|
||
var functionCall = chatMessage.FunctionCall; | ||
if (functionCall is not null) | ||
{ | ||
chatHistory.AddMessage(AuthorRole.Assistant, string.Empty, new Dictionary<string, string> | ||
{ | ||
{ "Name", functionCall.Name }, | ||
{ "Arguments", functionCall.Arguments } | ||
}); | ||
} | ||
} | ||
} |
Oops, something went wrong.