using System.Collections.Generic; using System.Linq; using xAiApi.Configurations; using xAiApi.Constants; using xAiApi.Models; using xCommons.Extensions; namespace xAiApi.Extensions { public static class XModelsExtensions { /// /// Validate a Model Descriptor ... /// /// /// public static bool IsValid(this XAiModelDescriptor source) { // var result = !source.IsNullOrDefault() && !source.Name.IsNullOrEmpty() && !source.Url.IsNullOrEmpty() && source.Url.IsValidUrl() && !source.LLM.IsNullOrEmpty() && source.Provider != Constants.XAiProvider.None; // return result; } /// /// Validate Configurations ... /// /// /// public static bool IsValid(this XAiApiConfiguration source) { // var result = !source.IsNullOrDefault() && source.Models.HasChild() && 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; } /// /// Extract Default Model Descriptor ... /// /// /// public static XAiModelDescriptor GetDefaultModel(this XAiApiConfiguration source) { // XAiModelDescriptor result = null; // if (source.IsValid()) { result = source.Models[0]; } // return result; } /// /// Retrieve Specified Model from Config ... /// /// /// /// public static XAiModelDescriptor GetModel( this XAiApiConfiguration source, string name ) { // XAiModelDescriptor result = null; // if (source.IsValid()) { // if (name.IsNullOrEmpty()) { result = source.GetDefaultModel(); } else { result = source.Models .FirstOrDefault(m => m.Name .Equals(name, System.StringComparison.InvariantCultureIgnoreCase)); } } // 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; } } }