-
Notifications
You must be signed in to change notification settings - Fork 38
Add fast raw memory search path #194
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -22,6 +22,8 @@ | |||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| import asyncio | ||||||||||||||||||||||||||||||||||||
| import logging | ||||||||||||||||||||||||||||||||||||
| import threading | ||||||||||||||||||||||||||||||||||||
| import time | ||||||||||||||||||||||||||||||||||||
| from typing import Any, Callable, Dict, List, Optional | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| from dotenv import load_dotenv | ||||||||||||||||||||||||||||||||||||
|
|
@@ -133,6 +135,12 @@ def __init__( | |||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| self.embed_fn = embed_fn | ||||||||||||||||||||||||||||||||||||
| self._snippet_stores: Dict[str, BaseVectorStore] = {} | ||||||||||||||||||||||||||||||||||||
| self._profile_catalog_cache: Dict[str, tuple[float, List[Dict[str, str]], list]] = {} | ||||||||||||||||||||||||||||||||||||
| self._raw_retrieval_plan_cache: Dict[tuple[tuple[str, ...], bool], tuple[str, ...]] = {} | ||||||||||||||||||||||||||||||||||||
| self._cache_ttl_seconds = 60.0 | ||||||||||||||||||||||||||||||||||||
| self._profile_catalog_cache_max_users = 256 | ||||||||||||||||||||||||||||||||||||
| self._profile_catalog_cache_lock = threading.Lock() | ||||||||||||||||||||||||||||||||||||
| self._raw_retrieval_plan_cache_lock = threading.Lock() | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| logger.info("RetrievalPipeline initialized") | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
|
|
@@ -494,6 +502,17 @@ def _fetch_profile_catalog(self, user_id: str): | |||||||||||||||||||||||||||||||||||
| catalog — list of {topic, sub_topic} for the prompt | ||||||||||||||||||||||||||||||||||||
| raw_results — the full SearchResult list, cached for _search_profile | ||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||
| now = time.monotonic() | ||||||||||||||||||||||||||||||||||||
| with self._profile_catalog_cache_lock: | ||||||||||||||||||||||||||||||||||||
| self._prune_profile_catalog_cache(now) | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| cached = self._profile_catalog_cache.get(user_id) | ||||||||||||||||||||||||||||||||||||
| if cached and now - cached[0] < self._cache_ttl_seconds: | ||||||||||||||||||||||||||||||||||||
| catalog, results = cached[1], cached[2] | ||||||||||||||||||||||||||||||||||||
| self._profile_catalog_cache.pop(user_id) | ||||||||||||||||||||||||||||||||||||
| self._profile_catalog_cache[user_id] = (now, catalog, results) | ||||||||||||||||||||||||||||||||||||
| return catalog, results | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||||||||||||||
| results = self.vector_store.search_by_metadata( | ||||||||||||||||||||||||||||||||||||
| filters={"user_id": user_id, "domain": "profile"}, | ||||||||||||||||||||||||||||||||||||
|
|
@@ -524,8 +543,35 @@ def _fetch_profile_catalog(self, user_id: str): | |||||||||||||||||||||||||||||||||||
| "sub_topic": "", | ||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| with self._profile_catalog_cache_lock: | ||||||||||||||||||||||||||||||||||||
| self._prune_profile_catalog_cache(now) | ||||||||||||||||||||||||||||||||||||
| self._profile_catalog_cache[user_id] = (now, catalog, results) | ||||||||||||||||||||||||||||||||||||
| return catalog, results | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| def _prune_profile_catalog_cache(self, now: float) -> None: | ||||||||||||||||||||||||||||||||||||
| """Bound profile catalog cache by TTL and number of cached users.""" | ||||||||||||||||||||||||||||||||||||
| expired_user_ids = [ | ||||||||||||||||||||||||||||||||||||
| cached_user_id | ||||||||||||||||||||||||||||||||||||
| for cached_user_id, (cached_at, _, _) in self._profile_catalog_cache.items() | ||||||||||||||||||||||||||||||||||||
| if now - cached_at >= self._cache_ttl_seconds | ||||||||||||||||||||||||||||||||||||
| ] | ||||||||||||||||||||||||||||||||||||
| for cached_user_id in expired_user_ids: | ||||||||||||||||||||||||||||||||||||
| self._profile_catalog_cache.pop(cached_user_id, None) | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| while len(self._profile_catalog_cache) >= self._profile_catalog_cache_max_users: | ||||||||||||||||||||||||||||||||||||
| oldest_user_id = next(iter(self._profile_catalog_cache)) | ||||||||||||||||||||||||||||||||||||
| self._profile_catalog_cache.pop(oldest_user_id, None) | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| def raw_retrieval_plan(self, domains: List[str], answer: bool = False) -> tuple[str, ...]: | ||||||||||||||||||||||||||||||||||||
| """Return a cached deterministic raw-search plan for the requested domains.""" | ||||||||||||||||||||||||||||||||||||
| ordered_allowed = ("profile", "temporal", "summary", "snippet", "code") | ||||||||||||||||||||||||||||||||||||
| normalized = tuple(d for d in ordered_allowed if d in set(domains)) | ||||||||||||||||||||||||||||||||||||
| key = (normalized, answer) | ||||||||||||||||||||||||||||||||||||
| with self._raw_retrieval_plan_cache_lock: | ||||||||||||||||||||||||||||||||||||
| if key not in self._raw_retrieval_plan_cache: | ||||||||||||||||||||||||||||||||||||
| self._raw_retrieval_plan_cache[key] = normalized | ||||||||||||||||||||||||||||||||||||
| return self._raw_retrieval_plan_cache[key] | ||||||||||||||||||||||||||||||||||||
|
Comment on lines
+565
to
+573
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| def _format_catalog(self, catalog: List[Dict[str, str]]) -> str: | ||||||||||||||||||||||||||||||||||||
| """Format profile catalog for the system prompt.""" | ||||||||||||||||||||||||||||||||||||
| if not catalog: | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When two concurrent calls for the same
user_idboth miss the cache in the firstwithblock, they both release the lock and both execute the expensivesearch_by_metadataquery. When the secondwithblock is reached, there's no re-check of whether another thread already populated the entry. The second writer overwrites the first with a stalenowtimestamp (captured at the very start of the function). A simple check likeif user_id not in self._profile_catalog_cache:before assigning would avoid the redundant overwrite and protect against cache stampede on concurrent first-hit requests for the same user.