Last Refactor Works for Improvement ...

This commit is contained in:
2026-10-29 14:17:11 +03:30
parent 07a3caa98f
commit d3ecc8feea
3 changed files with 38 additions and 38 deletions
+9 -5
View File
@@ -56,6 +56,7 @@ namespace xAiApi.Controllers
[Consumes("multipart/form-data")]
public async Task<ActionResult<XAiKnowledgeIngestResult>> Ingest(
[FromForm] IFormFileCollection files,
[FromForm] string knowledgeType = null,
[FromForm] bool replaceExisting = true,
CancellationToken cancellationToken = default
)
@@ -72,11 +73,14 @@ namespace xAiApi.Controllers
//
var userInfo = await GetUserInfo();
var connectionId = GetConnectionId();
//
var result = await ragService.IngestAsync(
files: files,
ownerId: userInfo.UserId,
userInfo: userInfo,
connectionId: connectionId,
knowledgeType: knowledgeType,
replaceExisting: replaceExisting,
cancellationToken: cancellationToken
);
@@ -123,7 +127,7 @@ namespace xAiApi.Controllers
//
var result = await ragService.AskWithRAGAsync(
request: request,
ownerId: userInfo.UserId,
userInfo: userInfo,
cancellationToken: cancellationToken
);
@@ -180,7 +184,7 @@ namespace xAiApi.Controllers
//
var enumerable = ragService.AskWithRAGAsEnumerable(
request: request,
ownerId: userInfo.UserId,
userInfo: userInfo,
cancellationToken: cancellationToken
);
@@ -242,7 +246,7 @@ namespace xAiApi.Controllers
//
var result = await ragService.LoadDocumentsAsync(
ownerId: userInfo.UserId,
userInfo: userInfo,
cancellationToken: cancellationToken
);
@@ -282,7 +286,7 @@ namespace xAiApi.Controllers
//
var result = await ragService.RemoveDocumentAsync(
fileName: fileName,
ownerId: userInfo.UserId,
userInfo: userInfo,
cancellationToken: cancellationToken
);
+5 -5
View File
@@ -2,9 +2,9 @@ using xAiApi.Models;
using System.Threading;
using xAiModels.Models.Dtos;
using System.Threading.Tasks;
using xIdentityModels.Models;
using Microsoft.AspNetCore.Http;
using System.Collections.Generic;
using xIdentityModels.Models;
namespace xAiApi.Interfaces
{
@@ -83,24 +83,24 @@ namespace xAiApi.Interfaces
/// <summary>
/// List Ingested Documents of Owner ...
/// </summary>
/// <param name="ownerId"></param>
/// <param name="userInfo"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
Task<IList<XAiKnowledgeDocumentInfo>> LoadDocumentsAsync(
string ownerId,
XUserClaimsInfoDto userInfo = null,
CancellationToken cancellationToken = default
);
/// <summary>
/// Remove All Vectors of a Document ...
/// </summary>
/// <param name="ownerId"></param>
/// <param name="fileName"></param>
/// <param name="userInfo"></param>
/// <param name="cancellationToken"></param>
/// <returns>Count of Removed Vectors</returns>
Task<int> RemoveDocumentAsync(
string ownerId,
string fileName,
XUserClaimsInfoDto userInfo = null,
CancellationToken cancellationToken = default
);
+24 -28
View File
@@ -10,13 +10,13 @@ using xAiApi.Configurations;
using xExceptions.Constants;
using xAiModels.Models.Dtos;
using System.Threading.Tasks;
using xIdentityModels.Models;
using xFileService.Interfaces;
using Microsoft.AspNetCore.Http;
using System.Collections.Generic;
using Microsoft.Extensions.Logging;
using xAiApi.Interfaces.Extractors;
using System.Runtime.CompilerServices;
using xIdentityModels.Models;
namespace xAiApi.Providers
{
@@ -249,6 +249,7 @@ namespace xAiApi.Providers
? GetOwnerPrefix(userInfo.UserId)
: GetDocumentChunkPrefix(userInfo.UserId, fileName);
var searchResult = await dataProvider.SearchVectorsAsync(
type: null,
topK: effectiveTopK,
identifierPrefix: prefix,
minScore: effectiveMinScore,
@@ -402,8 +403,11 @@ namespace xAiApi.Providers
//
#region Documents ...
/// <summary>
/// List Ingested Documents of Owner (Grouped by File Name) ...
/// List Ingested Documents of Owner ...
/// </summary>
/// <param name="userInfo"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task<IList<XAiKnowledgeDocumentInfo>> LoadDocumentsAsync(
XUserClaimsInfoDto userInfo = null,
CancellationToken cancellationToken = default
@@ -426,19 +430,19 @@ namespace xAiApi.Providers
//
var rag = configuration.RAGDescriptor;
var result = (vectors ?? [])
.GroupBy(v => GetMetadata(v.Metadata, rag.MetadataFileName) ?? v.Identifier)
.GroupBy(v => v.Metadata.GetMetadata(rag.MetadataFileName) ?? v.Identifier)
.Select(g =>
{
//
var first = g.First();
var ingestedText = GetMetadata(first.Metadata, XAiApiConstants.XAiKnowledgeMetadataIngestedAtUtc);
var ingestedText = first.Metadata.GetMetadata(XAiApiConstants.XAiKnowledgeMetadataIngestedAtUtc);
//
return new XAiKnowledgeDocumentInfo
{
FileName = g.Key,
ChunksCount = g.Count(),
MimeType = GetMetadata(first.Metadata, rag.MetadataMimeType),
MimeType = first.Metadata.GetMetadata(rag.MetadataMimeType),
IngestedAtUtc = DateTime.TryParse(
ingestedText, null, System.Globalization.DateTimeStyles.RoundtripKind, out var dt
) ? dt : null
@@ -454,15 +458,23 @@ namespace xAiApi.Providers
/// <summary>
/// Remove All Vectors of a Document ...
/// </summary>
/// <param name="fileName"></param>
/// <param name="userInfo"></param>
/// <param name="cancellationToken"></param>
/// <returns>Count of Removed Vectors</returns>
public async Task<int> RemoveDocumentAsync(
string ownerId,
string fileName,
XUserClaimsInfoDto userInfo = null,
CancellationToken cancellationToken = default
)
{
//
// Validate ...
if (ownerId.IsNullOrEmpty() || fileName.IsNullOrEmpty())
if (
fileName.IsNullOrEmpty() ||
userInfo.IsNullOrDefault() ||
userInfo.UserId.IsNullOrEmpty()
)
{
XException.InvalidArgs.Throw();
}
@@ -470,7 +482,7 @@ namespace xAiApi.Providers
//
var result = await dataProvider.RemoveVectorsAsync(
cancellationToken: cancellationToken,
identifierPrefix: GetDocumentChunkPrefix(ownerId, fileName)
identifierPrefix: GetDocumentChunkPrefix(userInfo.UserId, fileName)
);
//
@@ -480,6 +492,8 @@ namespace xAiApi.Providers
/// <summary>
/// Map Search Results to Public Sources ...
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
public IList<XAiDocumentSource> ToSources(
IEnumerable<XAiVectorSearchResult> context
)
@@ -491,8 +505,8 @@ namespace xAiApi.Providers
{
//
var text = c.Text ?? string.Empty;
var chunkIndexText = GetMetadata(c.Metadata, rag.MetadataChunkIndex);
var snippetLength = Math.Min(text.Length, rag.SourceSnippetLength);
var chunkIndexText = c.Metadata.GetMetadata(rag.MetadataChunkIndex);
//
return new XAiDocumentSource
@@ -500,7 +514,7 @@ namespace xAiApi.Providers
Score = c.Score,
Identifier = c.Identifier,
Snippet = text.Substring(0, snippetLength),
FileName = GetMetadata(c.Metadata, rag.MetadataFileName),
FileName = c.Metadata.GetMetadata(rag.MetadataFileName),
ChunkIndex = int.TryParse(chunkIndexText, out var idx) ? idx : null
};
})
@@ -645,24 +659,6 @@ namespace xAiApi.Providers
//
return result;
}
/// <summary>
/// Safe Metadata Reader ...
/// </summary>
private static string GetMetadata(
IDictionary<string, string> metadata,
string key
)
{
//
if (metadata.IsNullOrDefault() || !metadata.TryGetValue(key, out var value))
{
return null;
}
//
return value;
}
#endregion
}
}