-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
301 lines (231 loc) · 9.65 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
import os
import tomllib
from datetime import datetime
import pandas as pd
import s3fs
import streamlit as st
import torch
from dotenv import load_dotenv
from langchain_core.prompts import PromptTemplate
from loguru import logger
from src.app.feedbacks import feedback_titles, render_feedback_section
from src.app.history import (
activate_old_conversation,
create_unique_id,
read_history_from_parquet,
restore_history,
snapshot_sidebar_conversations,
summarize_conversation,
)
from src.app.session import initialize_session_state, reset_session_state
from src.app.utils import create_assistant_message, generate_answer_from_context, initialize_clients
from src.config import set_config
from src.model.prompt import question_instructions
from src.utils.utils_vllm import get_models_from_env
# ---------------- CONFIGURATION ---------------- #
load_dotenv(override=True)
# Patch for https://github.com/VikParuchuri/marker/issues/442
torch.classes.__path__ = []
ENGINE = "qdrant"
USE_RERANKING = True
config = set_config(
use_vault=True,
components=["s3", "mlflow", "database", "model"],
models_location={
"url_embedding_model": "ENV_URL_EMBEDDING_MODEL",
"url_generative_model": "ENV_URL_GENERATIVE_MODEL",
"url_reranking_model": "ENV_URL_RERANKING_MODEL",
},
database_manager=ENGINE,
# override={"QDRANT_COLLECTION_NAME": "dirag_experimentation_d9867c0409cf44e1b222f9f5ede05c06"},
)
fs = s3fs.S3FileSystem(endpoint_url=config.get("endpoint_url"))
path_log = os.getenv("PATH_LOG_APP")
# Fix marker warning from torch
torch.classes.__path__ = [os.path.join(torch.__path__[0], torch.classes.__file__)]
models = get_models_from_env(
url_embedding="URL_EMBEDDING_MODEL", url_generative="URL_GENERATIVE_MODEL", url_reranking="URL_RERANKING_MODEL"
)
embedding_model = models.get("embedding")
generative_model = models.get("completion")
reranking_model = models.get("reranking")
# ---------------- INITIALIZATION ---------------- #
DEFAULT_USERNAME = "anonymous"
with open("./src/app/constants.toml", "rb") as f:
messages = tomllib.load(f)
@st.cache_resource(show_spinner=False)
def initialize_clients_cache(config: dict, embedding_model=embedding_model, engine=ENGINE, **kwargs):
return initialize_clients(config=config, embedding_model=embedding_model, engine=engine, **kwargs)
retriever, chat_client = initialize_clients_cache(
config=config,
embedding_model=embedding_model,
use_reranking=False,
url_reranker=os.getenv("URL_RERANKING_MODEL"),
model_reranker=models.get("reranking"),
)
prompt = PromptTemplate.from_template(question_instructions)
# ---------------- STREAMLIT UI ---------------- #
st.set_page_config(page_title="insee.fr assistant")
with open("./src/app/style.css") as f:
css = f.read()
st.markdown(f"<style>{css}</style>", unsafe_allow_html=True)
# ---------------- INITIALIZE SESSION STATES ---------------- #
initialize_session_state(
{
"conversion_history": [],
"history": [],
"feedback": [],
"active_chat_history": None,
"clicked": False,
"username": DEFAULT_USERNAME,
"sidebar_conversations": None,
"just_loaded_history": False,
"has_initialized_conversation": False,
"retriever": [],
}
)
if "unique_id" not in st.session_state:
st.session_state.unique_id = create_unique_id()
if st.session_state.active_chat_history is not None:
st.session_state.unique_id = st.session_state.active_chat_history
unique_id = st.session_state.unique_id
active_user = st.session_state.username
# ---------------- SIDEBAR: HISTORY ---------------- #
sc1, sc2 = st.sidebar.columns((6, 1))
with st.sidebar:
username = st.text_input("username", DEFAULT_USERNAME)
if username != st.session_state.username:
# CREATE SESSION STATE FOR NEW USERNAME IN LEFT SIDEBAR
reset_session_state(
{
"username": username,
"sidebar_conversations": None,
"just_loaded_history": False,
"has_initialized_conversation": False,
"unique_id": create_unique_id, # notice: no parentheses
"history": [],
"feedback": [],
"active_chat_history": None,
}
)
st.rerun()
if st.button("➕ Nouvelle conversation", key="new_convo"):
# RESTART SESSION START FOR NEW CONVERSATION
start_message = create_assistant_message(content=messages["MESSAGE_START"])
reset_session_state(
{
"unique_id": create_unique_id,
"history": [start_message],
"feedback": [],
"active_chat_history": None,
"has_initialized_conversation": True,
"just_loaded_history": False,
}
)
st.rerun()
if st.session_state.username == "anonymous":
st.markdown(messages["MESSAGE_PAST_CONVERSATION_ANON"])
else:
st.markdown(messages["MESSAGE_PAST_CONVERSATION"])
if st.session_state.sidebar_conversations is None:
history_as_parquet = read_history_from_parquet(path_log, username, fs)
old_conversations = [
summarize_conversation(chat_client, generative_model, history)
for history in history_as_parquet
if history is not None
]
st.session_state.sidebar_conversations = old_conversations
# ✅ Save sidebar conversations as a snapshot
if old_conversations:
snapshot_sidebar_conversations(
old_conversations=old_conversations, path_log=path_log, username=username, filesystem=fs
)
for conversations in st.session_state.sidebar_conversations:
convo_id = conversations["id"]
title = conversations["summary"]
is_active = st.session_state.active_chat_history == convo_id
if is_active:
st.markdown(
f'<div class="active-conversation">{title}</div>',
unsafe_allow_html=True,
)
else:
if st.button(title, key=f"{convo_id}", on_click=activate_old_conversation, args=(convo_id, title)):
pass
# ---------------- INITIAL MESSAGE / LOAD HISTORY ---------------- #
if st.session_state.active_chat_history is not None and not st.session_state.just_loaded_history:
# When clicking on an old conversation
id_unique = st.session_state.active_chat_history
# Read and sort history
history = restore_history(path_log, username, id_unique, filesystem=fs)
# Store back to session state
reset_session_state(
{
"history": lambda: history.to_dict(orient="records"),
"unique_id": id_unique,
"just_loaded_history": True,
}
)
st.rerun()
if not st.session_state.has_initialized_conversation and st.session_state.active_chat_history is None:
# SESSION INITIALIZATION (PAGE LANDING OR NEW CONVERSATION)
st.session_state.history = [create_assistant_message(content=messages["MESSAGE_START"])]
st.session_state.has_initialized_conversation = True
# ---------------- CHAT MESSAGES & FEEDBACK ---------------- #
for i, message in enumerate(st.session_state.history):
# Main panel: messages and added widgets
with st.chat_message(message["role"]):
st.markdown(message["content"])
if message["role"] == "assistant" and i > 0:
best_documents = retriever.invoke(st.session_state.history[i - 1]["content"])
# best_documents_df = langchain_documents_to_df(best_documents)
st.session_state.retriever.append(best_documents)
logger.debug(st.session_state.retriever)
feedback_results = [
render_feedback_section(
index=i,
message=message,
title=cfg["title"],
optional_text=cfg["optional_text"],
key_prefix=cfg["key_prefix"],
unique_id=unique_id,
feedback_type=cfg["feedback_type"],
)
for cfg in feedback_titles
]
# edited_df = st.data_editor(best_documents_df)
if len(st.session_state["history"]) > 1:
conversation_history = pd.DataFrame(st.session_state["history"])
feedback_history = pd.DataFrame(st.session_state["feedback"])
conversation_history.to_parquet(
f"{path_log}/{username}/history/{unique_id}.parquet", index=False, filesystem=fs
)
feedback_history.to_parquet(
f"{path_log}/{username}/feedbacks/{unique_id}.parquet", index=False, filesystem=fs
)
# ---------------- HANDLE USER INPUT ---------------- #
if user_query := st.chat_input("Poser une question sur le site insee"):
st.session_state.history.append(
{"role": "user", "content": user_query, "date": datetime.now().strftime("%Y-%m-%d %H:%M:%S"), "id": unique_id}
)
with st.chat_message("user"):
st.markdown(user_query)
with st.chat_message("assistant"):
response = st.write_stream(
generate_answer_from_context(
retriever=retriever,
chat_client=chat_client,
generative_model=generative_model,
prompt=prompt,
question=user_query,
)
)
st.session_state.history.append(
{
"role": "assistant",
"content": response,
"date": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
"id": unique_id,
}
)
st.rerun()