-
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.
Initial version of prompt token replacements
- Loading branch information
1 parent
31f3b6a
commit be48a79
Showing
5 changed files
with
111 additions
and
1 deletion.
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
22 changes: 22 additions & 0 deletions
22
src/dotnet/Common/Models/ResourceProviders/Prompt/TokenReplacementDefinition.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,22 @@ | ||
using System.Text.Json.Serialization; | ||
|
||
namespace FoundationaLLM.Common.Models.ResourceProviders.Prompt | ||
{ | ||
/// <summary> | ||
/// String text token replacement definition. | ||
/// </summary> | ||
public class TokenReplacementDefinition | ||
{ | ||
/// <summary> | ||
/// The token to be replaced. | ||
/// </summary> | ||
[JsonPropertyName("token")] | ||
public required string Token { get; set; } | ||
|
||
/// <summary> | ||
/// The code to compute the replacement value. | ||
/// </summary> | ||
[JsonPropertyName("compute_code")] | ||
public required string ComputeCode { get; set; } | ||
} | ||
} |
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
68 changes: 68 additions & 0 deletions
68
src/dotnet/Orchestration/Services/TokenReplacement/TokenReplacementEngine.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,68 @@ | ||
using System.Text.RegularExpressions; | ||
using FoundationaLLM.Common.Models.ResourceProviders.Prompt; | ||
using Microsoft.CodeAnalysis.CSharp.Scripting; | ||
using Microsoft.CodeAnalysis.Scripting; | ||
|
||
namespace FoundationaLLM.Orchestration.Core.Services.TokenReplacement | ||
{ | ||
/// <summary> | ||
/// Token replacement engine, responsible for replacing tokens in a string with their computed values. | ||
/// </summary> | ||
public class TokenReplacementEngine | ||
{ | ||
private readonly List<TokenReplacementDefinition> _tokenReplacements; | ||
private readonly ScriptOptions _scriptOptions; | ||
|
||
/// <summary> | ||
/// Creates an instance of the <see cref="TokenReplacementEngine"/> class. | ||
/// </summary> | ||
/// <param name="tokenReplacements"></param> | ||
public TokenReplacementEngine(List<TokenReplacementDefinition> tokenReplacements) | ||
{ | ||
_tokenReplacements = tokenReplacements; | ||
|
||
// Define script options, such as referencing necessary assemblies and namespaces | ||
_scriptOptions = ScriptOptions.Default | ||
.AddImports("System"); | ||
} | ||
|
||
/// <summary> | ||
/// Replaces tokens in the input string with their corresponding computed values. | ||
/// </summary> | ||
/// <param name="input">The input string containing tokens to be replaced.</param> | ||
/// <returns>A task that represents the asynchronous operation. The task result contains the string with tokens replaced.</returns> | ||
public async Task<string> ReplaceTokensAsync(string input) | ||
{ | ||
if (string.IsNullOrWhiteSpace(input)) | ||
{ | ||
return input; | ||
} | ||
string pattern = @"{{\s*(\w+)\s*}}"; | ||
return await Task.Run(() => Regex.Replace(input, pattern, match => | ||
{ | ||
string tokenName = match.Groups[1].Value; | ||
var tokenReplacement = _tokenReplacements.Find(tr => tr.Token == $"{{{{{tokenName}}}}}"); | ||
|
||
if (tokenReplacement != null) | ||
{ | ||
try | ||
{ | ||
// Evaluate the compute code | ||
var result = CSharpScript.EvaluateAsync<string>( | ||
tokenReplacement.ComputeCode, | ||
_scriptOptions).Result; | ||
return result; | ||
} | ||
catch (Exception ex) | ||
{ | ||
// Handle errors in compute code | ||
return $"[Error: {ex.Message}]"; | ||
} | ||
} | ||
|
||
// If token not found, return it unchanged or handle as needed | ||
return match.Value; | ||
}, RegexOptions.IgnoreCase)); | ||
} | ||
} | ||
} |