Skip to content

Commit

Permalink
Allow a template factory to be specified to allow instructions for in…
Browse files Browse the repository at this point in the history
…clude template directives
  • Loading branch information
markwallace-microsoft committed Feb 18, 2025
1 parent dd29544 commit 45eebf5
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions dotnet/src/Agents/Abstractions/KernelAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ public abstract class KernelAgent : Agent
/// </summary>
public IPromptTemplate? Template { get; protected set; }

/// <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 @@ -46,13 +54,21 @@ 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)
{
// If <see cref="Template"/> is not set, use the default instructions.
if (this.Template == null)
// Use the provide 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)
{
return Task.FromResult<string?>(this.Instructions).Result;
return await this.Template.RenderAsync(kernel, arguments, cancellationToken).ConfigureAwait(false);
}

return await this.Template.RenderAsync(kernel, arguments, cancellationToken).ConfigureAwait(false);
// Use the instructions as-is
return Task.FromResult<string?>(this.Instructions).Result;
}

/// <summary>
Expand Down

0 comments on commit 45eebf5

Please sign in to comment.