"""This is the main module of the package""" import argparse import asyncio import os from functools import partial import dspy import torch from mem0 import Memory from mem0.configs.base import MemoryConfig from gnais.config import Config from gnais.search.classification import classify_search, extract_keywords from gnais.search.corpus import ( create_ensemble_retriever, get_chroma_db, get_docs, init_chroma_db, ) from gnais.search.prompts import GN_FACT_EXTRACTION_PROMPT, GN_UPDATE_MEMORY_PROMPT from gnais.search.rag import rag_search def _digest( query: str, memory: Memory, user_id: str, corpus_path: str, db_path: str ) -> str: """Run the full RAG pipeline for a single query and return the answer. This is a convenience wrapper around the same logic used by the CLI script, packaged as a plain synchronous function. """ async def _run() -> str: docs = get_docs(corpus_path) chroma_db = get_chroma_db(embed_model="Qwen/Qwen3-Embedding-0.6B") decision = classify_search(query).get("decision") retriever = create_ensemble_retriever( chroma_db=chroma_db, docs=docs, keyword_weight=0.7 if decision == "keyword" else 0.5, ) parts = [] async for chunk in rag_search( query=( extract_keywords(query).get("keywords") if decision == "keyword" else query ), retriever=retriever, memory=memory, user_id=user_id, ): parts.append(str(chunk)) return "".join(parts) return asyncio.run(_run()) if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument("query", help="Search query") parser.add_argument("--user-id", default="test-user", help="Mem0 user identity") args = parser.parse_args() torch.manual_seed(Config.SEED) if torch.cuda.is_available(): torch.cuda.manual_seed_all(Config.SEED) dspy.configure(lm=Config.DEFAULT_LLM) os.environ[f"{Config.MEMORY_MODEL.split('/')[0].upper()}_API_KEY"] = Config.API_KEY memory_config = MemoryConfig( custom_fact_extraction_prompt=GN_FACT_EXTRACTION_PROMPT, custom_update_extraction_prompt=GN_UPDATE_MEMORY_PROMPT, llm={ "provider": "litellm", "config": { "model": Config.MEMORY_MODEL, "temperature": 0.1, "max_tokens": 2_000, "api_key": Config.API_KEY, }, }, embedder={ "provider": "huggingface", "config": { "model": "Qwen/Qwen3-Embedding-0.6B", "embedding_dims": 1024, "model_kwargs": { "trust_remote_code": True, "device": "cuda" if torch.cuda.is_available() else "cpu", }, }, }, vector_store={ "provider": "chroma", "config": { "collection_name": "mem0", "host": "localhost", "port": 8001, }, }, ) memory = Memory(config=memory_config) digest = partial( _digest, memory=memory, corpus_path=Config.CORPUS_PATH, db_path=Config.DB_PATH ) print(digest(query=args.query, user_id=args.user_id))