Fixed embeddingCache not yet global

This commit is contained in:
2025-12-25 14:19:25 +01:00
parent b6e01a3f66
commit 25723cb7a4
2 changed files with 20 additions and 5 deletions

View File

@@ -159,7 +159,6 @@ public class Searchdomain
}
entityReader.Close();
modelsInUse = GetModels(entityCache);
embeddingCache = []; // TODO remove this and implement proper remediation to improve performance
}
public List<(float, string)> Search(string query, int? topN = null)
@@ -170,14 +169,29 @@ public class Searchdomain
return [.. cachedResult.Results.Select(r => (r.Score, r.Name))];
}
if (!embeddingCache.TryGetValue(query, out Dictionary<string, float[]>? queryEmbeddings))
bool hasQuery = embeddingCache.TryGetValue(query, out Dictionary<string, float[]>? queryEmbeddings);
bool allModelsInQuery = queryEmbeddings is not null && modelsInUse.All(model => queryEmbeddings.ContainsKey(model));
if (!(hasQuery && allModelsInQuery))
{
queryEmbeddings = Datapoint.GenerateEmbeddings(query, modelsInUse, aIProvider);
queryEmbeddings = Datapoint.GenerateEmbeddings(query, modelsInUse, aIProvider, embeddingCache);
if (embeddingCache.Count < embeddingCacheMaxSize) // TODO add better way of managing cache limit hits
{ // Idea: Add access count to each entry. On limit hit, sort the entries by access count and remove the bottom 10% of entries
if (!embeddingCache.ContainsKey(query))
{
embeddingCache.Add(query, queryEmbeddings);
}
} // TODO implement proper cache remediation for embeddingCache here
else
{
foreach (KeyValuePair<string, float[]> kvp in queryEmbeddings)
{
if (!embeddingCache.ContainsKey(kvp.Key))
{
embeddingCache[query][kvp.Key] = kvp.Value;
}
}
}
}
}
List<(float, string)> result = [];

View File

@@ -1,5 +1,6 @@
@using Server.Models
@using System.Web
@using Shared.Models
@using Server.Services
@using Server