last ...
This commit is contained in:
@@ -0,0 +1,173 @@
|
||||
|
||||
### FILE: C:\Users\hkhazaeeasl\Documents\Projets\XProjects\xSaherelmWorkspace\Modules\xAiService\Constants\ConfigurationNodeNames.cs
|
||||
|
||||
namespace xAiService.Constants
|
||||
{
|
||||
public partial struct ConfigurationNodeNames
|
||||
{
|
||||
public const string AI_SERVICE_NODE = "AIServiceConfiguration";
|
||||
}
|
||||
}
|
||||
|
||||
### FILE: C:\Users\hkhazaeeasl\Documents\Projets\XProjects\xSaherelmWorkspace\Modules\xAiService\Constants\XAiServiceConstants.cs
|
||||
|
||||
namespace xAiService.Constants
|
||||
{
|
||||
public struct XAiServiceConstants
|
||||
{
|
||||
//
|
||||
// Service Extensions Log Tag ...
|
||||
public const string XAiServiceDILogTag = "XAiService";
|
||||
|
||||
//
|
||||
// File Provider Identifiers ...
|
||||
public const string XAiProjectFilesIdentifier = "AiPrjF";
|
||||
public const string XAiMessageFilesIdentifier = "AiMsgF";
|
||||
public const string XMetaDataFileIdsIdentifier = "FileIds";
|
||||
public const string XAiConversationFilesIdentifier = "AiCnvrF";
|
||||
}
|
||||
}
|
||||
|
||||
### FILE: C:\Users\hkhazaeeasl\Documents\Projets\XProjects\xSaherelmWorkspace\Modules\xAiService\DI\XDIHelperExtension.cs
|
||||
|
||||
namespace xAiService.DI
|
||||
{
|
||||
public static partial class XDIHelperExtension
|
||||
{ }
|
||||
}
|
||||
|
||||
### FILE: C:\Users\hkhazaeeasl\Documents\Projets\XProjects\xSaherelmWorkspace\Modules\xAiService\Extensions\XAiServiceExtensions.cs
|
||||
|
||||
using xCommons.Extensions;
|
||||
using xExceptions.Constants;
|
||||
using xIdentityModels.Models;
|
||||
|
||||
namespace xAiService.Extensions
|
||||
{
|
||||
public static class XAiServiceExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Validate User Info ...
|
||||
/// </summary>
|
||||
/// <param name="source"></param>
|
||||
/// <returns></returns>
|
||||
public static bool IsValid(this XUserClaimsInfoDto source)
|
||||
{
|
||||
//
|
||||
var result =
|
||||
!source.IsNullOrDefault() &&
|
||||
!source.UserId.IsNullOrEmpty() &&
|
||||
!source.AccessToken.IsNullOrEmpty();
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Validate User Info ...
|
||||
/// </summary>
|
||||
/// <param name="source"></param>
|
||||
/// <param name="invalidException"></param>
|
||||
public static void Validate(
|
||||
this XUserClaimsInfoDto source,
|
||||
XException invalidException = XException.InvalidArgs
|
||||
)
|
||||
{
|
||||
//
|
||||
var isValid = source.IsValid();
|
||||
if (!isValid)
|
||||
{
|
||||
invalidException.Throw();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
### FILE: C:\Users\hkhazaeeasl\Documents\Projets\XProjects\xSaherelmWorkspace\Modules\xAiService\Helpers\VectorHelper.cs
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user