From b574f33389bb9d75ae69421a05e92b07ca89d218 Mon Sep 17 00:00:00 2001 From: "weirui.kwr@alibaba-inc.com" Date: Thu, 18 Jan 2024 10:35:37 +0800 Subject: [PATCH 1/5] chat log limit --- examples/game/app.py | 3 ++- examples/game/customer.py | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/examples/game/app.py b/examples/game/app.py index 17ad46c3c..412b504c2 100644 --- a/examples/game/app.py +++ b/examples/game/app.py @@ -22,6 +22,7 @@ enable_web_ui() glb_history_chat = [] +MAX_NUM_DISPLAY_MSG = 20 def get_chat() -> List[List]: @@ -36,7 +37,7 @@ def get_chat() -> List[List]: if line is not None: glb_history_chat += [line] - return glb_history_chat + return glb_history_chat[-MAX_NUM_DISPLAY_MSG:] if __name__ == "__main__": diff --git a/examples/game/customer.py b/examples/game/customer.py index 2f1e13b7f..6eaed53f4 100644 --- a/examples/game/customer.py +++ b/examples/game/customer.py @@ -49,7 +49,7 @@ def visit(self): # return ( # np.random.binomial( # n=1, - # p=min(10 / 100, 1.0), + # p=min(self.friendship / 100, 1.0), # ) # > 0 # ) From fade36f07f16dcc27eabaa57d52057729f97e80b Mon Sep 17 00:00:00 2001 From: "weirui.kwr@alibaba-inc.com" Date: Thu, 18 Jan 2024 10:42:57 +0800 Subject: [PATCH 2/5] rollback --- examples/game/customer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/game/customer.py b/examples/game/customer.py index 6eaed53f4..2f1e13b7f 100644 --- a/examples/game/customer.py +++ b/examples/game/customer.py @@ -49,7 +49,7 @@ def visit(self): # return ( # np.random.binomial( # n=1, - # p=min(self.friendship / 100, 1.0), + # p=min(10 / 100, 1.0), # ) # > 0 # ) From e05a1683b6d073c6101a2980b3ba8ac00e8fd92e Mon Sep 17 00:00:00 2001 From: "weirui.kwr@alibaba-inc.com" Date: Thu, 18 Jan 2024 11:00:43 +0800 Subject: [PATCH 3/5] fix robust issue --- examples/game/main.py | 15 +++++++++++++-- examples/game/ruled_user.py | 1 + 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/examples/game/main.py b/examples/game/main.py index 098f6d8e5..8cae0c78b 100644 --- a/examples/game/main.py +++ b/examples/game/main.py @@ -57,6 +57,9 @@ def invited_group_chat( msg = None elif answer == "结束邀请对话": break + else: + send_chat_msg("【系统】请重新选择。") + continue for c in invited_customer: msg = c(msg) send_pretty_msg(msg) @@ -141,7 +144,9 @@ def one_on_one_loop(customers, player): break send_pretty_msg(msg) send_chat_msg( - "【系统】请输入“做菜”启动做菜程序,它会按所选定食材产生菜品。" " (对话轮次过多会使得顾客综合满意度下降。)", + "【系统】请输入“做菜”启动做菜程序,它会按所选定食材产生菜品。 \n" + "【系统】对话轮次过多会使得顾客综合满意度下降。 \n" + "【系统】若不输入任何内容直接按回车键,顾客将离开餐馆。", ) msg = player(msg) if len(msg["content"]) == 0 or "[TERMINATE]" in msg["content"]: @@ -201,6 +206,9 @@ def invite_customers(customers): answer = query_answer(select_customer, "invited") if answer == "END": break + if answer not in select_customer: + send_chat_msg("【系统】请重新选择。") + continue invited_customers.append(answer) available_customers.remove(answer) @@ -312,7 +320,10 @@ def main(args) -> None: for c in customers if c.name not in checkpoint.invited_customers ] - checkpoint.visit_customers = one_on_one_loop(rest_customers, player) + checkpoint.visit_customers = one_on_one_loop( + rest_customers, + player, + ) checkpoint.stage_per_night = StagePerNight.MAKING_INVITATION elif checkpoint.stage_per_night == StagePerNight.MAKING_INVITATION: # ============ making invitation decision ============= diff --git a/examples/game/ruled_user.py b/examples/game/ruled_user.py index 5aae7b8e7..a0647d903 100644 --- a/examples/game/ruled_user.py +++ b/examples/game/ruled_user.py @@ -158,6 +158,7 @@ def cook(self): cook_list.clear() elif sel_ingr not in ingredients_list: send_chat_msg("【系统】不可用食材,请重新选择。") + continue else: cook_list.append(sel_ingr) end_query_answer() From 36254e1ade4cd06744ec3c1ad7baba6c8caef63b Mon Sep 17 00:00:00 2001 From: "weirui.kwr@alibaba-inc.com" Date: Thu, 18 Jan 2024 11:03:41 +0800 Subject: [PATCH 4/5] optimize system msg --- examples/game/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/game/main.py b/examples/game/main.py index 8cae0c78b..b18bc1011 100644 --- a/examples/game/main.py +++ b/examples/game/main.py @@ -178,7 +178,7 @@ def one_on_one_loop(customers, player): msg = customer(msg) # print(f"{customer_reply.name}(顾客):" + customer_reply.content) send_pretty_msg(msg) - send_chat_msg("【系统】直接回车以终止对话。") + send_chat_msg("【系统】若不输入任何内容直接按回车键,顾客将离开餐馆。") msg = player(msg) if len(msg["content"]) == 0: send_chat_msg(f"【系统】顾客{customer.name} 离开餐馆") From 9454b44303aaae26757427f2cd9e02049ad773db Mon Sep 17 00:00:00 2001 From: "weirui.kwr@alibaba-inc.com" Date: Thu, 18 Jan 2024 11:49:26 +0800 Subject: [PATCH 5/5] add export button --- examples/game/app.py | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/examples/game/app.py b/examples/game/app.py index 412b504c2..2b62f65d8 100644 --- a/examples/game/app.py +++ b/examples/game/app.py @@ -1,9 +1,8 @@ # -*- coding: utf-8 -*- -import copy -import sys from typing import List import os import yaml +import datetime import agentscope @@ -25,6 +24,17 @@ MAX_NUM_DISPLAY_MSG = 20 +def export_chat_history(): + timestamp = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S") + export_filename = f"chat_history_{timestamp}.txt" + + with open(export_filename, "w", encoding="utf-8") as file: + for role, message in glb_history_chat: + file.write(f"{role}: {message}\n") + + return gr.update(value=export_filename, visible=True) + + def get_chat() -> List[List]: """Load the chat info from the queue, and put it into the history @@ -64,6 +74,7 @@ def start_game(): with gr.Row(): chatbot = GroupChat(label="Dialog", show_label=False, height=600) + with gr.Row(): with gr.Column(): user_chat_input = gr.Textbox( @@ -72,10 +83,7 @@ def start_game(): show_label=False, interactive=True, ) - with gr.Column(): - send_button = gr.Button( - value="发送", - ) + user_chat_bot_suggest = gr.Dataset( label="选择一个", components=[user_chat_input], @@ -89,6 +97,16 @@ def start_game(): outputs=[user_chat_input], ) + with gr.Column(): + send_button = gr.Button( + value="发送", + ) + + with gr.Accordion("导出选项", open=False): + with gr.Column(): + export_button = gr.Button("导出完整游戏记录") + export_output = gr.File(label="下载完整游戏记录", visible=False) + def send_message(msg): send_player_input(msg) send_chat_msg(msg, "你") @@ -113,9 +131,11 @@ def update_suggest(): outputs = [chatbot, user_chat_bot_suggest] send_button.click(send_message, user_chat_input, user_chat_input) + export_button.click(export_chat_history, [], export_output) user_chat_input.submit(send_message, user_chat_input, user_chat_input) demo.load(get_chat, inputs=None, outputs=chatbot, every=0.5) demo.load(update_suggest, outputs=user_chat_bot_suggest, every=0.5) demo.load(start_game) + demo.queue() demo.launch()