uyen13 commited on
Commit
0b1e158
·
verified ·
1 Parent(s): fd311e5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -6
app.py CHANGED
@@ -73,16 +73,16 @@ def postprocess_answer(answer):
73
 
74
  return answer
75
 
76
- # Sửa lại prompt template với context query
77
  template = """Hãy trả lời câu hỏi dựa trên nội dung sau:
78
  {context}
79
 
80
- Câu hỏi: {query}
81
  Trả lời bằng tiếng Việt một cách tự nhiên:"""
82
 
83
  QA_PROMPT = PromptTemplate(
84
  template=template,
85
- input_variables=["context", "query"] # Thêm context vào input variables
86
  )
87
 
88
  def main():
@@ -101,15 +101,16 @@ def main():
101
 
102
  llm = load_llm()
103
 
104
- # Sửa lại cách khởi tạo RetrievalQA
105
  qa_chain = RetrievalQA.from_chain_type(
106
  llm=llm,
107
  chain_type="stuff",
108
  retriever=vectorstore.as_retriever(search_kwargs={"k": 4}),
109
  return_source_documents=True,
 
110
  chain_type_kwargs={
111
  "prompt": QA_PROMPT,
112
- "document_variable_name": "context" # Chỉ định tên biến chứa documents
113
  }
114
  )
115
 
@@ -117,7 +118,8 @@ def main():
117
  if query:
118
  with st.spinner("Đang tổng hợp câu trả lời..."):
119
  try:
120
- result = qa_chain({"query": query})
 
121
  raw_answer = result["result"]
122
  answer = postprocess_answer(raw_answer)
123
 
 
73
 
74
  return answer
75
 
76
+ # Sửa lại prompt template sử dụng 'question' thay vì 'query'
77
  template = """Hãy trả lời câu hỏi dựa trên nội dung sau:
78
  {context}
79
 
80
+ Câu hỏi: {question}
81
  Trả lời bằng tiếng Việt một cách tự nhiên:"""
82
 
83
  QA_PROMPT = PromptTemplate(
84
  template=template,
85
+ input_variables=["context", "question"] # Đổi thành 'question'
86
  )
87
 
88
  def main():
 
101
 
102
  llm = load_llm()
103
 
104
+ # Thêm tham số input_key để ánh xạ đúng tên biến
105
  qa_chain = RetrievalQA.from_chain_type(
106
  llm=llm,
107
  chain_type="stuff",
108
  retriever=vectorstore.as_retriever(search_kwargs={"k": 4}),
109
  return_source_documents=True,
110
+ input_key="question", # Thêm ánh xạ input key
111
  chain_type_kwargs={
112
  "prompt": QA_PROMPT,
113
+ "document_variable_name": "context"
114
  }
115
  )
116
 
 
118
  if query:
119
  with st.spinner("Đang tổng hợp câu trả lời..."):
120
  try:
121
+ # Truyền input dưới dạng dictionary với key đúng
122
+ result = qa_chain({"question": query}) # Đổi thành 'question'
123
  raw_answer = result["result"]
124
  answer = postprocess_answer(raw_answer)
125