last works ...

This commit is contained in:
2026-04-30 12:01:56 +03:30
parent 2b5e09912f
commit f7184c7b79
3 changed files with 210 additions and 10 deletions
+26 -10
View File
@@ -509,12 +509,17 @@ namespace xAiApi.AI.Services
//
// Getting Response Message from LLM ...
var chatResponseEnumerable = chatClient
var chatStream = chatClient
.GetStreamingResponseAsync(
options: chatOptions,
messages: requirements.ChatHistory,
cancellationToken: cancellationToken
);
isValid = !chatStream.IsNull();
if (!isValid)
{
yield break;
}
//
// Create an Empty Message for ID ...
@@ -531,31 +536,31 @@ namespace xAiApi.AI.Services
saveChanges: true,
item: responseMessage
);
//
// Collecting Data through Enumeration ...
// Loop through Async Enumerable ...
string content = string.Empty;
var sequence = DateTime.UtcNow.ToTimestamp();
await foreach (var data in chatResponseEnumerable)
await foreach(var msg in chatStream)
{
//
// Crucially, check the cancellation token before continuing
// Throwing OperationCanceledException is the standard way to signal cancellation.
if (cancellationToken.CanBeCanceled)
if (cancellationToken.IsCancellationRequested)
{
yield break;
}
//
// Append recieved data to Content ...
content += data;
content += msg.Text;
//
// Crate Message Update Model ...
var item = new XAiMessageUpdateDto
{
Sequense = sequence,
Content = data.Text,
Content = msg.Text,
Id = responseMessage.Id,
OwnerId = userInfo.UserId,
UpdatedAt = DateTime.UtcNow,
@@ -586,6 +591,7 @@ namespace xAiApi.AI.Services
}
//
// End Async Enumerable ...
yield break;
}
#endregion
@@ -976,7 +982,10 @@ namespace xAiApi.AI.Services
{
//
// Complete Chat History ...
foreach (var msg in conversation.Messages)
var orderedByTimeMessage = conversation.Messages
.OrderBy(x => x.CreatedOn)
.AsEnumerable();
foreach (var msg in orderedByTimeMessage)
{
//
result.ChatHistory.Add(
@@ -1025,11 +1034,18 @@ namespace xAiApi.AI.Services
XException.InvalidData.Throw();
}
//
// Update Conversation ...
var conversationEntity = await aiConversationRepository
.GetAsync(id: result.AiConversation.Id);
conversation = mapper.Map<XAiConversationDto>(conversationEntity);
result.AiConversation = conversation;
//
result.ChatHistory.Add(
new ChatMessage(
content: prompt,
role: XAiChatRole.User.ToChatRole()
content: promptAiMessage.Content,
role: promptAiMessage.Role.ToChatRole()
)
);