1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
"""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))
|