From c4e9fcb308057b8103616c27222375d5bf0f6a9a Mon Sep 17 00:00:00 2001 From: trducng Date: Wed, 11 Dec 2024 03:19:16 +0000 Subject: [PATCH] fix: improve answer verification progress reporting --- libs/ktem/ktem/pages/chat/__init__.py | 4 +- libs/ktem/ktem/pages/chat/verification.py | 63 +++++++++++++---------- 2 files changed, 39 insertions(+), 28 deletions(-) diff --git a/libs/ktem/ktem/pages/chat/__init__.py b/libs/ktem/ktem/pages/chat/__init__.py index 63a0c781..c3c9471f 100644 --- a/libs/ktem/ktem/pages/chat/__init__.py +++ b/libs/ktem/ktem/pages/chat/__init__.py @@ -467,13 +467,15 @@ class ChatPage(BasePage): ) self.verify_answer_btn.click( + lambda: gr.update(visible=True), + outputs=[self.verification_page.verification_ui], + ).then( self.verification_page.verify_answer, inputs=[ self.chat_panel.chatbot, self.original_retrieval_history, ], outputs=[ - self.verification_page.verification_ui, self.verification_page.verification_result, ], ) diff --git a/libs/ktem/ktem/pages/chat/verification.py b/libs/ktem/ktem/pages/chat/verification.py index 74d9620b..b49a9920 100644 --- a/libs/ktem/ktem/pages/chat/verification.py +++ b/libs/ktem/ktem/pages/chat/verification.py @@ -23,7 +23,7 @@ class VerificationPage(BasePage): def on_building_ui(self): with gr.Accordion( - "Verification Result", + "Verification result", visible=False, ) as self.verification_ui: self.verification_result = gr.HTML() @@ -39,6 +39,9 @@ class VerificationPage(BasePage): ) def highlight_spans(self, text, spans): + if len(spans) == 0: + return text + spans = sorted(spans, key=lambda x: x["start"]) highlighted_text = text[: spans[0]["start"]] for idx, span in enumerate(spans): @@ -65,40 +68,46 @@ class VerificationPage(BasePage): last_evidence ) + if not text_only_evidence: + raise gr.Error("No evidence found.") + gr.Info("Verifying the groundedness of the answer. Please wait...") result = verify_answer_groundedness_azure(query, answer, [text_only_evidence]) verification_output = "

Trust score: {:.2f}

".format( 1 - result["ungroundedPercentage"] ) - verification_output += "

Claims that might be incorrect

" - spans = [ - { - "start": claim["offset"]["codePoint"], - "end": claim["offset"]["codePoint"] + claim["length"]["codePoint"], - } - for claim in result["ungroundedDetails"] - ] - highlighted_text = self.highlight_spans(answer, spans) - highlighted_text = highlighted_text.replace("\n", "
") - verification_output += f"
{highlighted_text}
" - verification_output += "

Rationale

" - print(verification_output) - rationale = "" + if len(result["ungroundedDetails"]) == 0: + verification_output += "

Claims are correct

" + else: + verification_output += "

Claims that might be incorrect

" + spans = [ + { + "start": claim["offset"]["codePoint"], + "end": claim["offset"]["codePoint"] + claim["length"]["codePoint"], + } + for claim in result["ungroundedDetails"] + ] + highlighted_text = self.highlight_spans(answer, spans) + highlighted_text = highlighted_text.replace("\n", "
") + verification_output += f"
{highlighted_text}
" - for claim in result["ungroundedDetails"]: - rationale += Render.collapsible_with_header( - Document( - text="{}".format(claim["reason"]), - metadata={ - "file_name": "{}".format( - claim["text"] - ), - }, + verification_output += "

Rationale

" + rationale = "" + + for claim in result["ungroundedDetails"]: + rationale += Render.collapsible_with_header( + Document( + text="{}".format(claim["reason"]), + metadata={ + "file_name": "{}".format( + claim["text"] + ), + }, + ) ) - ) - verification_output += f"
{rationale}
" + verification_output += f"
{rationale}
" - return gr.update(visible=True), verification_output + return verification_output