import streamlit as st
st.set_page_config(
page_title="灵创对话",
page_icon=":web-demo:",
layout="wide",
)
st.markdown(""" """,
unsafe_allow_html=True)
# 初始化历史记录和past key values
if "history" not in st.session_state:
st.session_state.history = []
if "past_key_values" not in st.session_state:
st.session_state.past_key_values = None
# 设置max_length、top_p和temperature
# max_length = st.sidebar.slider("max_length", 0, 32768, 8192, step=1)
# top_p = st.sidebar.slider("top_p", 0.0, 1.0, 0.8, step=0.01)
# temperature = st.sidebar.slider("temperature", 0.0, 1.0, 0.6, step=0.01)
# 清理会话历史
# buttonClean = st.sidebar.button("清理会话历史", key="clean")
# if buttonClean:
# st.session_state.history = []
# st.session_state.past_key_values = None
# st.rerun()
mainContainer = st.container()
if len(st.session_state.history) == 0:
emptyContent = st.markdown("""
例子
用简单的术语解释质量守恒定律
对 10 岁生日有什么创意吗?
如何在 Javascript 中发出 HTTP 请求?request in Javascript?
能力
能记住用户早些时候的对话
允许用户提供后续更正
会拒绝不当的请求
提醒
少数可能会产生错误信息
可能会产生不当的指令或有偏见的内容
对 2021 年后的世界和事件的了解有限
""", unsafe_allow_html=True)
else:
# 渲染聊天历史记录
for i, message in enumerate(st.session_state.history):
if message["role"] == "user":
with mainContainer.chat_message(name="user", avatar="user"):
st.markdown(message["content"])
else:
with mainContainer.chat_message(name="assistant", avatar="assistant"):
st.markdown(message["content"])
# 输入框和输出框
with mainContainer.chat_message(name="user", avatar="user"):
input_placeholder = st.empty()
with mainContainer.chat_message(name="assistant", avatar="assistant"):
message_placeholder = st.empty()
# 获取用户输入
prompt_text = st.chat_input("请输入您的问题")
# 如果用户输入了内容,则生成回复
if prompt_text:
# 处理历史为空的情况
if len(st.session_state.history) == 0:
emptyContent.empty()
# 输入框和输出框
with mainContainer.chat_message(name="user", avatar="user"):
input_placeholder = st.empty()
with mainContainer.chat_message(name="assistant", avatar="assistant"):
message_placeholder = st.empty()
history = st.session_state.history
past_key_values = st.session_state.past_key_values
history.append({
'content': prompt_text, "role": 'user'
})
response = "您的问题:”" + prompt_text + "“,我暂时无法处理!请重新问一个问题吧!"
history.append({
'content': response, "role": 'system'
})
input_placeholder.markdown(prompt_text)
message_placeholder.markdown(response)
# 更新历史记录和past key values
st.session_state.history = history
st.session_state.past_key_values = past_key_values
# st.markdown("""
#
# """, unsafe_allow_html=True)