I am making a chat with CSV chatbot where we can upload a csv and talk with it it works okay in chat secanrios but when I ask it to plot a graph it doesnât show the plot on the GUI
Steps to reproduce
Code snippet:
agent = create_pandas_dataframe_agent(
OpenAI(temperature=0), df , verbose=True, agent_type=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
handle_parsing_errors=True, memory=memory)
st.success('Done!')
if prompt := st.chat_input("Ask a question about your CSV: "):
# st.chat_message("user").write(prompt)
# # Add user message to chat history
st.session_state.messages.append({"role": "user", "content": prompt})
# # Display user message in chat message container
with st.chat_message("user"):
st.markdown(prompt)
with st.chat_message("assistant"):
st_callback = StreamlitCallbackHandler(st.container())
response = agent.run(prompt, callbacks=[st_callback])
st.session_state.messages.append({"role": "assistant", "content": response})
st.write(response)
If applicable, please provide the steps we should take to reproduce the error or specified behavior.
Expected behavior:
SO when I ask a question for exaple plot a histogram it doesnât show the hisogram on the GUI in my browser, I know i can use st.pyplot but I want to have the output in a chat window like chatgpt
Actual behavior:
Instead the output is shown as âA histogram of the year column is plotted.â
Debug info
Streamlit version: streamlit==1.26.0
Python version: 3.11
Using Virtual env
OS version: Mac OS
Browser version: CHrome
Additional information
If needed, add any other context about the problem here.
Looks like you are trying to display chat messages in a chat format but this package does not support displaying plots inside chat messages, you can use st.plotly_chart outside the chat message container, below the chat input widget or you can use st.chat_message
without the streamlit_chat package, and insert any streamlit element into the returned container using with notation
I am looking for something where when I ask a question suppose âPlot a chart of certain columnâ then I want the reply from the assistant to be a âChart/vis of that columnâ.
I think somewhere you need to implement streamlit plot functionalities. Agents, under the hood solving the data frame operations. but no where they incorporate streamlit plotting functionality . However PandasAI library does that by including streamlit functionalities in form of middleware (if Iâm not wrong!)
hereâs an example you can try out for testing, but for this one needs to ensure that the initialised data frame is assigned as âdfâ ( also, you need to add conditional plots - which is not at all elegant way to do )
if "bar" in response_dict:
data = response_dict["bar"]
try:
df_data = {
col: [x[i] if isinstance(x, list) else x for x in data["data"]]
for i, col in enumerate(data["columns"])
}
df = pd.DataFrame(df_data)
if "Products" in df.columns:
df.set_index("Products", inplace=True)
st.bar_chart(df)
except ValueError:
print(f"Couldn't create DataFrame from data: {data}")
I would rather take an alternative approach in solving it by just implementing own prompts and executing fruther. ( drawing inspiration from PandasAI ) . Hereâs one of the app I built and demonstrated -