From 561542ea0a79e72bb02fe9eaf411de51a0fde63e Mon Sep 17 00:00:00 2001 From: Hadi Khazaee Asl Date: Sat, 15 Aug 2026 00:29:01 +0330 Subject: [PATCH] last ... --- Configurations/XAiApiConfiguration.cs | 5 + Constants/XAiApiConstants.cs | 3 + Extensions/XModelsExtensions.cs | 146 +++++++++++++++++++++++++- Models/XAiPromptDescriptor.cs | 49 +++++++++ 4 files changed, 202 insertions(+), 1 deletion(-) create mode 100644 Models/XAiPromptDescriptor.cs diff --git a/Configurations/XAiApiConfiguration.cs b/Configurations/XAiApiConfiguration.cs index 90d738e..4856014 100644 --- a/Configurations/XAiApiConfiguration.cs +++ b/Configurations/XAiApiConfiguration.cs @@ -8,5 +8,10 @@ namespace xAiApi.Configurations /// Allowed Models to Use ... /// public XAiModelDescriptor[] Models { get; set; } = []; + + /// + /// Allowed Prompts to Use ... + /// + public XAiPromptDescriptor[] Prompts { get; set; } = []; } } \ No newline at end of file diff --git a/Constants/XAiApiConstants.cs b/Constants/XAiApiConstants.cs index 8672c95..3dc5c59 100644 --- a/Constants/XAiApiConstants.cs +++ b/Constants/XAiApiConstants.cs @@ -6,5 +6,8 @@ namespace xAiApi.Constants // Database Descriptor ... public const string XAiApiDbIdentifier = "xAiApiDb"; public const string XAiApiMigrationsAssemblyIdentifier = "xAiApi"; + + // + public const string XAiApiIntroductionPromptName = "Introduction"; } } \ No newline at end of file diff --git a/Extensions/XModelsExtensions.cs b/Extensions/XModelsExtensions.cs index 45212d7..779ea3f 100644 --- a/Extensions/XModelsExtensions.cs +++ b/Extensions/XModelsExtensions.cs @@ -1,5 +1,7 @@ +using System.Collections.Generic; using System.Linq; using xAiApi.Configurations; +using xAiApi.Constants; using xAiApi.Models; using xCommons.Extensions; @@ -38,7 +40,58 @@ namespace xAiApi.Extensions var result = !source.IsNullOrDefault() && source.Models.HasChild() && - source.Models.All(m => m.IsValid()); + source.Models.All(m => m.IsValid()) && + source.Prompts.All(m => m.IsValid()) && + source.Prompts.Any(p => p.Name + .Equals( + XAiApiConstants.XAiApiIntroductionPromptName, + System.StringComparison.InvariantCultureIgnoreCase)); + + // + return result; + } + + /// + /// Validate Specified Param ... + /// + /// + /// + public static bool IsValid(this XAiPromptParam source) + { + // + var result = + !source.IsNullOrDefault() && + !source.Name.IsNullOrEmpty() && + !source.Param.IsNullOrEmpty(); + + // + return result; + } + + /// + /// Validate Specified Prompt ... + /// + /// + /// + public static bool IsValid(this XAiPromptDescriptor source) + { + // + var result = + !source.IsNullOrDefault() && + !source.Name.IsNullOrEmpty() && + !source.Template.IsNullOrEmpty(); + if (!result) + { + return result; + } + + // + if (source.Params.HasChild()) + { + result = + result && + source.Params.All(x => x.IsValid()); + } // return result; @@ -97,5 +150,96 @@ namespace xAiApi.Extensions // return result; } + + /// + /// Retrieve Introduction Prompt ... + /// + /// + /// + public static XAiPromptDescriptor GetIntroductionPrompt(this XAiApiConfiguration source) + { + // + XAiPromptDescriptor result = null; + + // + if (source.IsValid()) + { + // + result = source.Prompts + .FirstOrDefault(p => p.Name + .Equals( + XAiApiConstants.XAiApiIntroductionPromptName, + System.StringComparison.InvariantCultureIgnoreCase)); + } + + // + return result; + } + + /// + /// Select Specified Prompts and Parse it's Template + /// and Generate Message ... + /// + /// + /// + /// + /// + public static string GetPrompt( + this XAiApiConfiguration source, + string name, + IDictionary @params = null + ) + { + // + var result = string.Empty; + + // + // Normalize ... + if (name.IsNullOrEmpty()) + { + name = XAiApiConstants.XAiApiIntroductionPromptName; + } + + // + if (source.IsValid()) + { + // + var prompt = source.Prompts + .FirstOrDefault(p => p.Name + .Equals( + name, + System.StringComparison.InvariantCultureIgnoreCase + ) + ); + if (!prompt.IsNullOrDefault()) + { + // + result = prompt.Template; + + // + // Handle Params ... + if (!@params.IsNullOrDefault() && @params.HasChild()) + { + // + // Select Params ... + var existsParams = prompt.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/Models/XAiPromptDescriptor.cs b/Models/XAiPromptDescriptor.cs new file mode 100644 index 0000000..0636289 --- /dev/null +++ b/Models/XAiPromptDescriptor.cs @@ -0,0 +1,49 @@ +namespace xAiApi.Models +{ + /// + /// Describe Specified Prompt ... + /// + public class XAiPromptDescriptor + { + /// + /// Prompt Name ... + /// + public string Name { get; set; } + + /// + /// Prompt Template ... + /// + public string Template { get; set; } + + /// + /// Prompt Description ... + /// + public string Description { get; set; } + + /// + /// Prompt Template Params ... + /// + public XAiPromptParam[] Params { get; set; } = []; + } + + /// + /// Specified a Prompt Param ... + /// + public class XAiPromptParam + { + /// + /// Param Name ... + /// + public string Name { get; set; } + + /// + /// Param Identifier ... + /// + public string Param { get; set; } + + /// + /// Parma Description ... + /// + public string Description { get; set; } + } +} \ No newline at end of file