Untitled
unknown
python
a month ago
2.9 kB
2
Indexable
Never
from langchain.callbacks import StreamlitCallbackHandler import streamlit as st import pandas as pd import os import openpyxl from langchain_groq import ChatGroq from langchain_experimental.agents import create_pandas_dataframe_agent from langchain.agents.agent_types import AgentType from langchain.prompts import PromptTemplate from langchain.chains import LLMChain from dotenv import load_dotenv import os import matplotlib.pyplot as plt load_dotenv() file_formats = { "csv": pd.read_csv, "xls": pd.read_excel, "xlsx": pd.read_excel, "xlsm": pd.read_excel, "xlsb": pd.read_excel, } def clear_submit(): """ Clear the Submit Button State Returns: """ st.session_state["submit"] = False @st.cache_data(ttl="2h") def load_data(uploaded_file): try: ext = os.path.splitext(uploaded_file.name)[1][1:].lower() except: ext = uploaded_file.split(".")[-1] if ext in file_formats: return file_formats[ext](uploaded_file) else: st.error(f"Unsupported file format: {ext}") return None st.set_page_config(page_title="LangChain: Chat with pandas DataFrame", page_icon="🦜") st.title("🦜 LangChain: Chat with pandas DataFrame") uploaded_file = st.file_uploader( "Upload a Data file", type=list(file_formats.keys()), help="Various File formats are Support", on_change=clear_submit, ) if not uploaded_file: st.warning( "This app uses LangChain's `PythonAstREPLTool` which is vulnerable to arbitrary code execution. Please use caution in deploying and sharing this app." ) if uploaded_file: df = load_data(uploaded_file) if "messages" not in st.session_state or st.sidebar.button("Clear conversation history"): st.session_state["messages"] = [{"role": "assistant", "content": "How can I help you?"}] for msg in st.session_state.messages: st.chat_message(msg["role"]).write(msg["content"]) if prompt := st.chat_input(placeholder="What is this data about?"): st.session_state.messages.append({"role": "user", "content": prompt}) st.chat_message("user").write(prompt) llm = ChatGroq( model="mixtral-8x7b-32768", temperature=0, max_tokens=1024, api_key=os.getenv("GROQ_API_KEY"), streaming=True ) pandas_df_agent = create_pandas_dataframe_agent( llm, df, verbose=True, agent_type=AgentType.OPENAI_FUNCTIONS, handle_parsing_errors=True, allow_dangerous_code=True, ) with st.chat_message("assistant"): st_cb = StreamlitCallbackHandler(st.container(), expand_new_thoughts=False) response = pandas_df_agent.run(st.session_state.messages, callbacks=[st_cb]) st.session_state.messages.append({"role": "assistant", "content": response}) st.write(response)
Leave a Comment