update the qa chain
Browse files- src/streamlit_app.py +14 -10
src/streamlit_app.py
CHANGED
|
@@ -34,13 +34,14 @@ HF_TOKEN = os.environ.get("HF_TOKEN")
|
|
| 34 |
|
| 35 |
prompt = PromptTemplate(
|
| 36 |
input_variables=["context", "question"],
|
| 37 |
-
|
| 38 |
-
"You are a knowledgeable agricultural research assistant.
|
|
|
|
|
|
|
|
|
|
| 39 |
"Context:\n{context}\n\n"
|
| 40 |
-
"Question: {question}\n\n"
|
| 41 |
-
|
| 42 |
-
"Respond ONLY with the answer, nothing else."
|
| 43 |
-
),
|
| 44 |
)
|
| 45 |
|
| 46 |
|
|
@@ -70,7 +71,7 @@ def load_llm():
|
|
| 70 |
def setup_qa():
|
| 71 |
|
| 72 |
retriever = load_retriever()
|
| 73 |
-
llm = load_llm()
|
| 74 |
question_answer_chain = create_stuff_documents_chain(llm,prompt)
|
| 75 |
# chain = create_retrieval_chain(retriever, question_answer_chain)
|
| 76 |
|
|
@@ -79,7 +80,7 @@ def setup_qa():
|
|
| 79 |
|
| 80 |
|
| 81 |
# Streamlit App UI
|
| 82 |
-
st.title("🌾 AgriQuery: RAG-Based
|
| 83 |
|
| 84 |
query = st.text_input("Ask a question related to agriculture:")
|
| 85 |
|
|
@@ -87,6 +88,9 @@ if query:
|
|
| 87 |
qa = setup_qa()
|
| 88 |
with st.spinner("Thinking..."):
|
| 89 |
result = qa.invoke({"query":query})
|
| 90 |
-
|
| 91 |
-
|
|
|
|
|
|
|
|
|
|
| 92 |
|
|
|
|
| 34 |
|
| 35 |
prompt = PromptTemplate(
|
| 36 |
input_variables=["context", "question"],
|
| 37 |
+
template=(
|
| 38 |
+
"You are a knowledgeable agricultural research assistant.\n"
|
| 39 |
+
"Use the context to answer the question.\n"
|
| 40 |
+
"If you don't know, say \"I don't know\".\n\n"
|
| 41 |
+
"Return ONLY the answer between <answer> and </answer>.\n\n"
|
| 42 |
"Context:\n{context}\n\n"
|
| 43 |
+
"Question: {question}\n\n<answer>"
|
| 44 |
+
)
|
|
|
|
|
|
|
| 45 |
)
|
| 46 |
|
| 47 |
|
|
|
|
| 71 |
def setup_qa():
|
| 72 |
|
| 73 |
retriever = load_retriever()
|
| 74 |
+
llm = load_llm().bind(stop=["</answer>"])
|
| 75 |
question_answer_chain = create_stuff_documents_chain(llm,prompt)
|
| 76 |
# chain = create_retrieval_chain(retriever, question_answer_chain)
|
| 77 |
|
|
|
|
| 80 |
|
| 81 |
|
| 82 |
# Streamlit App UI
|
| 83 |
+
st.title("🌾 AgriQuery: RAG-Based Research Assistant")
|
| 84 |
|
| 85 |
query = st.text_input("Ask a question related to agriculture:")
|
| 86 |
|
|
|
|
| 88 |
qa = setup_qa()
|
| 89 |
with st.spinner("Thinking..."):
|
| 90 |
result = qa.invoke({"query":query})
|
| 91 |
+
raw = result["result"]
|
| 92 |
+
answer = raw.split("<answer>", 1)[-1].split("</answer>", 1)[0].strip()
|
| 93 |
+
|
| 94 |
+
st.success(answer)
|
| 95 |
+
# st.success(result['source_documents'])
|
| 96 |
|