- Apply Using XUserInfoClaims to RefreshAsync on McpServers ...
- Adding DateTime Prompt Handling in XAiServiceBase ... - Adding Support for Function Tools Attachment into ChatOptions when Asking Ai ... - Adding XDateTimeDescriptor class for Configuring DateTime attaching in Prompt Context, attached it on XAiApiConfiguration and also Configure it in appSettings File ...
This commit is contained in:
@@ -45,5 +45,11 @@ namespace xAiApi.Configurations
|
|||||||
/// RAG Configuration ...
|
/// RAG Configuration ...
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public XAiDocumentRAGDescriptor RAGDescriptor {get; set;} = new();
|
public XAiDocumentRAGDescriptor RAGDescriptor {get; set;} = new();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// DateTime Configuration ...
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
public XDateTimeDescriptor DateTime { get; set; } = new();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -468,16 +468,25 @@ namespace xAiApi.Controllers
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Refresh MCP Servers ...
|
/// Refresh MCP Servers ...
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
[HttpPost("Refresh")]
|
[HttpPost("Refresh")]
|
||||||
[Authorize(Policy = XPolicies.EnabledAdmin)]
|
[Authorize(Policy = XPolicies.EnabledAdmin)]
|
||||||
public async Task<ActionResult> RefreshMcpServers()
|
public async Task<ActionResult> RefreshMcpServers(
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
{
|
{
|
||||||
//
|
//
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
//
|
//
|
||||||
await mcpServerProvider.RefreshAsync();
|
var userInfo = await GetUserInfo();
|
||||||
|
|
||||||
|
//
|
||||||
|
await mcpServerProvider.RefreshAsync(
|
||||||
|
userInfo: userInfo,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
|
||||||
//
|
//
|
||||||
return Ok();
|
return Ok();
|
||||||
|
|||||||
@@ -45,8 +45,13 @@ namespace xAiApi.Interfaces
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Force Refresh Mcp Servers ...
|
/// Force Refresh Mcp Servers ...
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
/// <param name="userInfo"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
Task RefreshAsync();
|
Task RefreshAsync(
|
||||||
|
XUserClaimsInfoDto userInfo = null,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
);
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
//
|
//
|
||||||
|
|||||||
@@ -0,0 +1,35 @@
|
|||||||
|
namespace xAiApi.Models
|
||||||
|
{
|
||||||
|
public class XDateTimeDescriptor
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Adding Current DateTime to Prompt ...
|
||||||
|
/// </summary>
|
||||||
|
public bool InjectIntoSystemPrompt { get; set; } = true;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Time Zone ...
|
||||||
|
/// </summary>
|
||||||
|
public string DefaultTimezone { get; set; } = "Asia/Tehran";
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Georgian Date Format ...
|
||||||
|
/// </summary>
|
||||||
|
public string GregorianFormat { get; set; } = "yyyy-MM-dd HH:mm:ss";
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Include Persian Calendar ...
|
||||||
|
/// </summary>
|
||||||
|
public bool IncludePersianCalendar { get; set; } = true;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Include Day of Week ...
|
||||||
|
/// </summary>
|
||||||
|
public bool IncludeDayOfWeek { get; set; } = true;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Include Unix Timestamp ...
|
||||||
|
/// </summary>
|
||||||
|
public bool IncludeUnixTimestamp { get; set; } = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
+112
-3
@@ -1,10 +1,12 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
using xModels.Dtos;
|
using xModels.Dtos;
|
||||||
using xAiApi.Models;
|
using xAiApi.Models;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using xAiApi.Constants;
|
using xAiApi.Constants;
|
||||||
using xAiModels.Models;
|
using xAiModels.Models;
|
||||||
|
using TimeZoneConverter;
|
||||||
using xAiApi.Interfaces;
|
using xAiApi.Interfaces;
|
||||||
using xAiApi.Extensions;
|
using xAiApi.Extensions;
|
||||||
using xCommons.Providers;
|
using xCommons.Providers;
|
||||||
@@ -12,6 +14,7 @@ using xCommons.Extensions;
|
|||||||
using xAiModels.Constants;
|
using xAiModels.Constants;
|
||||||
using xAiModels.Interfaces;
|
using xAiModels.Interfaces;
|
||||||
using xAiModels.Extensions;
|
using xAiModels.Extensions;
|
||||||
|
using System.Globalization;
|
||||||
using xAiApi.Configurations;
|
using xAiApi.Configurations;
|
||||||
using xExceptions.Constants;
|
using xExceptions.Constants;
|
||||||
using xAiModels.Models.Dtos;
|
using xAiModels.Models.Dtos;
|
||||||
@@ -1204,9 +1207,23 @@ namespace xAiApi.Providers
|
|||||||
[
|
[
|
||||||
introductionPrompt,
|
introductionPrompt,
|
||||||
];
|
];
|
||||||
|
|
||||||
|
//
|
||||||
|
// Prepare Datetime ...
|
||||||
|
var datetimeContext = BuildDateTimeContext();
|
||||||
|
if (!datetimeContext.IsNullOrEmpty())
|
||||||
|
{
|
||||||
|
//
|
||||||
|
result.Add(new ChatMessage(
|
||||||
|
ChatRole.Assistant,
|
||||||
|
datetimeContext
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
if (forcePrompt &&
|
if (forcePrompt &&
|
||||||
prompt is not null &&
|
prompt is not null &&
|
||||||
!string.IsNullOrWhiteSpace(prompt))
|
!prompt.IsNullOrEmpty())
|
||||||
{
|
{
|
||||||
result.Add(promptMessage);
|
result.Add(promptMessage);
|
||||||
}
|
}
|
||||||
@@ -1236,6 +1253,7 @@ namespace xAiApi.Providers
|
|||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
|
var datetimeContext = BuildDateTimeContext();
|
||||||
var contextBlock = BuildContextBlock(context);
|
var contextBlock = BuildContextBlock(context);
|
||||||
var systemText = introductionPrompt;
|
var systemText = introductionPrompt;
|
||||||
if (context.HasChild() &&
|
if (context.HasChild() &&
|
||||||
@@ -1244,7 +1262,17 @@ namespace xAiApi.Providers
|
|||||||
{
|
{
|
||||||
//
|
//
|
||||||
systemText =
|
systemText =
|
||||||
$"{introductionPrompt}{Environment.NewLine}{Environment.NewLine}" +
|
$"{introductionPrompt}{Environment.NewLine}{Environment.NewLine}";
|
||||||
|
|
||||||
|
//
|
||||||
|
if (!datetimeContext.IsNullOrEmpty())
|
||||||
|
{
|
||||||
|
//
|
||||||
|
systemText += $"{datetimeContext}{Environment.NewLine}{Environment.NewLine}";
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
systemText +=
|
||||||
$"{ragPrompt}{Environment.NewLine}{Environment.NewLine}" +
|
$"{ragPrompt}{Environment.NewLine}{Environment.NewLine}" +
|
||||||
$"Context:{Environment.NewLine}{contextBlock}";
|
$"Context:{Environment.NewLine}{contextBlock}";
|
||||||
}
|
}
|
||||||
@@ -1305,12 +1333,22 @@ namespace xAiApi.Providers
|
|||||||
$"Context:{Environment.NewLine}{contextBlock}";
|
$"Context:{Environment.NewLine}{contextBlock}";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Prepare Datetime ...
|
||||||
|
var datetimeContext = BuildDateTimeContext();
|
||||||
|
if (!datetimeContext.IsNullOrEmpty())
|
||||||
|
{
|
||||||
|
//
|
||||||
|
systemText +=
|
||||||
|
$"{datetimeContext}{Environment.NewLine}{Environment.NewLine}";
|
||||||
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
// Apply Project Prompt as System Text ...
|
// Apply Project Prompt as System Text ...
|
||||||
if (!project.IsNullOrDefault() &&
|
if (!project.IsNullOrDefault() &&
|
||||||
!project.Prompt.IsNullOrEmpty())
|
!project.Prompt.IsNullOrEmpty())
|
||||||
{
|
{
|
||||||
systemText += $"{project.Prompt}{Environment.NewLine}";
|
systemText += $"{project.Prompt}{Environment.NewLine}{Environment.NewLine}";
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
@@ -1670,6 +1708,77 @@ namespace xAiApi.Providers
|
|||||||
//
|
//
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private string BuildDateTimeContext()
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var cfg = configuration.DateTime;
|
||||||
|
if (cfg == null || !cfg.InjectIntoSystemPrompt)
|
||||||
|
{
|
||||||
|
return string.Empty;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
try
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var tz = ResolveTimezone(cfg.DefaultTimezone);
|
||||||
|
var nowUtc = DateTime.UtcNow;
|
||||||
|
var nowLocal = TimeZoneInfo.ConvertTimeFromUtc(nowUtc, tz);
|
||||||
|
|
||||||
|
//
|
||||||
|
var sb = new StringBuilder();
|
||||||
|
sb.AppendLine("[CURRENT DATETIME — for your reference]");
|
||||||
|
sb.AppendLine($"Now (local, {tz.Id}): {nowLocal.ToString(cfg.GregorianFormat)}");
|
||||||
|
|
||||||
|
//
|
||||||
|
if (cfg.IncludeDayOfWeek)
|
||||||
|
{
|
||||||
|
sb.AppendLine($"Day of week: {nowLocal:dddd}");
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
if (cfg.IncludePersianCalendar)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var pc = new PersianCalendar();
|
||||||
|
sb.AppendLine($"Now (Persian): {pc.GetYear(nowLocal):D4}/{pc.GetMonth(nowLocal):D2}/{pc.GetDayOfMonth(nowLocal):D2} {nowLocal:HH:mm:ss}");
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
if (cfg.IncludeUnixTimestamp)
|
||||||
|
{
|
||||||
|
sb.AppendLine($"Unix timestamp: {new DateTimeOffset(nowUtc).ToUnixTimeSeconds()}");
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
sb.AppendLine("Use this information when the user asks about 'today', 'now', 'this week', relative dates, or any time-sensitive calculations.");
|
||||||
|
return sb.ToString().TrimEnd();
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
return string.Empty;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static TimeZoneInfo ResolveTimezone(string id)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
if (id.IsNullOrEmpty())
|
||||||
|
{
|
||||||
|
return TimeZoneInfo.Utc;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return TZConvert.GetTimeZoneInfo(id);
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
return TimeZoneInfo.Utc;
|
||||||
|
}
|
||||||
|
}
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -211,9 +211,19 @@ namespace xAiApi.Providers
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Force Refresh Mcp Servers ...
|
/// Force Refresh Mcp Servers ...
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
/// <param name="userInfo"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public async Task RefreshAsync()
|
public async Task RefreshAsync(
|
||||||
|
XUserClaimsInfoDto userInfo = null,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
{
|
{
|
||||||
|
//
|
||||||
|
if (!userInfo.IsAdmin()) {
|
||||||
|
XException.NotAllowed.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
logger.LogInformation("MCP: Reinitialize Mcp Servers ...");
|
logger.LogInformation("MCP: Reinitialize Mcp Servers ...");
|
||||||
foreach (var client in clients.Values)
|
foreach (var client in clients.Values)
|
||||||
@@ -230,7 +240,10 @@ namespace xAiApi.Providers
|
|||||||
|
|
||||||
//
|
//
|
||||||
isInitialized = false;
|
isInitialized = false;
|
||||||
await GetAvailableToolsAsync(cancellationToken: CancellationToken.None);
|
await GetAvailableToolsAsync(
|
||||||
|
userInfo: userInfo,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|||||||
@@ -130,6 +130,14 @@
|
|||||||
"MetadataMimeType": "mimeType",
|
"MetadataMimeType": "mimeType",
|
||||||
"PromptName": "ChatWithDocuments",
|
"PromptName": "ChatWithDocuments",
|
||||||
"MetadataChunkIndex": "chunkIndex"
|
"MetadataChunkIndex": "chunkIndex"
|
||||||
|
},
|
||||||
|
"DateTime": {
|
||||||
|
"IncludeDayOfWeek": true,
|
||||||
|
"IncludeUnixTimestamp": false,
|
||||||
|
"InjectIntoSystemPrompt": true,
|
||||||
|
"IncludePersianCalendar": true,
|
||||||
|
"DefaultTimezone": "Asia/Tehran",
|
||||||
|
"GregorianFormat": "yyyy-MM-dd HH:mm:ss"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"Logging": {
|
"Logging": {
|
||||||
|
|||||||
Reference in New Issue
Block a user