From 166c8106332acabdd8795805250ade1ed0ffb965 Mon Sep 17 00:00:00 2001 From: Jett Wang Date: Mon, 11 Dec 2023 15:27:25 +0800 Subject: [PATCH] Add new application panel and update chatbot command list MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A new application panel file (01_🎛️应用面板.py) is added which has a list of applications and their relevant information. Changes are made to the chatbot to handle education-related commands more effectively. This includes an update on the chatbot's command list and the addition of a quickly selectable command list in the sidebar in the context of educational applications. The Streamlit learning example has also been renamed. --- apps/chatbot.py | 28 +++++-- libs/prompts.py | 3 +- ...24\347\224\250\351\235\242\346\235\277.py" | 77 +++++++++++++++++++ ...26\347\250\213\345\257\274\345\270\210.py" | 3 +- ...04\344\273\266\345\255\246\344\271\240.py" | 0 5 files changed, 102 insertions(+), 9 deletions(-) create mode 100644 "pages/01_\360\237\216\233\357\270\217\345\272\224\347\224\250\351\235\242\346\235\277.py" rename "pages/01_\360\237\244\226Streamlit_\347\273\204\344\273\266\345\255\246\344\271\240.py" => "pages/09_\360\237\244\226Streamlit_\347\273\204\344\273\266\345\255\246\344\271\240.py" (100%) diff --git a/apps/chatbot.py b/apps/chatbot.py index 05f7890..9fe255b 100644 --- a/apps/chatbot.py +++ b/apps/chatbot.py @@ -12,7 +12,7 @@ load_dotenv() -def get_chatbot_page(state_prefix, knowledge_name, sysmsg_func): +def get_chatbot_page(state_prefix, knowledge_name, sysmsg_func, is_edu=False, show_libs=False): page_state = PageSessionState(state_prefix) # st.sidebar.markdown("# 💡Python 编程导师") @@ -22,6 +22,7 @@ def get_chatbot_page(state_prefix, knowledge_name, sysmsg_func): page_state.initn_attr("last_user_msg_processed", True) # 用于标记流式输出是否结束 page_state.initn_attr("streaming_end", True) + page_state.initn_attr("quick_command", "") def end_chat_streaming(): """当停止按钮被点击时执行,用于修改处理标志""" @@ -33,6 +34,15 @@ def start_chat_streaming(): page_state.streaming_end = False page_state.last_user_msg_processed = False + def on_input_prompt(iprompt: str): + if iprompt.strip() == "": + return + page_state.chat_prompt = iprompt + start_chat_streaming() + page_state.add_chat_msg("messages", {"role": "user", "content": page_state.chat_prompt}) + with st.chat_message("user"): + st.write(page_state.chat_prompt) + for msg in page_state.messages: with st.chat_message(msg["role"]): st.write(msg["content"]) @@ -47,11 +57,15 @@ def clear_chat_history(): st.chat_input("请等待上一条消息处理完毕", disabled=True) else: if prompt := st.chat_input("输入你的问题"): - page_state.chat_prompt = prompt - start_chat_streaming() - page_state.add_chat_msg("messages", {"role": "user", "content": page_state.chat_prompt}) - with st.chat_message("user"): - st.write(page_state.chat_prompt) + on_input_prompt(prompt) + + if is_edu: + qprompt = st.sidebar.selectbox("快速命令列表", ["", "/plan", "/start", "/continue", + "/test choice", "/test program", "/result", + "/help", "/config 中文", + ], index=0) + if st.sidebar.button("发送命令"): + on_input_prompt(qprompt) stop_action = st.sidebar.empty() if not page_state.streaming_end: @@ -65,7 +79,7 @@ def clear_chat_history(): with st.spinner("Thinking..."): # 检索知识库 kmsg = search_knowledge(knowledge_name, page_state.chat_prompt) - if kmsg != "": + if kmsg != "" and show_libs: st.expander("📚 知识库检索结果", expanded=False).markdown(kmsg) sysmsg = sysmsg_func(kmsg) response = openai_streaming(sysmsg, page_state.messages[-10:]) diff --git a/libs/prompts.py b/libs/prompts.py index 8affc03..7fb21d4 100644 --- a/libs/prompts.py +++ b/libs/prompts.py @@ -49,10 +49,11 @@ def get_codeboy_sysmsg(kmsg: str) -> str: * Middle School ### Commands -* /test: Test students' knowledge, comprehension, and problem-solving skills. * /plan : Create a lesson plan based on the student's needs and preferences. * /start : Start the specified lesson plan. * /continue: Continue from the previous operation. +* /test : Tests students' knowledge, understanding, and problem-solving skills. choice stands for multiple-choice and program stands for programming. +* /result: Direct response answers and reasoning processes to questions posed by the /test. * /config setup your configuration . * /language Setting the conversation language. * /help: Respond to the list of commands and their usage descriptions. diff --git "a/pages/01_\360\237\216\233\357\270\217\345\272\224\347\224\250\351\235\242\346\235\277.py" "b/pages/01_\360\237\216\233\357\270\217\345\272\224\347\224\250\351\235\242\346\235\277.py" new file mode 100644 index 0000000..5c305d0 --- /dev/null +++ "b/pages/01_\360\237\216\233\357\270\217\345\272\224\347\224\250\351\235\242\346\235\277.py" @@ -0,0 +1,77 @@ +import streamlit as st +from urllib.parse import quote as urlencode + +st.set_page_config(page_title="CoolStudy 应用面板", page_icon="🎛️") + +st.sidebar.markdown("# 🎛️ 应用面板") + +# List of apps +apps = [ + { + "name": "💥 方程式杀手", + "remark": "`一个简单的工具,用于化简和解决方程式`", + "link": urlencode("方程式杀手"), + }, + { + "name": "🔬 图像分析", + "remark": "`通过 AI 分析图像中的内容,提供有用的信息`", + "link": urlencode("图像分析"), + }, + { + "name": "✨ 智能思维导图", + "remark": "`通过 AI 模型分析,生成智能思维导图`", + "link": urlencode("智能思维导图"), + }, + { + "name": "🎙️ 语音转录", + "remark": "`通过 AI 模型识别语音内容,转录文本,并支持合成新语音`", + "link": urlencode("语音转录"), + }, + { + "name": "🌐 酷学365", + "remark": "`一个 AI 学习助手, 解答学习上的任何问题`", + "link": urlencode("酷学365"), + }, + { + "name": "🐍 Python_编程导师", + "remark": "`一个 Python 学习助手,可以设计学习计划、解答问题`", + "link": urlencode("Python_编程导师"), + }, + { + "name": "🎨 图像生成", + "remark": "`通过 AI 模型生成图像,包括人脸、动漫人物、风景等`", + "link": urlencode("图像生成"), + }, + { + "name": "🤖 Streamlit_组件学习", + "remark": "`一个 Streamlit 组件学习应用案例`", + "link": urlencode("Streamlit_组件学习"), + }, +] + +cols = st.columns(3) +# Iterating over the apps to create buttons in the UI +for i, app in enumerate(apps): + # Determine which column to place the app based on index + col = cols[i % 3] + # Create a button for each app in the respective column + with col.expander(app['name'], expanded=True): + st.markdown(app['remark']) + link = app['link'] + name = app['name'] + link_html = f""" + + {name} + +""" + st.markdown(link_html, unsafe_allow_html=True) + + # st.link_button(app['name'], app['link']) diff --git "a/pages/07_\360\237\220\215Python_\347\274\226\347\250\213\345\257\274\345\270\210.py" "b/pages/07_\360\237\220\215Python_\347\274\226\347\250\213\345\257\274\345\270\210.py" index 61409a5..3280462 100644 --- "a/pages/07_\360\237\220\215Python_\347\274\226\347\250\213\345\257\274\345\270\210.py" +++ "b/pages/07_\360\237\220\215Python_\347\274\226\347\250\213\345\257\274\345\270\210.py" @@ -4,6 +4,7 @@ from dotenv import load_dotenv from apps.chatbot import get_chatbot_page from libs.prompts import get_codeboy_sysmsg +from libs.session import PageSessionState sys.path.append(os.path.abspath('..')) load_dotenv() @@ -11,4 +12,4 @@ st.sidebar.markdown("# 💡Python 编程导师") -get_chatbot_page("codeboy", "codeboy", get_codeboy_sysmsg) +get_chatbot_page("codeboy", "codeboy", get_codeboy_sysmsg, is_edu=True) diff --git "a/pages/01_\360\237\244\226Streamlit_\347\273\204\344\273\266\345\255\246\344\271\240.py" "b/pages/09_\360\237\244\226Streamlit_\347\273\204\344\273\266\345\255\246\344\271\240.py" similarity index 100% rename from "pages/01_\360\237\244\226Streamlit_\347\273\204\344\273\266\345\255\246\344\271\240.py" rename to "pages/09_\360\237\244\226Streamlit_\347\273\204\344\273\266\345\255\246\344\271\240.py"