last ...
This commit is contained in:
@@ -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
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Get Template Params Filled Prompt ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="source"></param>
|
||||||
|
/// <param name="params"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static string GetMessage(
|
||||||
|
this XPromptDto source,
|
||||||
|
IDictionary<string, string> @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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Holds Initialized Prompts ...
|
||||||
|
/// </summary>
|
||||||
|
private static XPromptsDto prompts;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initialize Prompt Helper ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="path"></param>
|
||||||
|
/// <exception cref="Exception"></exception>
|
||||||
|
/// <exception cref="InvalidDataException"></exception>
|
||||||
|
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<XPromptsDto>(json, options);
|
||||||
|
if (!root.IsNullOrDefault())
|
||||||
|
{
|
||||||
|
prompts = new XPromptsDto { Prompts = root.Prompts };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Retrieve all Allowed Prompts as Enumerable ...
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static IEnumerable<XPromptDto> GetPrompts()
|
||||||
|
{
|
||||||
|
return prompts!.Prompts.AsEnumerable();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Get Specified Prompt ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="name"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
/// <exception cref="Exception"></exception>
|
||||||
|
public static XPromptDto GetPrompt(string name)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
return GetPrompts()
|
||||||
|
.FirstOrDefault(p => p.Name == name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,57 @@
|
|||||||
|
namespace xAiService.Models.Dtos
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Described Prompts in app ...
|
||||||
|
/// </summary>
|
||||||
|
public class XPromptsDto
|
||||||
|
{
|
||||||
|
public XPromptDto[] Prompts { get; set; } = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// a Single Prompt Description ...
|
||||||
|
/// </summary>
|
||||||
|
public class XPromptDto
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Prompt Name ...
|
||||||
|
/// </summary>
|
||||||
|
public string Name { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Prompt Template Message ...
|
||||||
|
/// </summary>
|
||||||
|
public string Prompt { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Prompt Description ...
|
||||||
|
/// </summary>
|
||||||
|
public string Description { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Describe Prompt Params ...
|
||||||
|
/// </summary>
|
||||||
|
public virtual XPromptParamDto[] Params { get; set; } = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Prompt Param used to Fill dynamic Datas to Specified Prompts ...
|
||||||
|
/// </summary>
|
||||||
|
public class XPromptParamDto
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Param Name ...
|
||||||
|
/// </summary>
|
||||||
|
public string Name { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Parma Identifier ...
|
||||||
|
/// </summary>
|
||||||
|
public string Param { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Param Description ...
|
||||||
|
/// </summary>
|
||||||
|
public string Description { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user