-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Regex capabilities to templating service
- Loading branch information
1 parent
7c987b6
commit 2780080
Showing
14 changed files
with
209 additions
and
71 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
13 changes: 0 additions & 13 deletions
13
src/dotnet/Common/Constants/ResourceProviders/PromptTokens.cs
This file was deleted.
Oops, something went wrong.
13 changes: 13 additions & 0 deletions
13
src/dotnet/Common/Constants/Templates/TemplateVariables.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,13 @@ | ||
namespace FoundationaLLM.Common.Constants.Templates | ||
{ | ||
/// <summary> | ||
/// Provides template variables that can be used in templates. | ||
/// </summary> | ||
public static class TemplateVariables | ||
{ | ||
/// <summary> | ||
/// Token for current date in UTC format. | ||
/// </summary> | ||
public const string CurrentDateTimeUTC = "current_datetime_utc"; | ||
} | ||
} |
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,15 @@ | ||
namespace FoundationaLLM.Common.Interfaces | ||
{ | ||
/// <summary> | ||
/// Defines the interface for a templating engine. | ||
/// </summary> | ||
public interface ITemplatingService | ||
{ | ||
/// <summary> | ||
/// Transforms the input string by replacing tokens with the corresponding values. | ||
/// </summary> | ||
/// <param name="s">The input string to be transformed.</param> | ||
/// <returns>The transformed string where all the valid tokens have been replaced.</returns> | ||
string Transform(string s); | ||
} | ||
} |
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
78 changes: 78 additions & 0 deletions
78
src/dotnet/Common/Services/Templates/RegexTemplatingService.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,78 @@ | ||
using FoundationaLLM.Common.Constants.Templates; | ||
using FoundationaLLM.Common.Interfaces; | ||
using Microsoft.Extensions.Logging; | ||
using System.Text.RegularExpressions; | ||
|
||
namespace FoundationaLLM.Common.Services.Templates | ||
{ | ||
/// <summary> | ||
/// Templating engine that uses regular expressions to replace tokens in strings. | ||
/// </summary> | ||
/// <param name="logger">The logger used for logging.</param> | ||
public partial class RegexTemplatingService( | ||
ILogger<RegexTemplatingService> logger) : ITemplatingService | ||
{ | ||
/// <summary> | ||
/// Regular expression pattern for template variables. | ||
/// </summary> | ||
private const string REGEX_VARIABLE_PATTERN = "\\{\\{foundationallm:(.*?)\\}\\}"; | ||
|
||
private readonly ILogger<RegexTemplatingService> _logger = logger; | ||
|
||
[GeneratedRegex(REGEX_VARIABLE_PATTERN, RegexOptions.Compiled)] | ||
private static partial Regex VariableRegex(); | ||
|
||
/// <inheritdoc/> | ||
public string Transform(string s) | ||
{ | ||
if (string.IsNullOrWhiteSpace(s)) | ||
{ | ||
return string.Empty; | ||
} | ||
|
||
try | ||
{ | ||
// Expects the format {{foundationallm:variable_name[:format]}} | ||
|
||
var matches = VariableRegex().Matches(s); | ||
Dictionary<string, string> replacements = []; | ||
|
||
foreach (Match match in matches) | ||
{ | ||
var matchedVariable = match.Value; | ||
|
||
var variableTokens = match.Groups[1].Value.Split(":", 2); | ||
var variableName = variableTokens[0]; | ||
var variableFormat = variableTokens.Length > 1 ? variableTokens[1] : null; | ||
|
||
switch (variableName) | ||
{ | ||
case TemplateVariables.CurrentDateTimeUTC: | ||
replacements.Add( | ||
matchedVariable, | ||
string.IsNullOrWhiteSpace(variableFormat) | ||
? DateTime.UtcNow.ToString() | ||
: DateTime.UtcNow.ToString(variableFormat)); | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
|
||
var transformedString = s; | ||
foreach (var replacement in replacements) | ||
{ | ||
transformedString = transformedString.Replace(replacement.Key, replacement.Value); | ||
} | ||
|
||
return transformedString; | ||
} | ||
catch (Exception ex) | ||
{ | ||
_logger.LogError(ex, "Error occurred while transforming the string."); | ||
} | ||
|
||
return s; | ||
} | ||
} | ||
} |
29 changes: 0 additions & 29 deletions
29
src/dotnet/Common/Services/TokenReplacement/TokenReplacementEngine.cs
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.