Skip to content

Commit

Permalink
Remove the IPromptTemplateFactory as a property of KernelAgent
Browse files Browse the repository at this point in the history
  • Loading branch information
markwallace-microsoft committed Feb 20, 2025
1 parent f159c49 commit c3d54dd
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 24 deletions.
8 changes: 5 additions & 3 deletions dotnet/samples/Concepts/Agents/ChatCompletion_Templating.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ await InvokeChatCompletionAgentWithTemplateAsync(
"""
Write a one verse poem on the requested topic in the style of {{$style}}.
Always state the requested style of the poem.
""");
""",
PromptTemplateConfig.SemanticKernelTemplateFormat,
new KernelPromptTemplateFactory());
}

[Fact]
Expand Down Expand Up @@ -79,8 +81,8 @@ Always state the requested style of the poem.

private async Task InvokeChatCompletionAgentWithTemplateAsync(
string instructionTemplate,
string? templateFormat = null,
IPromptTemplateFactory? templateFactory = null)
string templateFormat,
IPromptTemplateFactory templateFactory)
{
// Define the agent
PromptTemplateConfig templateConfig =
Expand Down
4 changes: 3 additions & 1 deletion dotnet/samples/Concepts/Agents/OpenAIAssistant_Templating.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ await InvokeAssistantAgentWithTemplateAsync(
"""
Write a one verse poem on the requested topic in the styles of {{$style}}.
Always state the requested style of the poem.
""");
""",
PromptTemplateConfig.SemanticKernelTemplateFormat,
new KernelPromptTemplateFactory());
}

[Fact]
Expand Down
15 changes: 0 additions & 15 deletions dotnet/src/Agents/Abstractions/KernelAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,6 @@ public abstract class KernelAgent : Agent
/// <inheritdoc/>
protected override ILoggerFactory ActiveLoggerFactory => this.LoggerFactory ?? this.Kernel.LoggerFactory;

/// <summary>
/// Gets or sets a prompt template factory on the agent instructions.
/// </summary>
/// <remarks>
/// If provided, will treat the instructions as a prompt template and will render using this factory.
/// </remarks>
public IPromptTemplateFactory? TemplateFactory { get; protected set; }

/// <summary>
/// Formats the system instructions for the agent.
/// </summary>
Expand All @@ -58,13 +50,6 @@ public abstract class KernelAgent : Agent
/// <returns>The formatted system instructions for the agent.</returns>
protected async Task<string?> FormatInstructionsAsync(Kernel kernel, KernelArguments? arguments, CancellationToken cancellationToken)
{
// Use the provided template factory to format the instructions
if (this.TemplateFactory is not null && !string.IsNullOrEmpty(this.Instructions))
{
var template = this.TemplateFactory.Create(new PromptTemplateConfig(this.Instructions!));
return await template.RenderAsync(kernel, arguments, cancellationToken).ConfigureAwait(false);
}

// Use the provided template as the instructions
if (this.Template is not null)
{
Expand Down
7 changes: 2 additions & 5 deletions dotnet/src/Agents/Core/ChatCompletionAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,15 @@ public ChatCompletionAgent() { }
/// </summary>
/// <param name="templateConfig">The prompt template configuration.</param>
/// <param name="templateFactory">An optional factory to produce the <see cref="IPromptTemplate"/> for the agent.</param>
/// <remarks>
/// When a template factory argument isn't provided, the default <see cref="KernelPromptTemplateFactory"/> is used.
/// </remarks>
public ChatCompletionAgent(
PromptTemplateConfig templateConfig,
IPromptTemplateFactory? templateFactory = null)
IPromptTemplateFactory templateFactory)
{
this.Name = templateConfig.Name;
this.Description = templateConfig.Description;
this.Instructions = templateConfig.Template;
this.Arguments = new(templateConfig.ExecutionSettings.Values);
this.Template = templateFactory?.Create(templateConfig);
this.Template = templateFactory.Create(templateConfig);
}

/// <inheritdoc/>
Expand Down

0 comments on commit c3d54dd

Please sign in to comment.