- 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:
+114
-5
@@ -1,10 +1,12 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using xModels.Dtos;
|
||||
using xAiApi.Models;
|
||||
using System.Threading;
|
||||
using xAiApi.Constants;
|
||||
using xAiModels.Models;
|
||||
using TimeZoneConverter;
|
||||
using xAiApi.Interfaces;
|
||||
using xAiApi.Extensions;
|
||||
using xCommons.Providers;
|
||||
@@ -12,6 +14,7 @@ using xCommons.Extensions;
|
||||
using xAiModels.Constants;
|
||||
using xAiModels.Interfaces;
|
||||
using xAiModels.Extensions;
|
||||
using System.Globalization;
|
||||
using xAiApi.Configurations;
|
||||
using xExceptions.Constants;
|
||||
using xAiModels.Models.Dtos;
|
||||
@@ -126,7 +129,7 @@ namespace xAiApi.Providers
|
||||
);
|
||||
|
||||
//
|
||||
Options.Tools = [.. availableTools .Cast<AITool>()];
|
||||
Options.Tools = [.. availableTools.Cast<AITool>()];
|
||||
}
|
||||
|
||||
//
|
||||
@@ -191,7 +194,7 @@ namespace xAiApi.Providers
|
||||
);
|
||||
|
||||
//
|
||||
Options.Tools = [.. availableTools .Cast<AITool>()];
|
||||
Options.Tools = [.. availableTools.Cast<AITool>()];
|
||||
}
|
||||
|
||||
//
|
||||
@@ -1204,9 +1207,23 @@ namespace xAiApi.Providers
|
||||
[
|
||||
introductionPrompt,
|
||||
];
|
||||
|
||||
//
|
||||
// Prepare Datetime ...
|
||||
var datetimeContext = BuildDateTimeContext();
|
||||
if (!datetimeContext.IsNullOrEmpty())
|
||||
{
|
||||
//
|
||||
result.Add(new ChatMessage(
|
||||
ChatRole.Assistant,
|
||||
datetimeContext
|
||||
));
|
||||
}
|
||||
|
||||
//
|
||||
if (forcePrompt &&
|
||||
prompt is not null &&
|
||||
!string.IsNullOrWhiteSpace(prompt))
|
||||
!prompt.IsNullOrEmpty())
|
||||
{
|
||||
result.Add(promptMessage);
|
||||
}
|
||||
@@ -1236,6 +1253,7 @@ namespace xAiApi.Providers
|
||||
}
|
||||
|
||||
//
|
||||
var datetimeContext = BuildDateTimeContext();
|
||||
var contextBlock = BuildContextBlock(context);
|
||||
var systemText = introductionPrompt;
|
||||
if (context.HasChild() &&
|
||||
@@ -1244,7 +1262,17 @@ namespace xAiApi.Providers
|
||||
{
|
||||
//
|
||||
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}" +
|
||||
$"Context:{Environment.NewLine}{contextBlock}";
|
||||
}
|
||||
@@ -1305,12 +1333,22 @@ namespace xAiApi.Providers
|
||||
$"Context:{Environment.NewLine}{contextBlock}";
|
||||
}
|
||||
|
||||
//
|
||||
// Prepare Datetime ...
|
||||
var datetimeContext = BuildDateTimeContext();
|
||||
if (!datetimeContext.IsNullOrEmpty())
|
||||
{
|
||||
//
|
||||
systemText +=
|
||||
$"{datetimeContext}{Environment.NewLine}{Environment.NewLine}";
|
||||
}
|
||||
|
||||
//
|
||||
// Apply Project Prompt as System Text ...
|
||||
if (!project.IsNullOrDefault() &&
|
||||
!project.Prompt.IsNullOrEmpty())
|
||||
{
|
||||
systemText += $"{project.Prompt}{Environment.NewLine}";
|
||||
systemText += $"{project.Prompt}{Environment.NewLine}{Environment.NewLine}";
|
||||
}
|
||||
|
||||
//
|
||||
@@ -1670,6 +1708,77 @@ namespace xAiApi.Providers
|
||||
//
|
||||
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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user