Spaces:
Sleeping
Sleeping
Jakaria commited on
Commit ·
02e3818
1
Parent(s): 5932508
Add Bangla model API
Browse files
rag.py
CHANGED
|
@@ -10,8 +10,24 @@ from config import VECTOR_DIR, EMBED_MODEL, GROQ_API_KEY, GROQ_MODEL
|
|
| 10 |
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
|
| 11 |
|
| 12 |
def reset_memory():
|
|
|
|
| 13 |
memory.clear()
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
def build_chain():
|
| 16 |
embeddings = HuggingFaceEmbeddings(model_name=EMBED_MODEL)
|
| 17 |
vectordb = FAISS.load_local(VECTOR_DIR, embeddings, allow_dangerous_deserialization=True)
|
|
@@ -19,12 +35,12 @@ def build_chain():
|
|
| 19 |
|
| 20 |
llm = ChatGroq(model=GROQ_MODEL, api_key=GROQ_API_KEY, temperature=0.1)
|
| 21 |
|
| 22 |
-
# ----
|
| 23 |
chat_prompt = ChatPromptTemplate.from_messages([
|
| 24 |
SystemMessagePromptTemplate.from_template(
|
| 25 |
"You are a helpful financial expert assistant with 10 years of experience. "
|
| 26 |
"Answer all questions using the context retrieved from the PDF. "
|
| 27 |
-
"Always include
|
| 28 |
),
|
| 29 |
HumanMessagePromptTemplate.from_template(
|
| 30 |
"""Context:
|
|
@@ -33,17 +49,32 @@ def build_chain():
|
|
| 33 |
Question:
|
| 34 |
{question}
|
| 35 |
|
| 36 |
-
Answer the question clearly and concisely. Always start answers
|
| 37 |
-
Include the reference in the format: Reference: page:X
|
| 38 |
|
| 39 |
-
|
| 40 |
Question: What is the purpose of the financial policy objectives and strategies statement?
|
| 41 |
Answer: The purpose of the financial policy objectives and strategies statement is to make transparent the Government’s financial strategies and to establish a benchmark for evaluating the Government’s conduct of financial policy.
|
| 42 |
Reference: page:5
|
| 43 |
|
| 44 |
-
Question:
|
| 45 |
-
Answer: The
|
| 46 |
-
Reference: page:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
Now answer the user's question in the same format:
|
| 49 |
"""
|
|
@@ -58,7 +89,11 @@ Now answer the user's question in the same format:
|
|
| 58 |
)
|
| 59 |
return chain
|
| 60 |
|
| 61 |
-
def answer(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
chain = build_chain()
|
| 63 |
result = chain.invoke({"question": question})
|
| 64 |
return result.get("answer") or result.get("result") or "No answer."
|
|
|
|
| 10 |
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
|
| 11 |
|
| 12 |
def reset_memory():
|
| 13 |
+
"""Clear previous conversation memory (use after ingesting a new PDF)"""
|
| 14 |
memory.clear()
|
| 15 |
|
| 16 |
+
def normalize_question(question: str) -> str:
|
| 17 |
+
"""
|
| 18 |
+
Ensure the question starts with 'What' if not already starting
|
| 19 |
+
with Who/When/Where/Why/How/What.
|
| 20 |
+
"""
|
| 21 |
+
question = question.strip()
|
| 22 |
+
if question == "":
|
| 23 |
+
return "What?"
|
| 24 |
+
|
| 25 |
+
first_word = question.split()[0].lower()
|
| 26 |
+
wh_words = ["what", "who", "when", "where", "why", "how","tell me about"]
|
| 27 |
+
if first_word not in wh_words:
|
| 28 |
+
question = "What " + question[0].lower() + question[1:] if len(question) > 0 else "What?"
|
| 29 |
+
return question
|
| 30 |
+
|
| 31 |
def build_chain():
|
| 32 |
embeddings = HuggingFaceEmbeddings(model_name=EMBED_MODEL)
|
| 33 |
vectordb = FAISS.load_local(VECTOR_DIR, embeddings, allow_dangerous_deserialization=True)
|
|
|
|
| 35 |
|
| 36 |
llm = ChatGroq(model=GROQ_MODEL, api_key=GROQ_API_KEY, temperature=0.1)
|
| 37 |
|
| 38 |
+
# ---- System + Human Prompt with diverse few-shot examples ----
|
| 39 |
chat_prompt = ChatPromptTemplate.from_messages([
|
| 40 |
SystemMessagePromptTemplate.from_template(
|
| 41 |
"You are a helpful financial expert assistant with 10 years of experience. "
|
| 42 |
"Answer all questions using the context retrieved from the PDF. "
|
| 43 |
+
"Always include reference (page or section) where the information was found."
|
| 44 |
),
|
| 45 |
HumanMessagePromptTemplate.from_template(
|
| 46 |
"""Context:
|
|
|
|
| 49 |
Question:
|
| 50 |
{question}
|
| 51 |
|
| 52 |
+
Answer the question clearly and concisely. Always start answers naturally (What, Who, When, Where, Why, How) and include references in the format: Reference: page:X or section name.
|
|
|
|
| 53 |
|
| 54 |
+
Few-shot examples:
|
| 55 |
Question: What is the purpose of the financial policy objectives and strategies statement?
|
| 56 |
Answer: The purpose of the financial policy objectives and strategies statement is to make transparent the Government’s financial strategies and to establish a benchmark for evaluating the Government’s conduct of financial policy.
|
| 57 |
Reference: page:5
|
| 58 |
|
| 59 |
+
Question: Who is responsible for preparing the annual budget?
|
| 60 |
+
Answer: The Ministry of Finance is responsible for preparing the annual budget, ensuring alignment with government policies.
|
| 61 |
+
Reference: page:12
|
| 62 |
+
|
| 63 |
+
Question: When is the fiscal report published each year?
|
| 64 |
+
Answer: The fiscal report is published annually in September to provide transparency on government revenues and expenditures.
|
| 65 |
+
Reference: page:20
|
| 66 |
+
|
| 67 |
+
Question: Where can one find the details of government subsidies in the document?
|
| 68 |
+
Answer: Details of government subsidies are provided in the fiscal policy section under the heading 'Subsidies and Support Programs'.
|
| 69 |
+
Reference: page:15
|
| 70 |
+
|
| 71 |
+
Question: Why is the financial risk management framework important?
|
| 72 |
+
Answer: The financial risk management framework is important to identify, assess, and mitigate potential risks in government financial operations.
|
| 73 |
+
Reference: page:18
|
| 74 |
+
|
| 75 |
+
Question: How does the government evaluate financial policy effectiveness?
|
| 76 |
+
Answer: The government evaluates financial policy effectiveness using key performance indicators and benchmarking against fiscal objectives.
|
| 77 |
+
Reference: page:22
|
| 78 |
|
| 79 |
Now answer the user's question in the same format:
|
| 80 |
"""
|
|
|
|
| 89 |
)
|
| 90 |
return chain
|
| 91 |
|
| 92 |
+
def answer(user_question: str) -> str:
|
| 93 |
+
"""
|
| 94 |
+
Normalize the user's question and ask the RAG chain.
|
| 95 |
+
"""
|
| 96 |
+
question = normalize_question(user_question)
|
| 97 |
chain = build_chain()
|
| 98 |
result = chain.invoke({"question": question})
|
| 99 |
return result.get("answer") or result.get("result") or "No answer."
|