Cleanup xAiService for Reconstruction ...
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
using System;
|
||||
using xCommons.Extensions;
|
||||
|
||||
namespace xAiService.Helpers
|
||||
{
|
||||
public static class VectorHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// Normalize a Vector ...
|
||||
/// </summary>
|
||||
/// <param name="vector"></param>
|
||||
public static void Normalize(float[] vector)
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
if (vector.IsNull() || !vector.HasChild())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
//
|
||||
float sum = 0f;
|
||||
foreach (var value in vector)
|
||||
{
|
||||
sum += value * value;
|
||||
}
|
||||
|
||||
//
|
||||
float norm = MathF.Sqrt(sum);
|
||||
if (norm <= 1e-9f)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
//
|
||||
for (int i = 0; i < vector.Length; i++)
|
||||
{
|
||||
vector[i] /= norm;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Similarity Check ...
|
||||
/// </summary>
|
||||
/// <param name="a"></param>
|
||||
/// <param name="b"></param>
|
||||
/// <returns></returns>
|
||||
public static float CosineSimilarity(float[] a, float[] b)
|
||||
{
|
||||
//
|
||||
if (a.IsNull() || b.IsNull())
|
||||
{
|
||||
return 0f;
|
||||
}
|
||||
|
||||
//
|
||||
if (a.Length != b.Length)
|
||||
{
|
||||
return 0f;
|
||||
}
|
||||
|
||||
//
|
||||
float dot = 0f;
|
||||
float normA = 0f;
|
||||
float normB = 0f;
|
||||
for (int i = 0; i < a.Length; i++)
|
||||
{
|
||||
//
|
||||
dot += a[i] * b[i];
|
||||
normA += a[i] * a[i];
|
||||
normB += b[i] * b[i];
|
||||
}
|
||||
|
||||
//
|
||||
float denominator = MathF.Sqrt(normA) * MathF.Sqrt(normB);
|
||||
if (denominator <= 1e-9f)
|
||||
{
|
||||
return 0f;
|
||||
}
|
||||
|
||||
//
|
||||
var result = dot / denominator;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,83 +0,0 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user