-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrag_module.py
More file actions
30 lines (27 loc) · 1.08 KB
/
Copy pathrag_module.py
File metadata and controls
30 lines (27 loc) · 1.08 KB
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
# rag_module.py
import json
from typing import Dict
from embedder import embed_text
from vectorstore_local import query_similar
from ai_module import get_ai_response
def _load_config():
with open("config.json", "r", encoding="utf-8") as f:
return json.load(f)
def build_prompt(question: str, passages):
context_block = "\n\n".join(
[f"[Source: {p['meta'].get('source','unknown')}]\n{p['text']}" for p in passages]
)
instructions = (
"You are Jarvis, a helpful assistant. Answer the user's question using ONLY the provided context. "
"If the answer is not in the context, say you don't know. Keep it concise.\n"
)
return f"{instructions}\nContext:\n{context_block}\n\nQuestion: {question}\nAnswer:"
def ask_knowledge(question: str) -> str:
cfg = _load_config()
top_k = cfg["rag"]["top_k"]
q_emb = embed_text(question)
passages = query_similar(q_emb, top_k)
if not passages:
return "I couldn't find anything relevant in your knowledge base."
prompt = build_prompt(question, passages)
return get_ai_response(prompt)