Spaces:
Sleeping
Sleeping
Update chatbot_rag.py
Browse files- chatbot_rag.py +11 -19
chatbot_rag.py
CHANGED
|
@@ -8,18 +8,23 @@ from langchain.chains import RetrievalQA
|
|
| 8 |
|
| 9 |
def build_qa():
|
| 10 |
"""Builds and returns the RAG QA pipeline."""
|
|
|
|
| 11 |
|
| 12 |
# 1. Embeddings
|
|
|
|
| 13 |
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
| 14 |
|
| 15 |
-
# 2. Load vector DB
|
|
|
|
| 16 |
vectorstore = Chroma(
|
| 17 |
persist_directory="db",
|
| 18 |
collection_name="rag-docs",
|
| 19 |
embedding_function=embeddings,
|
| 20 |
)
|
|
|
|
| 21 |
|
| 22 |
-
# 3. LLM
|
|
|
|
| 23 |
model_id = "microsoft/phi-3-mini-4k-instruct"
|
| 24 |
tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
|
| 25 |
model = AutoModelForCausalLM.from_pretrained(
|
|
@@ -27,6 +32,7 @@ def build_qa():
|
|
| 27 |
device_map="auto",
|
| 28 |
torch_dtype="auto"
|
| 29 |
)
|
|
|
|
| 30 |
|
| 31 |
pipe = pipeline(
|
| 32 |
"text-generation",
|
|
@@ -35,10 +41,10 @@ def build_qa():
|
|
| 35 |
max_new_tokens=256,
|
| 36 |
temperature=0.2,
|
| 37 |
)
|
| 38 |
-
|
| 39 |
llm = HuggingFacePipeline(pipeline=pipe)
|
| 40 |
|
| 41 |
-
# 4.
|
|
|
|
| 42 |
retriever = vectorstore.as_retriever(search_kwargs={"k": 3})
|
| 43 |
qa = RetrievalQA.from_chain_type(
|
| 44 |
llm=llm,
|
|
@@ -46,19 +52,5 @@ def build_qa():
|
|
| 46 |
return_source_documents=False
|
| 47 |
)
|
| 48 |
|
|
|
|
| 49 |
return qa
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
# Build at import time (so it's ready when app runs)
|
| 53 |
-
try:
|
| 54 |
-
qa_pipeline = build_qa()
|
| 55 |
-
except Exception as e:
|
| 56 |
-
qa_pipeline = None
|
| 57 |
-
print("❌ Failed to build QA pipeline:", e)
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
def get_answer(query: str) -> str:
|
| 61 |
-
"""Takes user query and returns chatbot response."""
|
| 62 |
-
if qa_pipeline is None:
|
| 63 |
-
return "⚠️ QA pipeline not initialized."
|
| 64 |
-
return qa_pipeline.run(query)
|
|
|
|
| 8 |
|
| 9 |
def build_qa():
|
| 10 |
"""Builds and returns the RAG QA pipeline."""
|
| 11 |
+
print("🚀 Starting QA pipeline...")
|
| 12 |
|
| 13 |
# 1. Embeddings
|
| 14 |
+
print("🔹 Loading embeddings...")
|
| 15 |
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
| 16 |
|
| 17 |
+
# 2. Load vector DB
|
| 18 |
+
print("🔹 Loading Chroma DB...")
|
| 19 |
vectorstore = Chroma(
|
| 20 |
persist_directory="db",
|
| 21 |
collection_name="rag-docs",
|
| 22 |
embedding_function=embeddings,
|
| 23 |
)
|
| 24 |
+
print("📂 Docs in DB:", vectorstore._collection.count())
|
| 25 |
|
| 26 |
+
# 3. LLM
|
| 27 |
+
print("🔹 Loading LLM...")
|
| 28 |
model_id = "microsoft/phi-3-mini-4k-instruct"
|
| 29 |
tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
|
| 30 |
model = AutoModelForCausalLM.from_pretrained(
|
|
|
|
| 32 |
device_map="auto",
|
| 33 |
torch_dtype="auto"
|
| 34 |
)
|
| 35 |
+
print("✅ LLM loaded.")
|
| 36 |
|
| 37 |
pipe = pipeline(
|
| 38 |
"text-generation",
|
|
|
|
| 41 |
max_new_tokens=256,
|
| 42 |
temperature=0.2,
|
| 43 |
)
|
|
|
|
| 44 |
llm = HuggingFacePipeline(pipeline=pipe)
|
| 45 |
|
| 46 |
+
# 4. QA Chain
|
| 47 |
+
print("🔹 Building RetrievalQA...")
|
| 48 |
retriever = vectorstore.as_retriever(search_kwargs={"k": 3})
|
| 49 |
qa = RetrievalQA.from_chain_type(
|
| 50 |
llm=llm,
|
|
|
|
| 52 |
return_source_documents=False
|
| 53 |
)
|
| 54 |
|
| 55 |
+
print("✅ QA pipeline ready.")
|
| 56 |
return qa
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|