File size: 1,429 Bytes
cf1b384
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

OLD_rag.py


==============================================================






from sentence_transformers import SentenceTransformer
import faiss
import os

model = SentenceTransformer('all-MiniLM-L6-v2')

def load_documents():
    docs = []
    for filename in ["milindgatha.txt", "bhaktas.txt", "apologetics.txt", "poc_questions.txt", "satire_offerings.txt"]:
        if os.path.exists(filename):
            with open(filename, "r", encoding="utf-8") as f:
                text = f.read()
                chunks = text.split("\n\n")
                for chunk in chunks:
                    docs.append((chunk.strip(), filename))
    return docs

docs = load_documents()
texts = [doc[0] for doc in docs]
embeddings = model.encode(texts, convert_to_numpy=True)

dimension = embeddings.shape[1]
index = faiss.IndexFlatIP(dimension)
index.add(embeddings)

def ask_baba(question, history):
    q_embedding = model.encode([question])[0]
    scores, ids = index.search(q_embedding.reshape(1, -1), k=3)
    top_chunks = [texts[i] for i in ids[0]]

    # Persona prompt: always directly address question first
    answer = f"πŸ™ **Brahmarshi Baba Milind says:**\n\n"
    answer += f"O seeker, you ask: *'{question}'* β€” know that even this question is sacred.\n\n"

    for chunk in top_chunks:
        answer += f"- {chunk}\n\n"

    answer += "Faith in Baba Milind triumphs over evidence, every single time."
    return answer