赞
踩
Streamlit确实是一个很不错的框架,特别是用在人工智能开发上,能够让开发人员将更多精力集中在人工智能处理上,下面的代码实现了一个简单的聊天机器人功能,机器人将用户的输入直接回复回去:
- import streamlit as st
-
- st.title("东临碣石聊天室")
-
- if "messages" not in st.session_state:
- st.session_state.messages = []
-
- # Display chat messages from history on app rerun
- for message in st.session_state.messages:
- with st.chat_message(message["role"]):
- st.markdown(message["content"])
-
- # React to user input
- if prompt := st.chat_input("What's your message?"):
- # Display user message in chat message container
- st.chat_message("user").markdown(prompt)
- # Add user message to chat history
- st.session_state.messages.append({"role": "user", "content": prompt})
-
- response = f"答复: {prompt}"
- # Display assistant response in chat message container
-
- with st.chat_message("assistant"):
- st.markdown(response)
- # Add assistant response to chat history
- st.session_state.messages.append({"role": "assistant", "content": response})
聊天室效果如下:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。