diff --git a/Configurations/.gitkeep b/Configurations/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/Constants/.gitkeep b/Constants/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/DI/.gitkeep b/DI/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/Extensions/.gitkeep b/Extensions/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/Extensions/ModelExtensions.cs b/Extensions/ConfigurationExtensions.cs similarity index 100% rename from Extensions/ModelExtensions.cs rename to Extensions/ConfigurationExtensions.cs diff --git a/Extensions/PromptExtensions.cs b/Extensions/PromptExtensions.cs new file mode 100644 index 0000000..176f48a --- /dev/null +++ b/Extensions/PromptExtensions.cs @@ -0,0 +1,60 @@ +using System.Collections.Generic; +using System.Linq; +using xAiService.Models.Dtos; +using xCommons.Extensions; + +namespace xAiService.Extensions +{ + public static class PromptExtensions + { + /// + /// Get Template Params Filled Prompt ... + /// + /// + /// + /// + public static string GetMessage( + this XPromptDto source, + IDictionary @params = null + ) + { + // + var result = string.Empty; + + // + if (!source.IsNullOrDefault() && + !source.Prompt.IsNullOrEmpty() + ) + { + result = source.Prompt; + } + + // + // Check Params Conditions ... + if (!source.Params.IsNullOrDefault() && + source.Params.HasChild() && + !@params.IsNullOrDefault() && + @params.HasChild() + ) + { + // + // Select Params ... + var existsParams = source.Params + .Where(p => @params.Any(pr => pr.Key == p.Name)) + .Distinct() + .ToList(); + + // + // Replace Selected Params ... + existsParams + .ForEach(pk => + { + result = result.Replace(pk.Param, @params[pk.Name]); + }); + } + + // + return result; + } + } +} \ No newline at end of file diff --git a/Helpers/XPromptHelper.cs b/Helpers/XPromptHelper.cs new file mode 100644 index 0000000..e5b6f8e --- /dev/null +++ b/Helpers/XPromptHelper.cs @@ -0,0 +1,83 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text.Json; +using System.Text.Json.Serialization; +using xAiService.Models.Dtos; +using xCommons.Extensions; + +namespace xAiService.Helpers +{ + public static class XPromptHelper + { + /// + /// Holds Initialized Prompts ... + /// + private static XPromptsDto prompts; + + /// + /// Initialize Prompt Helper ... + /// + /// + /// + /// + public static void Initialize(string path = "prompts.json") + { + // + // Create full Path and Check File Exists ... + var fullPath = Path.Combine(AppContext.BaseDirectory, path); + if (!File.Exists(fullPath)) + { + throw new Exception($"Prompts Resources not found: {fullPath}"); + } + + // + // Read File as Json Text ... + var json = File.ReadAllText(fullPath); + var options = new JsonSerializerOptions + { + AllowTrailingCommas = true, + PropertyNameCaseInsensitive = true, + ReadCommentHandling = JsonCommentHandling.Skip, + Converters = + { + new JsonStringEnumConverter( + namingPolicy: null, + allowIntegerValues: false + ) + } + }; + + // + // Serialized Root Object ... + var root = JsonSerializer.Deserialize(json, options); + if (!root.IsNullOrDefault()) + { + prompts = new XPromptsDto { Prompts = root.Prompts }; + } + } + + /// + /// Retrieve all Allowed Prompts as Enumerable ... + /// + /// + public static IEnumerable GetPrompts() + { + return prompts!.Prompts.AsEnumerable(); + } + + /// + /// Get Specified Prompt ... + /// + /// + /// + /// + public static XPromptDto GetPrompt(string name) + { + // + return GetPrompts() + .FirstOrDefault(p => p.Name == name); + } + } +} \ No newline at end of file diff --git a/Models/Dtos/XPromptsDto.cs b/Models/Dtos/XPromptsDto.cs new file mode 100644 index 0000000..3b38f33 --- /dev/null +++ b/Models/Dtos/XPromptsDto.cs @@ -0,0 +1,57 @@ +namespace xAiService.Models.Dtos +{ + /// + /// Described Prompts in app ... + /// + public class XPromptsDto + { + public XPromptDto[] Prompts { get; set; } = []; + } + + /// + /// a Single Prompt Description ... + /// + public class XPromptDto + { + /// + /// Prompt Name ... + /// + public string Name { get; set; } + + /// + /// Prompt Template Message ... + /// + public string Prompt { get; set; } + + /// + /// Prompt Description ... + /// + public string Description { get; set; } + + /// + /// Describe Prompt Params ... + /// + public virtual XPromptParamDto[] Params { get; set; } = null; + } + + /// + /// Prompt Param used to Fill dynamic Datas to Specified Prompts ... + /// + public class XPromptParamDto + { + /// + /// Param Name ... + /// + public string Name { get; set; } + + /// + /// Parma Identifier ... + /// + public string Param { get; set; } + + /// + /// Param Description ... + /// + public string Description { get; set; } + } +} \ No newline at end of file