Spaces:
Sleeping
Sleeping
Update chatbot_rag.py
Browse files- chatbot_rag.py +18 -1
chatbot_rag.py
CHANGED
|
@@ -3,6 +3,7 @@ from langchain_community.embeddings import HuggingFaceEmbeddings
|
|
| 3 |
from langchain_community.llms import HuggingFacePipeline
|
| 4 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
|
| 5 |
from langchain.chains import RetrievalQA
|
|
|
|
| 6 |
import traceback
|
| 7 |
|
| 8 |
def build_qa():
|
|
@@ -39,13 +40,29 @@ def build_qa():
|
|
| 39 |
# 4. QA Chain with retrieval
|
| 40 |
print("🔹 Building RetrievalQA...")
|
| 41 |
retriever = vectorstore.as_retriever(search_kwargs={"k": 3})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
qa = RetrievalQA.from_chain_type(
|
| 43 |
llm=llm,
|
| 44 |
retriever=retriever,
|
|
|
|
|
|
|
| 45 |
return_source_documents=False,
|
| 46 |
-
chain_type="stuff" # simplest chain, passes context + question
|
| 47 |
)
|
| 48 |
|
|
|
|
| 49 |
print("✅ QA pipeline ready.")
|
| 50 |
return qa
|
| 51 |
|
|
|
|
| 3 |
from langchain_community.llms import HuggingFacePipeline
|
| 4 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
|
| 5 |
from langchain.chains import RetrievalQA
|
| 6 |
+
from langchain.prompts import PromptTemplate
|
| 7 |
import traceback
|
| 8 |
|
| 9 |
def build_qa():
|
|
|
|
| 40 |
# 4. QA Chain with retrieval
|
| 41 |
print("🔹 Building RetrievalQA...")
|
| 42 |
retriever = vectorstore.as_retriever(search_kwargs={"k": 3})
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
template = """
|
| 46 |
+
Use the following context to answer the question at the end.
|
| 47 |
+
If you don't know the answer, just say "I don't know" — do not make up an answer.
|
| 48 |
+
|
| 49 |
+
Context:
|
| 50 |
+
{context}
|
| 51 |
+
|
| 52 |
+
Question: {question}
|
| 53 |
+
Answer (one short sentence):
|
| 54 |
+
"""
|
| 55 |
+
qa_prompt = PromptTemplate(template=template, input_variables=["context", "question"])
|
| 56 |
+
|
| 57 |
qa = RetrievalQA.from_chain_type(
|
| 58 |
llm=llm,
|
| 59 |
retriever=retriever,
|
| 60 |
+
chain_type="stuff",
|
| 61 |
+
chain_type_kwargs={"prompt": qa_prompt},
|
| 62 |
return_source_documents=False,
|
|
|
|
| 63 |
)
|
| 64 |
|
| 65 |
+
|
| 66 |
print("✅ QA pipeline ready.")
|
| 67 |
return qa
|
| 68 |
|