Fix Jinja Conversation Role Alternation in RAG Context History

- Merge Introduction prompt, RAG prompt and context block into one System message;
- Keep the user question as the following User message;
- Prevent consecutive System roles in AskWithContextAsync and AskWithContextAsEnumerable;
- Update the GetContextHistory XML documentation to describe the new message layout;
- Preserve the existing context retrieval and response flow.
- ...
This commit is contained in:
2026-10-26 10:43:17 +03:30
parent fa92763baa
commit fef38cad1b
4 changed files with 61 additions and 4 deletions
+27
View File
@@ -0,0 +1,27 @@
using xCommons.Providers;
using xCommons.Configurations;
using xIdentityHelper.Controllers;
using xIdentityService.Interfaces;
using Microsoft.Extensions.Logging;
namespace xAiApi.Controllers
{
public partial class AccountController : XAccountControllerBase
{
//
#region Constructor ...
public AccountController(
ILogger<AccountController> logger,
XAppConfiguration appConfiguration,
IXIdentityProvider identityProvider,
XValidationProvider validationProvider
) : base(
logger,
appConfiguration,
identityProvider,
validationProvider
)
{ }
#endregion
}
}
+24
View File
@@ -0,0 +1,24 @@
using xCommons.Providers;
using xCommons.Configurations;
using xIdentityService.Interfaces;
using xIdentityHelper.Controllers;
using Microsoft.Extensions.Logging;
namespace xAiApi.Controllers {
/// <summary>
/// Test all Authentication Policies ...
/// </summary>
public class TestIdentity : XTestIdentityControllerBase {
public TestIdentity (
ILogger<TestIdentity> logger,
XAppConfiguration appConfiguration,
IXIdentityProvider identityProvider,
XValidationProvider validationProvider
) : base (
logger,
appConfiguration,
identityProvider,
validationProvider
) { }
}
}
+6 -3
View File
@@ -342,7 +342,7 @@ namespace xAiApi.Providers
/// <summary>
/// Build Chat History for RAG:
/// [System: Introduction] + [System: ChatWithDocuments Prompt + Context Block] + [User: Prompt] ...
/// [System: Introduction + ChatWithDocuments Prompt + Context Block] + [User: Prompt] ...
/// </summary>
/// <param name="prompt"></param>
/// <param name="context"></param>
@@ -353,6 +353,7 @@ namespace xAiApi.Providers
)
{
//
var introductionPrompt = configuration.GetPrompt();
var ragPrompt = configuration.GetPrompt(configuration.RAGDescriptor.PromptName);
if (ragPrompt.IsNullOrEmpty())
{
@@ -361,12 +362,14 @@ namespace xAiApi.Providers
//
var contextBlock = BuildContextBlock(context);
var systemText = $"{ragPrompt}{Environment.NewLine}{Environment.NewLine}Context:{Environment.NewLine}{contextBlock}";
var systemText =
$"{introductionPrompt}{Environment.NewLine}{Environment.NewLine}" +
$"{ragPrompt}{Environment.NewLine}{Environment.NewLine}" +
$"Context:{Environment.NewLine}{contextBlock}";
//
List<ChatMessage> result =
[
new ChatMessage(ChatRole.System, configuration.GetPrompt()),
new ChatMessage(ChatRole.System, systemText),
new ChatMessage(ChatRole.User, prompt),
];
+4 -1
View File
@@ -157,9 +157,12 @@ namespace xAiApi.Providers
}
//
return [.. result
result = [.. result
.OrderByDescending(x => x.Score)
.Take(topK)];
//
return result;
}
public bool TryGet(