Skip to content

Commit

Permalink
Merge pull request #13 from rayrayraykk/limit
Browse files Browse the repository at this point in the history
Limit the chat history to avoid performance loss of UI & fix robust issue
  • Loading branch information
ZiTao-Li authored Jan 18, 2024
2 parents f3532b5 + 9454b44 commit 473bffd
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 10 deletions.
35 changes: 28 additions & 7 deletions examples/game/app.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
# -*- coding: utf-8 -*-
import copy
import sys
from typing import List
import os
import yaml
import datetime

import agentscope

Expand All @@ -22,6 +21,18 @@
enable_web_ui()

glb_history_chat = []
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]:
Expand All @@ -36,7 +47,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__":
Expand All @@ -63,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(
Expand All @@ -71,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],
Expand All @@ -88,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, "你")
Expand All @@ -112,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()
17 changes: 14 additions & 3 deletions examples/game/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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"]:
Expand Down Expand Up @@ -173,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} 离开餐馆")
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 =============
Expand Down
1 change: 1 addition & 0 deletions examples/game/ruled_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 473bffd

Please sign in to comment.