Merge pull request #51 from LD-Reborn/35-implement-enums-for-probmethods-in-the-shared-models

Fixed embeddingCache not yet global
This commit is contained in:
LD50
2025-12-25 14:19:41 +01:00
committed by GitHub
2 changed files with 20 additions and 5 deletions

View File

@@ -159,7 +159,6 @@ public class Searchdomain
} }
entityReader.Close(); entityReader.Close();
modelsInUse = GetModels(entityCache); modelsInUse = GetModels(entityCache);
embeddingCache = []; // TODO remove this and implement proper remediation to improve performance
} }
public List<(float, string)> Search(string query, int? topN = null) 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))]; 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 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 { // 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); 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 = []; List<(float, string)> result = [];

View File

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