Add semantic caching
Some checks failed
CI / lint (pull_request) Failing after 12s
CI / typecheck (pull_request) Successful in 13s
CI / test (pull_request) Failing after 27s

This commit is contained in:
2026-03-04 06:02:24 +00:00
parent 0cdf9cd44e
commit 132ea1c04f
9 changed files with 624 additions and 1 deletions

View File

@@ -67,6 +67,23 @@ async def knowledge_search(
{"time_ms": round((t_embed - t0) * 1000, 1)},
)
# Check semantic cache before vector search
if app.semantic_cache is not None and source is None:
cached = await app.semantic_cache.check(embedding)
if cached is not None:
t_cache = time.perf_counter()
log_structured_entry(
"knowledge_search completed from cache",
"INFO",
{
"embedding_ms": f"{round((t_embed - t0) * 1000, 1)}ms",
"cache_check_ms": f"{round((t_cache - t_embed) * 1000, 1)}ms",
"total_ms": f"{round((t_cache - t0) * 1000, 1)}ms",
"cache_hit": True,
},
)
return cached
# Perform vector search
log_structured_entry("Performing vector search", "INFO")
try:
@@ -98,16 +115,23 @@ async def knowledge_search(
"source_filter": source.value if source is not None else None,
"results_count": len(filtered_results),
"chunks": [s["id"] for s in filtered_results],
"cache_hit": False,
},
)
# Format and return results
formatted = format_search_results(filtered_results)
if not filtered_results:
log_structured_entry(
"No results found for query", "INFO", {"query": query[:100]}
)
return format_search_results(filtered_results)
# Store in semantic cache (only for unfiltered queries with results)
if app.semantic_cache is not None and source is None and filtered_results:
await app.semantic_cache.store(query, formatted, embedding)
return formatted
except Exception as e: # noqa: BLE001
# Catch-all for any unexpected errors