The main agent, now fully wired to use a live Ollama client.
| 11 | from rag_system.retrieval.retrievers import GraphRetriever |
| 12 | |
| 13 | class Agent: |
| 14 | """ |
| 15 | The main agent, now fully wired to use a live Ollama client. |
| 16 | """ |
| 17 | def __init__(self, pipeline_configs: Dict[str, Dict], llm_client: OllamaClient, ollama_config: Dict[str, str]): |
| 18 | self.pipeline_configs = pipeline_configs |
| 19 | self.llm_client = llm_client |
| 20 | self.ollama_config = ollama_config |
| 21 | |
| 22 | gen_model = self.ollama_config["generation_model"] |
| 23 | |
| 24 | # Initialize the single, persistent retrieval pipeline for this agent |
| 25 | self.retrieval_pipeline = RetrievalPipeline(pipeline_configs, self.llm_client, self.ollama_config) |
| 26 | |
| 27 | self.verifier = Verifier(llm_client, gen_model) |
| 28 | self.query_decomposer = QueryDecomposer(llm_client, gen_model) |
| 29 | |
| 30 | # ๐ OPTIMIZED: TTL cache now stores embeddings for semantic matching |
| 31 | self._cache_max_size = 100 # fallback size limit for manual eviction helper |
| 32 | self._query_cache: TTLCache = TTLCache(maxsize=self._cache_max_size, ttl=300) |
| 33 | self.semantic_cache_threshold = self.pipeline_configs.get("semantic_cache_threshold", 0.98) |
| 34 | # If set to "session", semantic-cache hits will be restricted to the same chat session. |
| 35 | # Otherwise (default "global") answers can be reused across sessions. |
| 36 | self.cache_scope = self.pipeline_configs.get("cache_scope", "global") # 'global' or 'session' |
| 37 | |
| 38 | # ๐ NEW: In-memory store for conversational history per session |
| 39 | self.chat_histories: LRUCache = LRUCache(maxsize=100) # Stores history for 100 recent sessions |
| 40 | |
| 41 | graph_config = self.pipeline_configs.get("graph_strategy", {}) |
| 42 | if graph_config.get("enabled"): |
| 43 | self.graph_query_translator = GraphQueryTranslator(llm_client, gen_model) |
| 44 | self.graph_retriever = GraphRetriever(graph_config["graph_path"]) |
| 45 | print("Agent initialized with live GraphRAG capabilities.") |
| 46 | else: |
| 47 | print("Agent initialized (GraphRAG disabled).") |
| 48 | |
| 49 | # ---- Load document overviews for fast routing ---- |
| 50 | self._global_overview_path = os.path.join("index_store", "overviews", "overviews.jsonl") |
| 51 | self.doc_overviews: list[str] = [] |
| 52 | self._current_overview_session: str | None = None # cache key to avoid rereading on every query |
| 53 | self._load_overviews(self._global_overview_path) |
| 54 | |
| 55 | def _load_overviews(self, path: str): |
| 56 | """Helper to load overviews from a .jsonl file into self.doc_overviews.""" |
| 57 | import json, os |
| 58 | self.doc_overviews.clear() |
| 59 | if not os.path.exists(path): |
| 60 | return |
| 61 | try: |
| 62 | with open(path, encoding="utf-8") as fh: |
| 63 | for line in fh: |
| 64 | try: |
| 65 | rec = json.loads(line) |
| 66 | if isinstance(rec, dict) and rec.get("overview"): |
| 67 | self.doc_overviews.append(rec["overview"].strip()) |
| 68 | except Exception: |
| 69 | continue |
| 70 | print(f"๐ Loaded {len(self.doc_overviews)} overviews from {path}") |