Refactoring Services ...

This commit is contained in:
2026-11-01 22:36:20 +03:30
parent ce4e5927dd
commit fa1014bf20
7 changed files with 434 additions and 253 deletions
+223 -22
View File
@@ -104,6 +104,7 @@ namespace xAiApi.Providers
using var client = Descriptor.GetClient();
var history = GetHistory(prompt);
var response = await client.GetResponseAsync(
options: Options,
messages: history,
cancellationToken: cancellationToken
);
@@ -149,7 +150,7 @@ namespace xAiApi.Providers
using var client = Descriptor.GetClient();
var history = GetHistory(prompt);
var enumerable = client.GetStreamingResponseAsync(
options: null,
options: Options,
messages: history
);
@@ -169,29 +170,34 @@ namespace xAiApi.Providers
}
/// <summary>
/// Ask a Question using Provided Context Chunks (RAG) without Persisting ...
/// Ask a Question By Providing Vector Search Result ...
/// </summary>
/// <param name="prompt"></param>
/// <param name="context"></param>
/// <param name="userInfo"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public virtual async Task<string> AskWithContextAsync(
public virtual async Task<string> AskTextByContextAsync(
string prompt,
IEnumerable<XAiVectorSearchResult> context,
IList<XAiVectorSearchResult> context = null,
XUserClaimsInfoDto userInfo = null,
CancellationToken cancellationToken = default
)
{
//
// Validate Args ...
if (prompt.IsNullOrEmpty())
{
XException.InvalidArgs.Throw();
}
validationProvider
.GroupValidationBuilder()
.AddNotEmpty(prompt)
.AddNotNull(userInfo)
.AddNotEmpty(userInfo.UserId)
.ValidateGroup();
//
using var client = Descriptor.GetClient();
var history = GetHistory(prompt, context);
var response = await client.GetResponseAsync(
options: Options,
messages: history,
cancellationToken: cancellationToken
);
@@ -212,31 +218,219 @@ namespace xAiApi.Providers
}
/// <summary>
/// Ask a Question using Provided Context Chunks (RAG) and Stream Response ...
/// Ask a Question By By Providing Vector Search Result and Stream Response ...
/// </summary>
/// <param name="prompt"></param>
/// <param name="context"></param>
/// <param name="userInfo"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public virtual async IAsyncEnumerable<string> AskWithContextAsEnumerable(
public virtual async IAsyncEnumerable<string> AskTextByContextAsEnumerable(
string prompt,
IEnumerable<XAiVectorSearchResult> context,
IList<XAiVectorSearchResult> context = null,
XUserClaimsInfoDto userInfo = null,
[EnumeratorCancellation]
CancellationToken cancellationToken = default
)
{
//
// Validate Args ...
if (prompt.IsNullOrEmpty())
{
XException.InvalidArgs.Throw();
}
validationProvider
.GroupValidationBuilder()
.AddNotEmpty(prompt)
.AddNotNull(userInfo)
.AddNotEmpty(userInfo.UserId)
.ValidateGroup();
//
using var client = Descriptor.GetClient();
var history = GetHistory(prompt, context);
var enumerable = client.GetStreamingResponseAsync(
options: null,
options: Options,
messages: history,
cancellationToken: cancellationToken
);
//
await foreach (var res in enumerable)
{
//
if (cancellationToken.IsCancellationRequested)
{
yield break;
}
//
yield return res.Text;
}
}
/// <summary>
/// Ask a Question By Rag Retrive From Knowledge ...
/// </summary>
/// <param name="prompt"></param>
/// <param name="topK"></param>
/// <param name="minScore"></param>
/// <param name="userInfo"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public virtual async Task<string> AskTextFromKnowledgeAsync(
string prompt,
int? topK = null,
float? minScore = null,
XUserClaimsInfoDto userInfo = null,
CancellationToken cancellationToken = default
)
{
//
// Validate Args ...
validationProvider
.GroupValidationBuilder()
.AddNotEmpty(prompt)
.AddNotNull(userInfo)
.AddNotEmpty(userInfo.UserId)
.ValidateGroup();
//
// Normalize ...
if (!topK.HasValue ||
topK.Value < 1 ||
topK.Value > configuration.RAGDescriptor.MaxTopK
)
{
//
if (!topK.HasValue ||
(topK.HasValue && topK.Value < 1))
{
topK = configuration.RAGDescriptor.DefaultTopK;
}
else if (topK.HasValue && topK.Value > configuration.RAGDescriptor.MaxTopK)
{
topK = configuration.RAGDescriptor.MaxTopK;
}
}
if (!minScore.HasValue ||
minScore.Value <= 0 ||
minScore.Value > 1
)
{
minScore = configuration.RAGDescriptor.DefaultMinScore;
}
//
// Prepare Knowledge Request ...
var knowledgeRequest = new XAiKnowledgeAskRequest
{
TopK = topK,
Prompt = prompt,
IncludeUser = true,
MinScore = minScore,
IncludeGlobal = true,
};
var context = await ragProvider.RetrieveAsync(
userInfo: userInfo,
request: knowledgeRequest,
cancellationToken: cancellationToken
);
//
using var client = Descriptor.GetClient();
var history = GetHistory(prompt, context);
var response = await client.GetResponseAsync(
options: Options,
messages: history,
cancellationToken: cancellationToken
);
//
// Validate Response ...
if (!response.IsValid())
{
client.Dispose();
XException.ActionFailed.Throw();
}
//
var result = response.Text;
//
return result;
}
/// <summary>
/// Ask a Question By Rag Retrive From Knowledge and Stream Response ...
/// </summary>
/// <param name="prompt"></param>
/// <param name="topK"></param>
/// <param name="minScore"></param>
/// <param name="userInfo"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public virtual async IAsyncEnumerable<string> AskTextFromKnowledgeAsEnumerable(
string prompt,
int? topK = null,
float? minScore = null,
XUserClaimsInfoDto userInfo = null,
[EnumeratorCancellation]
CancellationToken cancellationToken = default
)
{
//
// Validate Args ...
validationProvider
.GroupValidationBuilder()
.AddNotEmpty(prompt)
.AddNotNull(userInfo)
.AddNotEmpty(userInfo.UserId)
.ValidateGroup();
//
// Normalize ...
if (!topK.HasValue ||
topK.Value < 1 ||
topK.Value > configuration.RAGDescriptor.MaxTopK
)
{
//
if (!topK.HasValue ||
(topK.HasValue && topK.Value < 1))
{
topK = configuration.RAGDescriptor.DefaultTopK;
}
else if (topK.HasValue && topK.Value > configuration.RAGDescriptor.MaxTopK)
{
topK = configuration.RAGDescriptor.MaxTopK;
}
}
if (!minScore.HasValue ||
minScore.Value <= 0 ||
minScore.Value > 1
)
{
minScore = configuration.RAGDescriptor.DefaultMinScore;
}
//
// Prepare Knowledge Request ...
var knowledgeRequest = new XAiKnowledgeAskRequest
{
TopK = topK,
Prompt = prompt,
IncludeUser = true,
MinScore = minScore,
IncludeGlobal = true,
};
var context = await ragProvider.RetrieveAsync(
userInfo: userInfo,
request: knowledgeRequest,
cancellationToken: cancellationToken
);
//
using var client = Descriptor.GetClient();
var history = GetHistory(prompt, context);
var enumerable = client.GetStreamingResponseAsync(
options: Options,
messages: history,
cancellationToken: cancellationToken
);
@@ -1280,7 +1474,7 @@ namespace xAiApi.Providers
/// <param name="prompt"></param>
/// <param name="context"></param>
/// <returns></returns>
protected virtual IList<ChatMessage> GetHistory(
private IList<ChatMessage> GetHistory(
string prompt,
IEnumerable<XAiVectorSearchResult> context
)
@@ -1295,10 +1489,17 @@ namespace xAiApi.Providers
//
var contextBlock = BuildContextBlock(context);
var systemText =
$"{introductionPrompt}{Environment.NewLine}{Environment.NewLine}" +
$"{ragPrompt}{Environment.NewLine}{Environment.NewLine}" +
$"Context:{Environment.NewLine}{contextBlock}";
var systemText = introductionPrompt;
if (context.HasChild() &&
!contextBlock.IsNullOrEmpty()
)
{
//
systemText =
$"{introductionPrompt}{Environment.NewLine}{Environment.NewLine}" +
$"{ragPrompt}{Environment.NewLine}{Environment.NewLine}" +
$"Context:{Environment.NewLine}{contextBlock}";
}
//
List<ChatMessage> result =
@@ -1347,7 +1548,7 @@ namespace xAiApi.Providers
!contextBlock.IsNullOrEmpty())
{
//
systemText =
systemText =
$"{introductionPrompt}{Environment.NewLine}{Environment.NewLine}" +
$"{ragPrompt}{Environment.NewLine}{Environment.NewLine}" +
$"Context:{Environment.NewLine}{contextBlock}";
+24 -2
View File
@@ -447,12 +447,14 @@ namespace xAiApi.Providers
/// <param name="fileName"></param>
/// <param name="scope"></param>
/// <param name="userInfo"></param>
/// <param name="connectionId"></param>
/// <param name="cancellationToken"></param>
/// <returns>Count of Removed Vectors</returns>
public async Task<int> RemoveDocumentAsync(
string fileName,
XAiKnowledgeScopeDescriptor scope = null,
XUserClaimsInfoDto userInfo = null,
string connectionId = null,
CancellationToken cancellationToken = default
)
{
@@ -479,8 +481,28 @@ namespace xAiApi.Providers
//
// Remove Files using ...
var file = await fileProvider.FindOne(
x => x.FileName ==
)
x => x.FileName
.Equals(
fileName,
StringComparison.InvariantCultureIgnoreCase) &&
x.OwnerId
.Equals(
userInfo.UserId,
StringComparison.InvariantCultureIgnoreCase),
cancellationToken: cancellationToken
);
//
if (!file.IsNullOrDefault())
{
//
await fileProvider.Remove(
id: file.Id,
userInfo: userInfo,
connectionId: connectionId,
cancellationToken: cancellationToken
);
}
}
//