2024-07-13 08:37:57 +00:00
|
|
|
using System.Text.Json.Serialization;
|
|
|
|
|
2024-05-04 09:11:09 +00:00
|
|
|
using AIStudio.Provider;
|
|
|
|
using AIStudio.Settings;
|
2025-02-17 15:51:26 +00:00
|
|
|
using AIStudio.Tools.RAG.RAGProcesses;
|
2024-05-04 09:11:09 +00:00
|
|
|
|
|
|
|
namespace AIStudio.Chat;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Text content in the chat.
|
|
|
|
/// </summary>
|
|
|
|
public sealed class ContentText : IContent
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// The minimum time between two streaming events, when the user
|
|
|
|
/// enables the energy saving mode.
|
|
|
|
/// </summary>
|
|
|
|
private static readonly TimeSpan MIN_TIME = TimeSpan.FromSeconds(3);
|
|
|
|
|
|
|
|
#region Implementation of IContent
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2024-07-13 08:37:57 +00:00
|
|
|
[JsonIgnore]
|
2024-05-04 09:11:09 +00:00
|
|
|
public bool InitialRemoteWait { get; set; }
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2024-07-13 08:37:57 +00:00
|
|
|
// [JsonIgnore]
|
2024-05-04 09:11:09 +00:00
|
|
|
public bool IsStreaming { get; set; }
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2024-07-13 08:37:57 +00:00
|
|
|
[JsonIgnore]
|
2024-05-04 09:11:09 +00:00
|
|
|
public Func<Task> StreamingDone { get; set; } = () => Task.CompletedTask;
|
|
|
|
|
2024-07-13 08:37:57 +00:00
|
|
|
/// <inheritdoc />
|
|
|
|
[JsonIgnore]
|
2024-05-04 09:11:09 +00:00
|
|
|
public Func<Task> StreamingEvent { get; set; } = () => Task.CompletedTask;
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2025-03-08 12:56:38 +00:00
|
|
|
public async Task<ChatThread> CreateFromProviderAsync(IProvider provider, Model chatModel, IContent? lastPrompt, ChatThread? chatThread, CancellationToken token = default)
|
2024-05-04 09:11:09 +00:00
|
|
|
{
|
|
|
|
if(chatThread is null)
|
2025-03-08 12:56:38 +00:00
|
|
|
return new();
|
2025-02-17 11:33:34 +00:00
|
|
|
|
2025-03-08 19:13:08 +00:00
|
|
|
if(!chatThread.IsLLMProviderAllowed(provider))
|
|
|
|
{
|
|
|
|
var logger = Program.SERVICE_PROVIDER.GetService<ILogger<ContentText>>()!;
|
|
|
|
logger.LogError("The provider is not allowed for this chat thread due to data security reasons. Skipping the AI process.");
|
|
|
|
return chatThread;
|
|
|
|
}
|
|
|
|
|
2025-02-17 15:51:26 +00:00
|
|
|
// Call the RAG process. Right now, we only have one RAG process:
|
|
|
|
if (lastPrompt is not null)
|
2025-02-15 14:41:12 +00:00
|
|
|
{
|
2025-03-08 10:14:20 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
var rag = new AISrcSelWithRetCtxVal();
|
|
|
|
chatThread = await rag.ProcessAsync(provider, lastPrompt, chatThread, token);
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
var logger = Program.SERVICE_PROVIDER.GetService<ILogger<ContentText>>()!;
|
|
|
|
logger.LogError(e, "Skipping the RAG process due to an error.");
|
|
|
|
}
|
2025-02-15 14:41:12 +00:00
|
|
|
}
|
2025-02-17 15:51:26 +00:00
|
|
|
|
2024-11-13 19:33:52 +00:00
|
|
|
// Store the last time we got a response. We use this later
|
2024-05-04 09:11:09 +00:00
|
|
|
// to determine whether we should notify the UI about the
|
|
|
|
// new content or not. Depends on the energy saving mode
|
|
|
|
// the user chose.
|
|
|
|
var last = DateTimeOffset.Now;
|
|
|
|
|
2025-02-17 15:51:26 +00:00
|
|
|
// Get the settings manager:
|
|
|
|
var settings = Program.SERVICE_PROVIDER.GetService<SettingsManager>()!;
|
|
|
|
|
2024-09-01 18:10:03 +00:00
|
|
|
// Start another thread by using a task to uncouple
|
2024-05-04 09:11:09 +00:00
|
|
|
// the UI thread from the AI processing:
|
|
|
|
await Task.Run(async () =>
|
|
|
|
{
|
|
|
|
// We show the waiting animation until we get the first response:
|
|
|
|
this.InitialRemoteWait = true;
|
|
|
|
|
|
|
|
// Iterate over the responses from the AI:
|
2025-01-02 13:50:54 +00:00
|
|
|
await foreach (var deltaText in provider.StreamChatCompletion(chatModel, chatThread, settings, token))
|
2024-05-04 09:11:09 +00:00
|
|
|
{
|
|
|
|
// When the user cancels the request, we stop the loop:
|
|
|
|
if (token.IsCancellationRequested)
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Stop the waiting animation:
|
|
|
|
this.InitialRemoteWait = false;
|
|
|
|
this.IsStreaming = true;
|
|
|
|
|
|
|
|
// Add the response to the text:
|
|
|
|
this.Text += deltaText;
|
|
|
|
|
|
|
|
// Notify the UI that the content has changed,
|
|
|
|
// depending on the energy saving mode:
|
|
|
|
var now = DateTimeOffset.Now;
|
2024-08-05 19:12:52 +00:00
|
|
|
switch (settings.ConfigurationData.App.IsSavingEnergy)
|
2024-05-04 09:11:09 +00:00
|
|
|
{
|
|
|
|
// Energy saving mode is off. We notify the UI
|
|
|
|
// as fast as possible -- no matter the odds:
|
|
|
|
case false:
|
|
|
|
await this.StreamingEvent();
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Energy saving mode is on. We notify the UI
|
|
|
|
// only when the time between two events is
|
|
|
|
// greater than the minimum time:
|
|
|
|
case true when now - last > MIN_TIME:
|
|
|
|
last = now;
|
|
|
|
await this.StreamingEvent();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop the waiting animation (in case the loop
|
2024-09-01 18:10:03 +00:00
|
|
|
// was stopped, or no content was received):
|
2024-05-04 09:11:09 +00:00
|
|
|
this.InitialRemoteWait = false;
|
|
|
|
this.IsStreaming = false;
|
|
|
|
}, token);
|
|
|
|
|
|
|
|
// Inform the UI that the streaming is done:
|
|
|
|
await this.StreamingDone();
|
2025-03-08 12:56:38 +00:00
|
|
|
return chatThread;
|
2024-05-04 09:11:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The text content.
|
|
|
|
/// </summary>
|
|
|
|
public string Text { get; set; } = string.Empty;
|
|
|
|
}
|