Pisek commited on
Commit ·
68752b9
1
Parent(s): 21401fd
feat: add Thai persona to chatbot and improve form feedback formatting
Browse files
src/chatbot/app_interface.py
CHANGED
|
@@ -2,6 +2,7 @@ import gradio as gr
|
|
| 2 |
from graph_manager import user_input_handler
|
| 3 |
from nodes import form_info, get_form_info, set_form_info
|
| 4 |
import ast
|
|
|
|
| 5 |
|
| 6 |
def chat_message_handler(message_input, history, session_id):
|
| 7 |
response = user_input_handler(message_input, session_id)
|
|
@@ -98,17 +99,29 @@ def update_form_information_handler(form_information):
|
|
| 98 |
# seller_phone_number_input
|
| 99 |
return seller_name_input, seller_address_input, seller_phone_number_input, buyer_name_input, buyer_address_input, transaction_date_input, total_payment_amount_input, expense_justification_input
|
| 100 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
with gr.Blocks() as app:
|
| 102 |
-
session_id = gr.State(
|
| 103 |
form_information = gr.State(form_info)
|
| 104 |
|
| 105 |
new_btn = gr.Button("เริ่มใหม่", variant="primary")
|
| 106 |
-
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
with gr.Tabs():
|
| 109 |
# Chat tab
|
| 110 |
with gr.Tab("แชท"):
|
| 111 |
-
chatbot = gr.Chatbot(type="messages", label="กล่องข้อความ", height=400)
|
| 112 |
with gr.Column():
|
| 113 |
image_input = gr.UploadButton(label="อัปโหลดรูปภาพ 📎", file_types=["image"])
|
| 114 |
message_input = gr.Textbox(placeholder="พิมพ์ข้อความ...", lines=1, container=False)
|
|
@@ -126,6 +139,12 @@ with gr.Blocks() as app:
|
|
| 126 |
outputs=[chatbot, message_input, form_information]
|
| 127 |
)
|
| 128 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
# Form tab
|
| 130 |
with gr.Tab("ฟอร์ม"):
|
| 131 |
gr.Markdown("**ชื่อผู้ขาย**")
|
|
|
|
| 2 |
from graph_manager import user_input_handler
|
| 3 |
from nodes import form_info, get_form_info, set_form_info
|
| 4 |
import ast
|
| 5 |
+
import uuid
|
| 6 |
|
| 7 |
def chat_message_handler(message_input, history, session_id):
|
| 8 |
response = user_input_handler(message_input, session_id)
|
|
|
|
| 99 |
# seller_phone_number_input
|
| 100 |
return seller_name_input, seller_address_input, seller_phone_number_input, buyer_name_input, buyer_address_input, transaction_date_input, total_payment_amount_input, expense_justification_input
|
| 101 |
|
| 102 |
+
def generate_session_id():
|
| 103 |
+
return str(uuid.uuid4())
|
| 104 |
+
|
| 105 |
+
_chat_prefill = [
|
| 106 |
+
gr.ChatMessage(
|
| 107 |
+
role="assistant",
|
| 108 |
+
content="สวัสดีค่ะ ดิฉันชื่อ เอมมี่ ค่ะ ยินดีช่วยคุณเรื่องการนำส่งรายการค่าใช้จ่ายค่ะ คุณสามารถเริ่มต้นด้วยการอัพโหลดรูปภาพของบิล 1 รูปค่ะ")
|
| 109 |
+
]
|
| 110 |
+
|
| 111 |
with gr.Blocks() as app:
|
| 112 |
+
session_id = gr.State(generate_session_id()) # Initialize with a real session ID
|
| 113 |
form_information = gr.State(form_info)
|
| 114 |
|
| 115 |
new_btn = gr.Button("เริ่มใหม่", variant="primary")
|
| 116 |
+
|
| 117 |
+
def reset_chat(session):
|
| 118 |
+
new_session_id = generate_session_id()
|
| 119 |
+
return _chat_prefill, new_session_id
|
| 120 |
|
| 121 |
with gr.Tabs():
|
| 122 |
# Chat tab
|
| 123 |
with gr.Tab("แชท"):
|
| 124 |
+
chatbot = gr.Chatbot(_chat_prefill, type="messages", label="กล่องข้อความ", height=400)
|
| 125 |
with gr.Column():
|
| 126 |
image_input = gr.UploadButton(label="อัปโหลดรูปภาพ 📎", file_types=["image"])
|
| 127 |
message_input = gr.Textbox(placeholder="พิมพ์ข้อความ...", lines=1, container=False)
|
|
|
|
| 139 |
outputs=[chatbot, message_input, form_information]
|
| 140 |
)
|
| 141 |
|
| 142 |
+
new_btn.click(
|
| 143 |
+
fn=reset_chat,
|
| 144 |
+
inputs=[session_id],
|
| 145 |
+
outputs=[chatbot, session_id]
|
| 146 |
+
)
|
| 147 |
+
|
| 148 |
# Form tab
|
| 149 |
with gr.Tab("ฟอร์ม"):
|
| 150 |
gr.Markdown("**ชื่อผู้ขาย**")
|
src/chatbot/form_management/acceptance_criteria.py
CHANGED
|
@@ -26,7 +26,7 @@ acceptance_criteria = {
|
|
| 26 |
"Must not be empty.",
|
| 27 |
"Must be a valid phone number format."
|
| 28 |
],
|
| 29 |
-
"exampleInput": "052-
|
| 30 |
},
|
| 31 |
{
|
| 32 |
"fieldName": "Buyer Name",
|
|
|
|
| 26 |
"Must not be empty.",
|
| 27 |
"Must be a valid phone number format."
|
| 28 |
],
|
| 29 |
+
"exampleInput": "052-1352991"
|
| 30 |
},
|
| 31 |
{
|
| 32 |
"fieldName": "Buyer Name",
|
src/chatbot/llm_engine.py
CHANGED
|
@@ -29,7 +29,7 @@ def generate_expense_info_feedback(acceptance_criteria: dict, form_info: dict) -
|
|
| 29 |
response = client.responses.create(
|
| 30 |
model="gpt-4.1-mini",
|
| 31 |
temperature=0,
|
| 32 |
-
input=feedback_generation_prompt.format(
|
| 33 |
)
|
| 34 |
|
| 35 |
return response.output_text
|
|
|
|
| 29 |
response = client.responses.create(
|
| 30 |
model="gpt-4.1-mini",
|
| 31 |
temperature=0,
|
| 32 |
+
input=feedback_generation_prompt.format(formatted_date, acceptance_criteria, form_info)
|
| 33 |
)
|
| 34 |
|
| 35 |
return response.output_text
|
src/chatbot/nodes.py
CHANGED
|
@@ -78,9 +78,15 @@ def apply_ocr(image_path: str) -> dict:
|
|
| 78 |
|
| 79 |
print("feedback: {}".format(feedback))
|
| 80 |
|
| 81 |
-
return
|
| 82 |
-
|
| 83 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
@tool
|
| 86 |
def edit_form(key: str, value: str) -> dict:
|
|
@@ -101,9 +107,15 @@ def edit_form(key: str, value: str) -> dict:
|
|
| 101 |
feedback = exp_desc_feedback(value)
|
| 102 |
else:
|
| 103 |
feedback = auditor_feedback(form_info)
|
| 104 |
-
return
|
| 105 |
-
|
| 106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
else:
|
| 108 |
print(form_info)
|
| 109 |
print(f"Invalid key: {key}. No changes made.")
|
|
|
|
| 78 |
|
| 79 |
print("feedback: {}".format(feedback))
|
| 80 |
|
| 81 |
+
return f"""
|
| 82 |
+
<OCR_Extraction>
|
| 83 |
+
{form_info}
|
| 84 |
+
</OCR_Extraction>
|
| 85 |
+
|
| 86 |
+
<Feedback>
|
| 87 |
+
{feedback}
|
| 88 |
+
</Feedback>
|
| 89 |
+
"""
|
| 90 |
|
| 91 |
@tool
|
| 92 |
def edit_form(key: str, value: str) -> dict:
|
|
|
|
| 107 |
feedback = exp_desc_feedback(value)
|
| 108 |
else:
|
| 109 |
feedback = auditor_feedback(form_info)
|
| 110 |
+
return f"""
|
| 111 |
+
<OCR_Extraction>
|
| 112 |
+
{form_info}
|
| 113 |
+
</OCR_Extraction>
|
| 114 |
+
|
| 115 |
+
<Feedback>
|
| 116 |
+
{feedback}
|
| 117 |
+
</Feedback>
|
| 118 |
+
"""
|
| 119 |
else:
|
| 120 |
print(form_info)
|
| 121 |
print(f"Invalid key: {key}. No changes made.")
|
src/chatbot/prompts/agent_prompts.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
speaker_system_message = """
|
| 2 |
-
You are a Thai-speaking assistant that helps users submit receipts and fill in a form.
|
|
|
|
| 3 |
Your job is to communicate with the user and determine which tools to use given the user input, like applying OCR to fill in the form information, or editing a form field.
|
| 4 |
|
| 5 |
The form field includes the following keys:
|
|
@@ -13,11 +14,15 @@ Total Payment Amount: The total amount paid.
|
|
| 13 |
Expense Justification: The justification for the expense as it pertains to the project.
|
| 14 |
|
| 15 |
Users cannot add new fields to the form, but they can edit the existing fields.
|
| 16 |
-
Each time the form is revised, feedback will be provided, and your job to convey the feedback to the user.
|
| 17 |
Do not stray from the feedback provided.
|
| 18 |
Do not mention the correct fields to the user, focus on the invalid one shown in the provided feedback.
|
| 19 |
|
| 20 |
Be sure to ask clarifying questions if the user input is unclear when using your tools.
|
| 21 |
If the user has not submitted an image before, go ahead and use the ocr tool without asking for confirmation from the user.
|
| 22 |
If the user has submitted an image before that has been processed, ask to check if they want to overwrite before using the ocr tool.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
"""
|
|
|
|
| 1 |
speaker_system_message = """
|
| 2 |
+
You are a Thai-speaking assistant, named เอมมี่, that helps users submit receipts and fill in a form.
|
| 3 |
+
ีํYour persona is a friendly, helpful female. You will appropriate end a Thai sentence with "ค่ะ".
|
| 4 |
Your job is to communicate with the user and determine which tools to use given the user input, like applying OCR to fill in the form information, or editing a form field.
|
| 5 |
|
| 6 |
The form field includes the following keys:
|
|
|
|
| 14 |
Expense Justification: The justification for the expense as it pertains to the project.
|
| 15 |
|
| 16 |
Users cannot add new fields to the form, but they can edit the existing fields.
|
| 17 |
+
Each time the form is revised, feedback will be provided, and your job to summarize current form information and convey the feedback to the user.
|
| 18 |
Do not stray from the feedback provided.
|
| 19 |
Do not mention the correct fields to the user, focus on the invalid one shown in the provided feedback.
|
| 20 |
|
| 21 |
Be sure to ask clarifying questions if the user input is unclear when using your tools.
|
| 22 |
If the user has not submitted an image before, go ahead and use the ocr tool without asking for confirmation from the user.
|
| 23 |
If the user has submitted an image before that has been processed, ask to check if they want to overwrite before using the ocr tool.
|
| 24 |
+
|
| 25 |
+
Once the form is complete for the first time, you inform the user that they can (1) review the form information themselves with the "ฟอร์ม" tab or (2) review the form information with your help one by one. You will help the user correct or change the information of each field.
|
| 26 |
+
|
| 27 |
+
Once all fields have been reviewed, you will inform the user to use "ฟอร์ม" tab to submit the form.
|
| 28 |
"""
|
src/chatbot/prompts/chat_completion_prompts.py
CHANGED
|
@@ -20,10 +20,17 @@ Explanation: <Explanation of why the value is invalid>
|
|
| 20 |
|
| 21 |
If there are no violations, output "Form is complete and valid".
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
The current date is: {}
|
| 27 |
"""
|
| 28 |
|
| 29 |
expense_description_feedback_prompt = """Determine whether the expense description is valid based on the project tasks described.
|
|
|
|
| 20 |
|
| 21 |
If there are no violations, output "Form is complete and valid".
|
| 22 |
|
| 23 |
+
The current date is: {}.
|
| 24 |
+
|
| 25 |
+
<Acceptance Criteria>
|
| 26 |
+
{}
|
| 27 |
+
</Acceptance Criteria>
|
| 28 |
+
|
| 29 |
+
<Form Information>
|
| 30 |
+
{}
|
| 31 |
+
</Form Information>
|
| 32 |
+
|
| 33 |
|
|
|
|
| 34 |
"""
|
| 35 |
|
| 36 |
expense_description_feedback_prompt = """Determine whether the expense description is valid based on the project tasks described.
|