写在前面
是的谁家好人一礼拜用两个lib搭chatbot。Taipy搭的长下面这样:
Taipy 本身其实不支持聊天,但是因为可以自定义css之类的,还是可以做一个chatbot。但是Taipy的缺点是对于事件/动作/功能的支持比较少。。比如说折叠,它自带的expandable就只有向上折叠。
这里只放页面搭建部分的代码了。
with tgb.Page() as page:
with tgb.layout(columns="300px 1"):
# 左侧框
with tgb.part(class_name="sidebar"):
tgb.text("## Taipy x GPT-3.5", mode="md")
tgb.button(
"New Conversation",
class_name="fullwidth plain",
id="reset_app_button",
on_action=reset_chat,
)
tgb.text(f"{folder_string}", mode="md", id="folder_selection", )
tgb.button(
"Browse Folder",
class_name="fullwidth",
on_action=open_folder,
)
tgb.text("Copy your website:", mode="md")
tgb.input(
"{website}",
on_action=load_website,
change_delay=-1,
class_name="fullwidth ",
propagate=True
)
tgb.text("Load local database:", mode="md")
tgb.button(
"Load from database",
class_name="fullwidth",
on_action=load_database,
)
# 右侧聊天界面
with tgb.part(class_name="p1"):
tgb.part(partial="{conv}", height="500px", class_name="card card_chat")
with tgb.part("card mt1"):
tgb.input(
"{query_message}",
on_action=send_message,
change_delay=-1,
label="Write your message:",
class_name="fullwidth",
)
聊天界面的创建
def create_conv(state):
messages_dict = {}
with tgb.Page() as conversation:
for i, message in enumerate(state.messages):
text = message["content"].replace("<br>", "\n").replace('"', "'")
messages_dict[f"message_{i}"] = text
if message['style'] == 'assistant_message':
test_class = "assistant_test"
with tgb.part(class_name="single_message"):
tgb.text(
"assistant",
class_name=test_class,
mode="md",
)
tgb.text(
"{messages_dict['" + f"message_{i}" + "'] if messages_dict else ''}",
class_name=f"message_base {message['style']}",
mode="md",
)
else:
test_class = "user_test"
with tgb.part(class_name="single_message"):
tgb.text(
"{messages_dict['" + f"message_{i}" + "'] if messages_dict else ''}",
class_name=f"message_base {message['style']}",
mode="md",
)
tgb.text(
"user",
class_name=test_class,
mode="md",
)
state.messages_dict = messages_dict
return conversation