-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrag_qa_system.py
More file actions
executable file
·87 lines (63 loc) · 2.5 KB
/
Copy pathrag_qa_system.py
File metadata and controls
executable file
·87 lines (63 loc) · 2.5 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
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
from sentence_transformers import SentenceTransformer
import chromadb
import subprocess
from translation_utils import setup_translation, translate_to_arabic
# Load local embedding model
embedder = SentenceTransformer("all-MiniLM-L6-v2")
# Connect to ChromaDB
chroma_client = chromadb.PersistentClient(path="./chroma_db")
collection = chroma_client.get_or_create_collection(name="dr_x_documents")
def retrieve_relevant_chunks(question: str, k: int = 5):
query_embedding = embedder.encode([question])[0]
results = collection.query(
query_embeddings=[query_embedding],
n_results=k
)
return results["documents"][0], results["metadatas"][0]
def generate_answer_with_ollama(question: str, context: str, model: str = "tinyllama"):
prompt = f"""
You are an expert AI trained to answer questions about scientific documents.
Context:
{context}
Question:
{question}
Answer:"""
result = subprocess.run(
["ollama", "run", model],
input=prompt.encode(),
capture_output=True
)
return result.stdout.decode("utf-8").strip()
# def answer_question(question: str):
# chunks, metadata = retrieve_relevant_chunks(question)
# context = "\n\n".join(chunks)
# answer = generate_answer_with_ollama(question, context)
# return answer
def answer_question(question: str):
# Step 1: retrieve chunks
chunks, metadata = retrieve_relevant_chunks(question)
context = "\n\n".join(chunks)
# Step 2: generate English answer
english_answer = generate_answer_with_ollama(question, context)
# Step 3: translate to Arabic
try:
arabic_answer = translate_to_arabic(english_answer)
except Exception as e:
arabic_answer = "⚠️ Translation failed: " + str(e)
return english_answer, arabic_answer
# if __name__ == "__main__":
# while True:
# user_q = input("\n❓ Ask a question about Dr. X's documents (or type 'exit'): ")
# if user_q.lower() in ['exit', 'quit']:
# break
# response = answer_question(user_q)
# print("\n💬 Answer:\n", response)
if __name__ == "__main__":
setup_translation() # Run once to ensure translation model is installed
while True:
user_q = input("\n❓ Ask a question (or type 'exit'): ")
if user_q.lower() in ['exit', 'quit']:
break
english_ans, arabic_ans = answer_question(user_q)
print("\n💬 English Answer:\n", english_ans)
print("\n🗣️ Arabic Translation:\n", arabic_ans)