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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import time
|
3 |
+
import random
|
4 |
+
import torch
|
5 |
+
import dotenv
|
6 |
+
import ollama
|
7 |
+
import logging
|
8 |
+
import requests
|
9 |
+
import streamlit as st
|
10 |
+
|
11 |
+
from typing import Optional, List
|
12 |
+
dotenv.load_dotenv()
|
13 |
+
logger = logging.getLogger(__name__)
|
14 |
+
|
15 |
+
# Default prompt context (unchanged)
|
16 |
+
DEFAULT_PROMPT_CONTEXT = """Bạn là một trợ lí AI pháp luật Việt Nam, có kiến thức về pháp luật Việt Nam.
|
17 |
+
Dựa vào ngữ cảnh hoặc tài liệu sau hãy trả lời câu hỏi người dùng
|
18 |
+
Ngữ cảnh: {context}
|
19 |
+
Câu hỏi: {question}
|
20 |
+
"""
|
21 |
+
|
22 |
+
class Config:
|
23 |
+
# URL_RETRIEVE = str(os.getenv("URL_RETRIEVE", "http://202.191.56.254:9002/getPredictionOutput"))
|
24 |
+
URL_RETRIEVE = str(os.getenv("URL_RETRIEVE", "http://0.0.0.0:9002/getPredictionOutput"))
|
25 |
+
TOP_K = int(os.getenv("TOP_K", 5))
|
26 |
+
|
27 |
+
|
28 |
+
def prompt_model(top_k_chunks: Optional[List]):
|
29 |
+
res = []
|
30 |
+
for context in top_k_chunks:
|
31 |
+
text = ''
|
32 |
+
if context.get("diem_id", ""):
|
33 |
+
text += "điểm " + context.get("diem_id", "") + " "
|
34 |
+
|
35 |
+
if context.get("khoan_id", ""):
|
36 |
+
text += "khoản " + str(int(context.get("khoan_id"))) + " "
|
37 |
+
|
38 |
+
if context.get("diem_id", ""):
|
39 |
+
text += context.get("diem_id", "") + " "
|
40 |
+
|
41 |
+
if context.get("law_id", ""):
|
42 |
+
if 'ttlt' in context.get("law_id", ""):
|
43 |
+
text += "thông tư liên tịch " + context.get("law_id", "").upper() + " "
|
44 |
+
elif 'tt' in context.get("law_id", ""):
|
45 |
+
text += "thông tư " + context.get("law_id", "").upper() + " "
|
46 |
+
elif 'nđ' in context.get("law_id", ""):
|
47 |
+
text += "nghị định " + context.get("law_id", "").upper() + " "
|
48 |
+
elif 'tb' in context.get("law_id", ""):
|
49 |
+
text += "thông báo " + context.get("law_id", "").upper() + " "
|
50 |
+
else:
|
51 |
+
text += "luật số " + context.get("law_id", "").upper() + " "
|
52 |
+
|
53 |
+
if context.get("title", "") and random.choice([1, 0]):
|
54 |
+
text += context.get("title", "") + " "
|
55 |
+
|
56 |
+
text += context.get("text", "") + " "
|
57 |
+
res.append(text)
|
58 |
+
return res[:Config.TOP_K]
|
59 |
+
|
60 |
+
|
61 |
+
def get_retrieval(query, config):
|
62 |
+
retrieve = {"query": [query]}
|
63 |
+
response = requests.post(config.URL_RETRIEVE, json=retrieve)
|
64 |
+
if response.status_code == 200:
|
65 |
+
return response.json()['predict'][0][0][0]['top_relevant_chunks']
|
66 |
+
else:
|
67 |
+
return []
|
68 |
+
|
69 |
+
|
70 |
+
def model_res_generator(prompt_template, retrieval_results, question):
|
71 |
+
context = '\n'.join(prompt_model(retrieval_results))
|
72 |
+
input_model = prompt_template.format(context=context, question=question)
|
73 |
+
|
74 |
+
stream = ollama.chat(
|
75 |
+
model=st.session_state["model"],
|
76 |
+
messages=[{'role': 'user', 'content': input_model}],
|
77 |
+
options={
|
78 |
+
'temperature': 0.0
|
79 |
+
},
|
80 |
+
stream=True,
|
81 |
+
)
|
82 |
+
|
83 |
+
full_response = ""
|
84 |
+
for chunk in stream:
|
85 |
+
chunk_content = chunk.get("message", {}).get("content", "")
|
86 |
+
if chunk_content:
|
87 |
+
full_response += chunk_content
|
88 |
+
yield full_response
|
89 |
+
|
90 |
+
|
91 |
+
def process_input(prompt_template, user_input, config):
|
92 |
+
if user_input:
|
93 |
+
st.session_state.messages.append({"role": "user", "content": user_input})
|
94 |
+
retrieval_results = get_retrieval(user_input, config)
|
95 |
+
retrieval_results.sort(key=lambda x: x['bi_score'], reverse=True)
|
96 |
+
st.session_state.retrieval_results = retrieval_results[:st.session_state.top_k]
|
97 |
+
st.session_state.queries_and_results[user_input] = st.session_state.retrieval_results
|
98 |
+
st.session_state.selected_query = user_input
|
99 |
+
st.session_state.query_list = list(st.session_state.queries_and_results.keys())
|
100 |
+
st.session_state.messages.append({"role": "assistant", "content": ""})
|
101 |
+
|
102 |
+
for message in st.session_state.messages[-2:-1]:
|
103 |
+
with st.chat_message(message["role"]):
|
104 |
+
st.markdown(message["content"])
|
105 |
+
|
106 |
+
with st.chat_message("assistant"):
|
107 |
+
message_placeholder = st.empty()
|
108 |
+
for chunk in model_res_generator(prompt_template, retrieval_results, user_input):
|
109 |
+
message_placeholder.markdown(chunk + "▌")
|
110 |
+
st.session_state.messages[-1]["content"] = chunk
|
111 |
+
message_placeholder.markdown(st.session_state.messages[-1]["content"])
|
112 |
+
|
113 |
+
st.rerun()
|
114 |
+
|
115 |
+
|
116 |
+
def reset_session_state(config):
|
117 |
+
st.session_state.messages = []
|
118 |
+
st.session_state.retrieval_results = []
|
119 |
+
st.session_state.queries_and_results = {}
|
120 |
+
st.session_state.selected_query = None
|
121 |
+
st.session_state.top_k = config.TOP_K
|
122 |
+
st.session_state.query_list = []
|
123 |
+
|
124 |
+
|
125 |
+
def update_all_queries_results(config):
|
126 |
+
"""Cập nhật lại kết quả của tất cả các truy vấn khi top_k thay đổi"""
|
127 |
+
for query in st.session_state.queries_and_results.keys():
|
128 |
+
retrieval_results = get_retrieval(query, config)
|
129 |
+
retrieval_results.sort(key=lambda x: x['bi_score'], reverse=True)
|
130 |
+
st.session_state.queries_and_results[query] = retrieval_results[:st.session_state.top_k]
|
131 |
+
st.rerun()
|
132 |
+
|
133 |
+
|
134 |
+
|
135 |
+
if __name__ == "__main__":
|
136 |
+
st.set_page_config(page_title="AsklexAI", page_icon="🧊", layout="centered")
|
137 |
+
config = Config()
|
138 |
+
models = [model["name"] for model in ollama.list()["models"]]
|
139 |
+
|
140 |
+
# Initialize session state variables if not already initialized
|
141 |
+
if 'query_list' not in st.session_state:
|
142 |
+
st.session_state.query_list = []
|
143 |
+
if 'messages' not in st.session_state:
|
144 |
+
st.session_state.messages = []
|
145 |
+
if 'queries_and_results' not in st.session_state:
|
146 |
+
st.session_state.queries_and_results = {}
|
147 |
+
if 'model' not in st.session_state:
|
148 |
+
st.session_state.model = ""
|
149 |
+
if 'retrieval_results' not in st.session_state:
|
150 |
+
st.session_state.retrieval_results = []
|
151 |
+
if 'selected_query' not in st.session_state:
|
152 |
+
st.session_state.selected_query = None
|
153 |
+
if 'top_k' not in st.session_state:
|
154 |
+
st.session_state.top_k = 5
|
155 |
+
if 'custom_prompt' not in st.session_state:
|
156 |
+
st.session_state.custom_prompt = DEFAULT_PROMPT_CONTEXT # Set default custom prompt if not yet set
|
157 |
+
|
158 |
+
st.markdown("""
|
159 |
+
<style>
|
160 |
+
.css-1aumxhk {
|
161 |
+
background-color: #F0F2F6; /* Light blue-gray background */
|
162 |
+
}
|
163 |
+
.css-1aumxhk .stMarkdown {
|
164 |
+
color: #333; /* Darker text for better readability */
|
165 |
+
}
|
166 |
+
.css-1aumxhk .stButton>button {
|
167 |
+
background-color: #4A90E2; /* Blue button color */
|
168 |
+
color: white;
|
169 |
+
}
|
170 |
+
.css-1aumxhk .stSelectbox>div {
|
171 |
+
background-color: white;
|
172 |
+
border-color: #4A90E2;
|
173 |
+
}
|
174 |
+
</style>
|
175 |
+
""", unsafe_allow_html=True)
|
176 |
+
|
177 |
+
|
178 |
+
with st.sidebar:
|
179 |
+
if st.button("New Chat", use_container_width=True):
|
180 |
+
reset_session_state(config)
|
181 |
+
|
182 |
+
st.subheader("Danh sách các truy vấn:")
|
183 |
+
selected_query = st.selectbox(
|
184 |
+
"Chọn truy vấn",
|
185 |
+
options=st.session_state.query_list[::-1] + ["--Chọn truy vấn--"],
|
186 |
+
key="query_selectbox"
|
187 |
+
)
|
188 |
+
|
189 |
+
if selected_query != "--Chọn truy vấn--":
|
190 |
+
st.session_state.selected_query = selected_query
|
191 |
+
|
192 |
+
if st.session_state.selected_query:
|
193 |
+
selected_results = st.session_state.queries_and_results[st.session_state.selected_query]
|
194 |
+
for i, result in enumerate(selected_results):
|
195 |
+
with st.expander(f"Top {i+1}: {result['title'][0].upper() + result['title'][1:50]}... (Score: {result['bi_score']:.2f})"):
|
196 |
+
st.markdown(f"**Văn bản:** {result['law_id']}")
|
197 |
+
st.markdown(f"**Tiêu đề:** {result['title'][0].upper() + result['title'][1:]}")
|
198 |
+
st.markdown(f"**Nội dung:** {result['text']}")
|
199 |
+
|
200 |
+
st.subheader("Cài đặt Retrieve")
|
201 |
+
new_top_k = st.slider("Chọn số lượng kết quả top-k:", min_value=1, max_value=30, value=st.session_state.top_k)
|
202 |
+
if new_top_k != st.session_state.top_k:
|
203 |
+
st.session_state.top_k = new_top_k
|
204 |
+
update_all_queries_results(config)
|
205 |
+
|
206 |
+
st.session_state.model = st.selectbox("Chọn mô hình Ollama", models)
|
207 |
+
with st.expander("Custom Prompt", expanded=False):
|
208 |
+
custom_prompt = st.text_area("Prompt Template", value=st.session_state.custom_prompt, height=300)
|
209 |
+
if st.button("Save"):
|
210 |
+
st.session_state.custom_prompt = custom_prompt
|
211 |
+
st.success("Prompt template updated.")
|
212 |
+
st.caption(st.session_state.custom_prompt)
|
213 |
+
|
214 |
+
|
215 |
+
# Use the custom prompt if available, otherwise use the default one
|
216 |
+
prompt_template = st.session_state.custom_prompt
|
217 |
+
|
218 |
+
if len(st.session_state.messages) != 0:
|
219 |
+
for message in st.session_state.messages:
|
220 |
+
with st.chat_message(message["role"]):
|
221 |
+
st.markdown(message["content"])
|
222 |
+
else:
|
223 |
+
with st.chat_message("assistant"):
|
224 |
+
intro_text = "Chào bạn! Tôi là trợ lý pháp luật Việt Nam. Tôi có thể giúp bạn trả lời các câu hỏi và tìm kiếm về pháp luật Việt Nam. Nếu có câu hỏi gì xin vui lòng nhắn bên dưới!"
|
225 |
+
message_placeholder = st.empty()
|
226 |
+
for i in range(len(intro_text) + 1):
|
227 |
+
message_placeholder.markdown(intro_text[:i+1] + "▌")
|
228 |
+
time.sleep(0.005)
|
229 |
+
message_placeholder.markdown(intro_text)
|
230 |
+
st.session_state.messages.append({
|
231 |
+
"role": "assistant",
|
232 |
+
"content": intro_text
|
233 |
+
})
|
234 |
+
|
235 |
+
col1, col2, col3 = st.columns(3)
|
236 |
+
with col1:
|
237 |
+
sg_1 = st.button("Điều kiện áp dụng hợp đồng trong pháp luật Việt Nam?", use_container_width=True)
|
238 |
+
|
239 |
+
with col2:
|
240 |
+
sg_2 = st.button("Quy định về bảo vệ quyền lợi người tiêu dùng?", use_container_width=True)
|
241 |
+
|
242 |
+
with col3:
|
243 |
+
sg_3 = st.button("Quy trình khiếu nại trong pháp luật Việt Nam?", use_container_width=True)
|
244 |
+
|
245 |
+
|
246 |
+
if sg_1:
|
247 |
+
user_input = "Điều kiện áp dụng hợp đồng trong pháp luật Việt Nam?"
|
248 |
+
process_input(prompt_template, user_input, config)
|
249 |
+
elif sg_2:
|
250 |
+
user_input = "Quy định về bảo vệ quyền lợi người tiêu dùng?"
|
251 |
+
process_input(prompt_template, user_input, config)
|
252 |
+
elif sg_3:
|
253 |
+
user_input = "Quy trình khiếu nại trong pháp luật Việt Nam?"
|
254 |
+
process_input(prompt_template, user_input, config)
|
255 |
+
|
256 |
+
user_input = st.chat_input("Nhập tin nhắn của bạn")
|
257 |
+
if user_input:
|
258 |
+
process_input(prompt_template, user_input, config)
|
259 |
+
|