last ...
This commit is contained in:
@@ -8,5 +8,10 @@ namespace xAiApi.Configurations
|
||||
/// Allowed Models to Use ...
|
||||
/// </summary>
|
||||
public XAiModelDescriptor[] Models { get; set; } = [];
|
||||
|
||||
/// <summary>
|
||||
/// Allowed Prompts to Use ...
|
||||
/// </summary>
|
||||
public XAiPromptDescriptor[] Prompts { get; set; } = [];
|
||||
}
|
||||
}
|
||||
@@ -6,5 +6,8 @@ namespace xAiApi.Constants
|
||||
// Database Descriptor ...
|
||||
public const string XAiApiDbIdentifier = "xAiApiDb";
|
||||
public const string XAiApiMigrationsAssemblyIdentifier = "xAiApi";
|
||||
|
||||
//
|
||||
public const string XAiApiIntroductionPromptName = "Introduction";
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Validate Specified Param ...
|
||||
/// </summary>
|
||||
/// <param name="source"></param>
|
||||
/// <returns></returns>
|
||||
public static bool IsValid(this XAiPromptParam source)
|
||||
{
|
||||
//
|
||||
var result =
|
||||
!source.IsNullOrDefault() &&
|
||||
!source.Name.IsNullOrEmpty() &&
|
||||
!source.Param.IsNullOrEmpty();
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Validate Specified Prompt ...
|
||||
/// </summary>
|
||||
/// <param name="source"></param>
|
||||
/// <returns></returns>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieve Introduction Prompt ...
|
||||
/// </summary>
|
||||
/// <param name="source"></param>
|
||||
/// <returns></returns>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Select Specified Prompts and Parse it's Template
|
||||
/// and Generate Message ...
|
||||
/// </summary>
|
||||
/// <param name="source"></param>
|
||||
/// <param name="name"></param>
|
||||
/// <param name="params"></param>
|
||||
/// <returns></returns>
|
||||
public static string GetPrompt(
|
||||
this XAiApiConfiguration source,
|
||||
string name,
|
||||
IDictionary<string, string> @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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
namespace xAiApi.Models
|
||||
{
|
||||
/// <summary>
|
||||
/// Describe Specified Prompt ...
|
||||
/// </summary>
|
||||
public class XAiPromptDescriptor
|
||||
{
|
||||
/// <summary>
|
||||
/// Prompt Name ...
|
||||
/// </summary>
|
||||
public string Name { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Prompt Template ...
|
||||
/// </summary>
|
||||
public string Template { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Prompt Description ...
|
||||
/// </summary>
|
||||
public string Description { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Prompt Template Params ...
|
||||
/// </summary>
|
||||
public XAiPromptParam[] Params { get; set; } = [];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Specified a Prompt Param ...
|
||||
/// </summary>
|
||||
public class XAiPromptParam
|
||||
{
|
||||
/// <summary>
|
||||
/// Param Name ...
|
||||
/// </summary>
|
||||
public string Name { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Param Identifier ...
|
||||
/// </summary>
|
||||
public string Param { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Parma Description ...
|
||||
/// </summary>
|
||||
public string Description { get; set; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user