**st.session_state属性出现错误**

huangapple go评论65阅读模式
英文:

**Error with st.session_state attribute**

问题

Here is the translated code portion:

if "generated" not in st.session_state:
    st.session_state["generated"] = []

if "past" not in st.session_state:
    st.session_state["past"] = []

if "stored_session" not in st.session_state:
    st.session_state["stored_session"] = [] 


def get_text():
    input_text = st.text_input("You: ", st.session_state.get("input", "Hello, how are you?"), key="input",
                               placeholder="Your Notion bot, ask me anything")
    return input_text

def new_chat():
    save = []
    for i in range(len(st.session_state['generated'])-1, -1, -1):
        save.append("User:" + st.session_state["past"][i])
        save.append("Bot:" + st.session_state["generated"][i])        
    st.session_state["stored_session"].append(save)
    st.session_state["generated"] = []
    st.session_state["past"] = []
    st.session_state["input"] = ""

st.sidebar.button("New Chat", on_click=new_chat)

user_input = get_text()

if user_input:

    result = chain({"question": user_input})
    output = f"Answer: {result['answer']}"
        
    st.session_state["past"].append(user_input)
    st.session_state["generated"].append(output)


if st.session_state["generated"]:

    for i in range(len(st.session_state["generated"]) - 1, -1, -1):
        message(st.session_state["past"][i], is_user=True, key=str(i) + "_user")
        message(st.session_state["generated"][i], key=str(i))

Regarding the error message you're encountering, it seems like the issue is related to the use of st.session_state.past.append(user_input) and st.session_state.generated.append(output) in your code. You should access the session_state dictionary using square brackets, like st.session_state["past"] and st.session_state["generated"], as shown in the translated code above. Make sure to update your code accordingly to fix this issue.

英文:

I am new to python and streamlit and I am creating a bot to answer based on a vectorstore and i am using Langchain too. This is my part for the streamlit UI code and although I initialize the session state, I'm having an error related to it.

if "generated" not in st.session_state:
    st.session_state["generated"] = []

if "past" not in st.session_state:
    st.session_state["past"] = []

if "stored_session" not in st.session_state:
    st.session_state["stored_session"] = [] 


def get_text():
    input_text = st.text_input("You: ", st.session_state.get("input", "Hello, how are you?"), key="input",
                               placeholder="Your Notion bot, ask me anything")
    return input_text

def new_chat()
    save = []
    for i in range(len(st.session_state['generated'])-1, -1, -1):
        save.append("User:" + st.session_state["past"][i])
        save.append("Bot:" + st.session_state["generated"][i])        
    st.session_state["stored_session"].append(save)
    st.session_state["generated"] = []
    st.session_state["past"] = []
    st.session_state["input"] = ""

st.sidebar.button("New Chat", on_click=new_chat)

user_input = get_text()

if user_input:

    result = chain({"question": user_input})
    output = f"Answer: {result['answer']}"
    #output = f"Answer: {result['answer']}\nSources: {result['sources']}"
        
    st.session_state.past.append(user_input)
    st.session_state.generated.append(output)


if st.session_state["generated"]:

    for i in range(len(st.session_state["generated"]) - 1, -1, -1):
        message(st.session_state["past"][i], is_user=True, key=str(i) + "_user")
        message(st.session_state["generated"][i], key=str(i))

The error i get is :
AttributeError: st.session_state has no attribute "past". Did you forget to initialize it?
I am running the script in the terminal using streamlit run filename.py
What could be the problem?

答案1

得分: 1

"Just run your python code by

streamlit run xxx.py
```"

<details>
<summary>英文:</summary>



just run your python code by

streamlit run xxx.py




</details>



huangapple
  • 本文由 发表于 2023年6月4日 23:35:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76401156.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定