-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[.Net]: Introduce ChatCompletionAgent to AutoGen.SemanticKernel packa…
…ge (#2584) * WIP add SKAgent to proj * Fix Unit test * Remove accidental coommit * Add version props * Revert Kludge test changes * PR comments : executionSettings and use / upgrade SemanticKernelExperimentalVersion * Add back deleted api and constructor, mark as Obsolete * PR feedback : Introduce SemanticKernelChatCompletionAgent. Add unit tests and refactor semanticKernelChatMessageContentConnector to be SkSequentialChatMessageContentConnector.cs * Revert SkSequentialChatMessageContentConnector * PR comments, remove systemMessage in SemanticKernelChatCompletionAgent * Fix formatting * Fix bad merge * Revert "Fix bad merge" This reverts commit a189ad9. * Remove accidental commit --------- Co-authored-by: luongdavid <[email protected]>
- Loading branch information
1 parent
5be103a
commit b529fe2
Showing
7 changed files
with
169 additions
and
7 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
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
51 changes: 51 additions & 0 deletions
51
dotnet/src/AutoGen.SemanticKernel/SemanticKernelChatCompletionAgent.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,51 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// SemanticKernelChatCompletionAgent.cs | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.SemanticKernel; | ||
using Microsoft.SemanticKernel.Agents; | ||
using Microsoft.SemanticKernel.ChatCompletion; | ||
|
||
namespace AutoGen.SemanticKernel; | ||
|
||
public class SemanticKernelChatCompletionAgent : IAgent | ||
{ | ||
public string Name { get; } | ||
private readonly ChatCompletionAgent _chatCompletionAgent; | ||
|
||
public SemanticKernelChatCompletionAgent(ChatCompletionAgent chatCompletionAgent) | ||
{ | ||
this.Name = chatCompletionAgent.Name ?? throw new ArgumentNullException(nameof(chatCompletionAgent.Name)); | ||
this._chatCompletionAgent = chatCompletionAgent; | ||
} | ||
|
||
public async Task<IMessage> GenerateReplyAsync(IEnumerable<IMessage> messages, GenerateReplyOptions? options = null, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
ChatMessageContent[] reply = await _chatCompletionAgent | ||
.InvokeAsync(BuildChatHistory(messages), cancellationToken) | ||
.ToArrayAsync(cancellationToken: cancellationToken); | ||
|
||
return reply.Length > 1 | ||
? throw new InvalidOperationException("ResultsPerPrompt greater than 1 is not supported in this semantic kernel agent") | ||
: new MessageEnvelope<ChatMessageContent>(reply[0], from: this.Name); | ||
} | ||
|
||
private ChatHistory BuildChatHistory(IEnumerable<IMessage> messages) | ||
{ | ||
return new ChatHistory(ProcessMessage(messages)); | ||
} | ||
|
||
private IEnumerable<ChatMessageContent> ProcessMessage(IEnumerable<IMessage> messages) | ||
{ | ||
return messages.Select(m => m switch | ||
{ | ||
IMessage<ChatMessageContent> cmc => cmc.Content, | ||
_ => throw new ArgumentException("Invalid message type") | ||
}); | ||
} | ||
} |
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