Skip to content

Commit

Permalink
Save question answering data as a log file
Browse files Browse the repository at this point in the history
  • Loading branch information
doviettung96 committed May 31, 2024
1 parent ad61c71 commit 20a04a4
Showing 1 changed file with 91 additions and 0 deletions.
91 changes: 91 additions & 0 deletions libs/ktem/ktem/pages/chat/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import asyncio
from copy import deepcopy
from typing import Optional
from pathlib import Path
from filelock import FileLock
from datetime import datetime
import csv

import gradio as gr
from ktem.app import BasePage
Expand All @@ -27,6 +31,7 @@ def __init__(self, app):
def on_building_ui(self):
with gr.Row():
self.chat_state = gr.State(STATE)
self.original_chat_history = gr.State([])
with gr.Column(scale=1, elem_id="conv-settings-panel"):
self.chat_control = ConversationControl(self._app)

Expand Down Expand Up @@ -119,6 +124,14 @@ def on_register_events(self):
+ self._indices_input,
outputs=None,
concurrency_limit=20,
).then(
fn=self.backup_original_history,
inputs=[
self.chat_panel.chatbot
],
outputs=[
self.original_chat_history
]
)

self.chat_panel.regen_btn.click(
Expand Down Expand Up @@ -154,6 +167,16 @@ def on_register_events(self):
fn=self.is_liked,
inputs=[self.chat_control.conversation_id],
outputs=None,
).success(
fn=lambda *inputs: self.save_log(*inputs, log_dir=getattr(flowsettings, "KH_APP_DATA_DIR", "logs")),
inputs=[
self.chat_control.conversation_id,
self.chat_panel.chatbot,
self.original_chat_history,
self._app.settings_state,
self.info_panel,
],
outputs=None
)

self.chat_control.btn_new.click(
Expand Down Expand Up @@ -478,3 +501,71 @@ def regen_fn(
yield chat, refs, new_state

state["app"]["regen"] = False

def backup_original_history(self, chat_history):
original_history = []
for message in chat_history:
original_history.append(message)
return original_history


def save_log(self, conversation_id, chat_history, original_chat_history, settings, info_panel, log_dir):
with Session(engine) as session:
statement = select(Conversation).where(Conversation.id == conversation_id)
result = session.exec(statement).one()

data_source = deepcopy(result.data_source)
likes = data_source.get("likes", [])

if not likes:
return

if not Path(log_dir).exists():
Path(log_dir).mkdir(parents=True)

lock = FileLock(Path(log_dir) / ".lock")
# get current date
today = datetime.now()
formatted_date = today.strftime('%d%m%Y_%H')

feedback = likes[-1][-1]
message_index = likes[-1][0]
current_message = chat_history[message_index[0]]
original_message = original_chat_history[message_index[0]]
is_original = all([current_item == original_item for current_item, original_item in zip(current_message, original_message)])

dataframe = [[
conversation_id,
message_index,
chat_history,
current_message[0],
current_message[1],
is_original,
feedback,
settings,
info_panel
]]


with lock:
log_file = Path(log_dir) / f"{formatted_date}_log.csv"
is_log_file_exist = log_file.is_file()
with open(log_file, "a") as f:
writer = csv.writer(f)
# write headers
if not is_log_file_exist:
writer.writerow([
"Conversation ID",
"Message ID",
"Chat History",
"Question",
"Answer",
"Original/ Rewritten",
"Feedback",
"Settings",
"Evidences",
])
for item in dataframe:
item[4] = item[4].replace('\u0000', '')

writer.writerows(dataframe)

0 comments on commit 20a04a4

Please sign in to comment.