83 lines
2.5 KiB
C#
83 lines
2.5 KiB
C#
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);
|
|
}
|
|
}
|
|
} |