deddoggo commited on
Commit
8860136
·
1 Parent(s): a53f1d8

multi-turns chatbot

Browse files
Files changed (2) hide show
  1. app.py +1 -1
  2. rag_pipeline.py +25 -13
app.py CHANGED
@@ -38,7 +38,7 @@ with gr.Blocks(theme=gr.themes.Soft(), title="Chatbot Luật Giao thông Việt
38
  clear = gr.ClearButton([msg, chatbot])
39
 
40
  def respond(message, chat_history):
41
- bot_message = chat_interface(message, chat_history)
42
  chat_history.append((message, bot_message))
43
  return "", chat_history
44
 
 
38
  clear = gr.ClearButton([msg, chatbot])
39
 
40
  def respond(message, chat_history):
41
+ bot_message = chat_interface(message, COMPONENTS, chat_history)
42
  chat_history.append((message, bot_message))
43
  return "", chat_history
44
 
rag_pipeline.py CHANGED
@@ -79,7 +79,29 @@ def initialize_components(data_path):
79
  "bm25_model": bm25_model
80
  }
81
 
82
- def generate_response(query, components):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83
  """
84
  Tạo câu trả lời cho một query bằng cách sử dụng các thành phần đã được khởi tạo.
85
  """
@@ -96,7 +118,7 @@ def generate_response(query, components):
96
  faiss_index=components["faiss_index"],
97
  chunks_data=components["chunks_data"],
98
  bm25_model=components["bm25_model"],
99
- k=5,
100
  initial_k_multiplier=18
101
  )
102
 
@@ -113,17 +135,7 @@ def generate_response(query, components):
113
  context = "\n\n---\n\n".join(context_parts)
114
 
115
  # 3. Xây dựng Prompt và tạo câu trả lời
116
- prompt = f"""Dưới đây một số thông tin trích dẫn từ văn bản luật giao thông đường bộ Việt Nam.
117
- Hãy SỬ DỤNG CÁC THÔNG TIN NÀY để trả lời câu hỏi một cách chính xác và đầy đủ.
118
- Nếu câu hỏi đưa ra nhiều đáp án thì chọn 1 đáp án đúng nhất.
119
-
120
- ### Thông tin luật:
121
- {context}
122
-
123
- ### Câu hỏi:
124
- {query}
125
-
126
- ### Trả lời:"""
127
 
128
  print("--- Bắt đầu tạo câu trả lời từ LLM ---")
129
  inputs = tokenizer(prompt, return_tensors="pt").to("cuda" if torch.cuda.is_available() else "cpu")
 
79
  "bm25_model": bm25_model
80
  }
81
 
82
+ def build_multiturn_prompt(chat_history, current_query, retrieved_context):
83
+ # Lấy tối đa N lượt gần nhất (tránh token quá dài)
84
+ max_turns = 5
85
+ history_text = ""
86
+ for i, (user_msg, bot_msg) in enumerate(chat_history[-max_turns:]):
87
+ history_text += f"[Người dùng]: {user_msg}\n[Chatbot]: {bot_msg}\n"
88
+
89
+ full_prompt = f"""Bạn là một chatbot trợ lý luật giao thông Việt Nam. Dưới đây là các đoạn hội thoại trước đó, và trích dẫn luật phù hợp để trả lời câu hỏi tiếp theo.
90
+
91
+ ### Lịch sử hội thoại:
92
+ {history_text}
93
+
94
+ ### Thông tin luật:
95
+ {retrieved_context}
96
+
97
+ ### Câu hỏi hiện tại:
98
+ {current_query}
99
+
100
+ ### Trả lời:"""
101
+
102
+ return full_prompt
103
+
104
+ def generate_response(query, components, chat_history=None):
105
  """
106
  Tạo câu trả lời cho một query bằng cách sử dụng các thành phần đã được khởi tạo.
107
  """
 
118
  faiss_index=components["faiss_index"],
119
  chunks_data=components["chunks_data"],
120
  bm25_model=components["bm25_model"],
121
+ k=3,
122
  initial_k_multiplier=18
123
  )
124
 
 
135
  context = "\n\n---\n\n".join(context_parts)
136
 
137
  # 3. Xây dựng Prompt và tạo câu trả lời
138
+ prompt = build_multiturn_prompt(chat_history or [], query, context)
 
 
 
 
 
 
 
 
 
 
139
 
140
  print("--- Bắt đầu tạo câu trả lời từ LLM ---")
141
  inputs = tokenizer(prompt, return_tensors="pt").to("cuda" if torch.cuda.is_available() else "cpu")