Skip to content

Commit

Permalink
.Net Agents - Support role-override for ChatCompletionAgent (#10601)
Browse files Browse the repository at this point in the history
### Motivation and Context
<!-- Thank you for your contribution to the semantic-kernel repo!
Please help reviewers and future users, providing the following
information:
  1. Why is this change required?
  2. What problem does it solve?
  3. What scenario does it contribute to?
  4. If it fixes an open issue, please link to the issue here.
-->

The "O*" series deep-reasoning models from OpenAI do not support
`System` role. For certain versions, support was added for a `Developer`
role.

### Description
<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->

Allow `ChatCompletionAgent` make use of "O*" series models by supporting
`InstructionsRole` that may be optionally overriden.

### 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 😄
  • Loading branch information
crickman authored Feb 19, 2025
1 parent b6e4e66 commit a9b20b1
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion dotnet/src/Agents/Core/ChatCompletionAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ public ChatCompletionAgent(
this.Template = templateFactory?.Create(templateConfig);
}

/// <summary>
/// Gets the role used for the agent instructions. Defaults to "system".
/// </summary>
/// <remarks>
/// Certain versions of "O*" series (deep reasoning) models require the instructions
/// to be provided as "developer" role. Other versions support neither role and
/// an agent targeting such a model cannot provide instructions. Agent functionality
/// will be dictated entirely by the provided plugins.
/// </remarks>
public AuthorRole InstructionsRole { get; init; } = AuthorRole.System;

/// <inheritdoc/>
public override IAsyncEnumerable<ChatMessageContent> InvokeAsync(
ChatHistory history,
Expand Down Expand Up @@ -113,7 +124,7 @@ private async Task<ChatHistory> SetupAgentChatHistoryAsync(

if (!string.IsNullOrWhiteSpace(instructions))
{
chat.Add(new ChatMessageContent(AuthorRole.System, instructions) { AuthorName = this.Name });
chat.Add(new ChatMessageContent(this.InstructionsRole, instructions) { AuthorName = this.Name });
}

chat.AddRange(history);
Expand Down

0 comments on commit a9b20b1

Please sign in to comment.