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