mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2026-10-11 07:53:47 +00:00
added sql integration instead of json files
This commit is contained in:
1 parent
2e06af13e0
commit
c59e33e1b1
22 files changed
+700
-156
No files matched your search
@@ -1,90 +1,27 @@
|
||||
using System.Text.Json;
|
||||
|
||||
using AIStudio.Settings;
|
||||
using AIStudio.Tools.Databases.EmbeddingState;
|
||||
using AIStudio.Tools.Databases.VectorStore;
|
||||
|
||||
namespace AIStudio.Tools.Services;
|
||||
|
||||
public sealed partial class DataSourceEmbeddingService
|
||||
{
|
||||
private const string STATE_FILENAME = "rag-embedding-state.json";
|
||||
|
||||
private readonly JsonSerializerOptions jsonOptions = new()
|
||||
private async Task ResetPersistedStateAsync(
|
||||
string dataSourceName,
|
||||
string dataSourceId,
|
||||
VectorStoreClient? vectorStore,
|
||||
EmbeddingStateClient? embeddingState,
|
||||
CancellationToken token)
|
||||
{
|
||||
WriteIndented = true,
|
||||
};
|
||||
await this.DeleteCollectionAsync(this.GetCollectionName(dataSourceName, dataSourceId), vectorStore, token);
|
||||
|
||||
private async Task EnsureStateLoadedAsync(CancellationToken token)
|
||||
{
|
||||
if (this.stateLoaded)
|
||||
embeddingState ??= await databaseClientProvider.GetEmbeddingStateAsync(token);
|
||||
if (!embeddingState.IsAvailable)
|
||||
{
|
||||
logger.LogWarning("Could not delete SQLite embedding state for data source '{DataSourceId}' because the embedding state database '{DatabaseName}' is unavailable.", dataSourceId, embeddingState.Name);
|
||||
return;
|
||||
|
||||
await this.stateLock.WaitAsync(token);
|
||||
try
|
||||
{
|
||||
if (this.stateLoaded)
|
||||
return;
|
||||
|
||||
var statePath = this.GetStatePath();
|
||||
if (!string.IsNullOrWhiteSpace(statePath) && File.Exists(statePath))
|
||||
{
|
||||
var json = await File.ReadAllTextAsync(statePath, token);
|
||||
var persistedState = JsonSerializer.Deserialize<PersistedEmbeddingState>(json, this.jsonOptions);
|
||||
this.manifests = persistedState?.DataSources ?? new Dictionary<string, DataSourceEmbeddingManifest>(StringComparer.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
this.stateLoaded = true;
|
||||
}
|
||||
finally
|
||||
{
|
||||
this.stateLock.Release();
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<DataSourceEmbeddingManifest> GetManifestAsync(string dataSourceId, CancellationToken token)
|
||||
{
|
||||
await this.EnsureStateLoadedAsync(token);
|
||||
if (this.manifests.TryGetValue(dataSourceId, out var manifest))
|
||||
return manifest;
|
||||
|
||||
manifest = new DataSourceEmbeddingManifest();
|
||||
this.manifests[dataSourceId] = manifest;
|
||||
return manifest;
|
||||
}
|
||||
|
||||
private async Task SaveStateAsync(CancellationToken token)
|
||||
{
|
||||
var statePath = this.GetStatePath();
|
||||
if (string.IsNullOrWhiteSpace(statePath))
|
||||
return;
|
||||
|
||||
var directory = Path.GetDirectoryName(statePath);
|
||||
if (!string.IsNullOrWhiteSpace(directory))
|
||||
Directory.CreateDirectory(directory);
|
||||
|
||||
var persistedState = new PersistedEmbeddingState
|
||||
{
|
||||
DataSources = this.manifests
|
||||
};
|
||||
|
||||
var json = JsonSerializer.Serialize(persistedState, this.jsonOptions);
|
||||
await File.WriteAllTextAsync(statePath, json, token);
|
||||
}
|
||||
|
||||
private async Task ResetPersistedStateAsync(string dataSourcename, string dataSourceId, VectorStoreClient? vectorStore, CancellationToken token)
|
||||
{
|
||||
await this.EnsureStateLoadedAsync(token);
|
||||
this.manifests.Remove(dataSourceId);
|
||||
await this.DeleteCollectionAsync(this.GetCollectionName(dataSourcename, dataSourceId), vectorStore, token);
|
||||
await this.SaveStateAsync(token);
|
||||
await embeddingState.DeleteDataSourceAsync(dataSourceId, token);
|
||||
logger.LogInformation("Reset persisted embedding state for data source '{DataSourceId}'.", dataSourceId);
|
||||
}
|
||||
|
||||
private string GetStatePath()
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(SettingsManager.ConfigDirectory))
|
||||
return string.Empty;
|
||||
|
||||
return Path.Combine(SettingsManager.ConfigDirectory, STATE_FILENAME);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user