-
-
Notifications
You must be signed in to change notification settings - Fork 35
Feat/cost optimized query pipeline #232
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
Merged
shrixtacy
merged 2 commits into
shrixtacy:master
from
ishanraj953:feat/cost-optimized-query-pipeline
Mar 31, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| """ | ||
| Cost-Optimized Query Processing System for AI Council. | ||
|
|
||
| Pipeline (left-to-right): | ||
|
|
||
| User Input | ||
| → QueryCache (short-circuit on cache hit) | ||
| → EmbeddingEngine (dense vector representation) | ||
| → VectorStore (top-k nearest-neighbour search) | ||
| → TopicClassifier (topic label + context chunks) | ||
| → SmartQueryDecomposer (sub-queries + dependency graph) | ||
| → ModelRouter (cheap / mid / expensive tier) | ||
| → TokenOptimizer (prompt compression + RAG cherry-pick) | ||
| → Execution (parallel, via existing orchestration) | ||
| → ResponseAggregator (merge + CostReport) | ||
| → QueryCache.store() | ||
| → PipelineResult | ||
|
|
||
| Public API:: | ||
|
|
||
| from ai_council.query_pipeline import QueryPipeline, PipelineConfig | ||
|
|
||
| pipeline = QueryPipeline.from_config() | ||
| result = await pipeline.process("Explain quicksort and give Python code") | ||
| print(result.cost_report) | ||
| """ | ||
|
|
||
| from .config import PipelineConfig | ||
| from .embeddings import EmbeddingEngine | ||
| from .vector_store import VectorStore, SearchResult | ||
| from .topic_classifier import TopicClassifier, ClassificationResult | ||
| from .query_decomposer import SmartQueryDecomposer, DecompositionResult, SubQuery | ||
| from .model_router import ModelRouter, RoutingDecision, ModelTier | ||
| from .token_optimizer import TokenOptimizer, OptimizedPrompt | ||
| from .cache import QueryCache, CacheStats | ||
| from .pipeline import QueryPipeline, PipelineResult, CostReport | ||
|
|
||
| __all__ = [ | ||
| # top-level pipeline | ||
| "QueryPipeline", | ||
| "PipelineResult", | ||
| "CostReport", | ||
| "PipelineConfig", | ||
| # individual components (composable) | ||
| "EmbeddingEngine", | ||
| "VectorStore", | ||
| "SearchResult", | ||
| "TopicClassifier", | ||
| "ClassificationResult", | ||
| "SmartQueryDecomposer", | ||
| "DecompositionResult", | ||
| "SubQuery", | ||
| "ModelRouter", | ||
| "RoutingDecision", | ||
| "ModelTier", | ||
| "TokenOptimizer", | ||
| "OptimizedPrompt", | ||
| "QueryCache", | ||
| "CacheStats", | ||
| ] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,212 @@ | ||
| """QueryCache — two-level LRU cache for query results. | ||
|
|
||
| Level 1: In-memory ``OrderedDict`` LRU (always available). | ||
| Level 2: ``diskcache`` persistence (optional, activated when installed). | ||
|
|
||
| Cache keys are SHA-256 hashes of the normalised query text, so the cache | ||
| is resilient to minor whitespace/punctuation variations. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import hashlib | ||
| import logging | ||
| import time | ||
| from collections import OrderedDict | ||
| from dataclasses import dataclass, field | ||
| from typing import Any, Dict, Optional | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| def _normalise(query: str) -> str: | ||
| """Normalise a query for cache key generation.""" | ||
| return " ".join(query.lower().split()) | ||
|
|
||
|
|
||
| def _make_key(query: str) -> str: | ||
| return hashlib.sha256(_normalise(query).encode()).hexdigest() | ||
|
|
||
|
|
||
| # ───────────────────────────────────────────────────────────────────────────── | ||
| # Data classes | ||
| # ───────────────────────────────────────────────────────────────────────────── | ||
|
|
||
| @dataclass | ||
| class CachedResponse: | ||
| query_key: str | ||
| result: Any | ||
| stored_at: float = field(default_factory=time.time) | ||
| ttl_seconds: int = 3600 | ||
| hit_count: int = 0 | ||
|
|
||
| def is_expired(self) -> bool: | ||
| return (time.time() - self.stored_at) > self.ttl_seconds | ||
|
|
||
|
|
||
| @dataclass | ||
| class CacheStats: | ||
| hits: int = 0 | ||
| misses: int = 0 | ||
| evictions: int = 0 | ||
| size: int = 0 | ||
|
|
||
| @property | ||
| def hit_rate(self) -> float: | ||
| total = self.hits + self.misses | ||
| return self.hits / total if total else 0.0 | ||
|
|
||
| @property | ||
| def miss_rate(self) -> float: | ||
| return 1.0 - self.hit_rate | ||
|
|
||
|
|
||
| # ───────────────────────────────────────────────────────────────────────────── | ||
| # QueryCache | ||
| # ───────────────────────────────────────────────────────────────────────────── | ||
|
|
||
| class QueryCache: | ||
| """Two-level LRU query cache. | ||
|
|
||
| Args: | ||
| max_memory_entries: Maximum entries in the in-memory LRU. | ||
| ttl_seconds: Default time-to-live for cached entries. | ||
| persist: Enable diskcache persistence (requires ``diskcache``). | ||
| persist_path: Path for the diskcache directory. | ||
|
|
||
| Example:: | ||
|
|
||
| cache = QueryCache(max_memory_entries=256, ttl_seconds=3600) | ||
| cache.store("What is quicksort?", {"answer": "..."}) | ||
| hit = cache.lookup("What is quicksort?") | ||
| assert hit is not None | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| max_memory_entries: int = 512, | ||
| ttl_seconds: int = 3600, | ||
| persist: bool = False, | ||
| persist_path: str = "~/.ai_council/cache/query_pipeline", | ||
| ): | ||
| self._max = max_memory_entries | ||
| self._ttl = ttl_seconds | ||
| self._mem: OrderedDict[str, CachedResponse] = OrderedDict() | ||
| self._stats = CacheStats() | ||
| self._disk: Optional[Any] = None | ||
|
|
||
| if persist: | ||
| self._disk = self._init_disk(persist_path) | ||
|
|
||
| # ── Disk cache init ─────────────────────────────────────────────────────── | ||
|
|
||
| @staticmethod | ||
| def _init_disk(path: str) -> Optional[Any]: | ||
| try: | ||
| import diskcache # type: ignore | ||
| import os | ||
| resolved = os.path.expanduser(path) | ||
| dc = diskcache.Cache(resolved, size_limit=256 * 1024 * 1024) | ||
| logger.info("QueryCache: diskcache persisted to '%s'.", resolved) | ||
| return dc | ||
| except ImportError: | ||
| logger.warning("QueryCache: diskcache not installed; memory-only mode.") | ||
| return None | ||
| except Exception as exc: | ||
| logger.warning("QueryCache: failed to init diskcache (%s); memory-only mode.", exc) | ||
| return None | ||
|
|
||
| # ── Public API ──────────────────────────────────────────────────────────── | ||
|
|
||
| def lookup(self, query: str) -> Optional[Any]: | ||
| """Return the cached result for *query*, or ``None`` on a miss/expiry.""" | ||
| key = _make_key(query) | ||
|
|
||
| # Level 1: memory | ||
| if key in self._mem: | ||
| entry = self._mem[key] | ||
| if entry.is_expired(): | ||
| del self._mem[key] | ||
| self._stats.evictions += 1 | ||
| else: | ||
| self._mem.move_to_end(key) | ||
| entry.hit_count += 1 | ||
| self._stats.hits += 1 | ||
| logger.debug("QueryCache HIT (memory) for key=%s...", key[:12]) | ||
| return entry.result | ||
|
|
||
| # Level 2: disk | ||
| if self._disk is not None: | ||
| try: | ||
| data = self._disk.get(key) | ||
| if data is not None: | ||
| # Promote to memory | ||
| self._mem_store(key, data, self._ttl) | ||
| self._stats.hits += 1 | ||
| logger.debug("QueryCache HIT (disk) for key=%s...", key[:12]) | ||
| return data | ||
| except Exception as exc: | ||
| logger.warning("QueryCache disk lookup failed: %s", exc) | ||
|
|
||
| self._stats.misses += 1 | ||
| return None | ||
|
|
||
| def store(self, query: str, result: Any, ttl: Optional[int] = None) -> None: | ||
| """Cache *result* under *query* for *ttl* seconds.""" | ||
| key = _make_key(query) | ||
| effective_ttl = ttl if ttl is not None else self._ttl | ||
|
|
||
| self._mem_store(key, result, effective_ttl) | ||
|
|
||
| if self._disk is not None: | ||
| try: | ||
| self._disk.set(key, result, expire=effective_ttl) | ||
| except Exception as exc: | ||
| logger.warning("QueryCache disk store failed: %s", exc) | ||
|
|
||
| logger.debug("QueryCache stored key=%s... (ttl=%ds)", key[:12], effective_ttl) | ||
|
|
||
| def invalidate(self, query: str) -> bool: | ||
| """Remove a single entry. Returns True if it existed.""" | ||
| key = _make_key(query) | ||
| found = False | ||
| if key in self._mem: | ||
| del self._mem[key] | ||
| found = True | ||
| if self._disk is not None: | ||
| try: | ||
| found = self._disk.delete(key) or found | ||
| except Exception: | ||
| pass | ||
| return found | ||
|
|
||
| def clear(self) -> None: | ||
| """Clear all cached entries (memory + disk).""" | ||
| self._mem.clear() | ||
| if self._disk is not None: | ||
| try: | ||
| self._disk.clear() | ||
| except Exception: | ||
| pass | ||
| logger.info("QueryCache cleared.") | ||
|
|
||
| def stats(self) -> CacheStats: | ||
| self._stats.size = len(self._mem) | ||
| return self._stats | ||
|
|
||
| # ── Internals ───────────────────────────────────────────────────────────── | ||
|
|
||
| def _mem_store(self, key: str, result: Any, ttl: int) -> None: | ||
| if key in self._mem: | ||
| self._mem.move_to_end(key) | ||
| else: | ||
| if len(self._mem) >= self._max: | ||
| # Evict LRU | ||
| evicted_key = next(iter(self._mem)) | ||
| del self._mem[evicted_key] | ||
| self._stats.evictions += 1 | ||
| self._mem[key] = CachedResponse( | ||
| query_key=key, | ||
| result=result, | ||
| ttl_seconds=ttl, | ||
| ) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
The module docstring shows
pipeline = QueryPipeline.from_config(), butQueryPipelineexposesbuild()(and nofrom_config). This makes the public API example incorrect for users. Update the docstring to use the actual factory method (or add the missingfrom_configalias).