├── AppV3
├── .env
├── html_templates.py
├── requirements.txt
└── main.py
├── app
├── .env.example
└── main.py
├── appV2
├── apikey.py
└── main.py
└── README.md
/AppV3/.env:
--------------------------------------------------------------------------------
1 | OPENAI_API_KEY=
2 |
--------------------------------------------------------------------------------
/app/.env.example:
--------------------------------------------------------------------------------
1 | OPENAI_API_KEY=
2 |
--------------------------------------------------------------------------------
/appV2/apikey.py:
--------------------------------------------------------------------------------
1 | apikey="Input API Key Here"
2 |
--------------------------------------------------------------------------------
/AppV3/html_templates.py:
--------------------------------------------------------------------------------
1 | css = '''
2 | ', unsafe_allow_html=True)
64 |
65 | # Accept input from user
66 | query = st.text_input("Enter a query:")
67 |
68 | # Execute Button Logic
69 | if st.button("Execute") and query:
70 | with st.spinner('Generating response...'):
71 | try:
72 | answer = agent.run(query)
73 |
74 | # Store conversation
75 | st.session_state.past.append(query)
76 | st.session_state.generated.append(answer)
77 |
78 | # Display conversation in reverse order
79 | for i in range(len(st.session_state.past)-1, -1, -1):
80 | st.write(f'
{st.session_state.generated[i]}
', unsafe_allow_html=True)
81 | st.write(f'{st.session_state.past[i]}
', unsafe_allow_html=True)
82 | st.write("")
83 |
84 | except Exception as e:
85 | st.error(f"An error occurred: {str(e)}")
86 |
87 |
88 | if __name__ == "__main__":
89 | main()
90 |
--------------------------------------------------------------------------------
/AppV3/main.py:
--------------------------------------------------------------------------------
1 | # Imports
2 | import streamlit as st
3 | import pandas as pd
4 | from dotenv import load_dotenv
5 | from langchain.agents import create_pandas_dataframe_agent
6 | from langchain.chat_models import ChatOpenAI
7 | from langchain.llms import OpenAI
8 | from langchain.agents.agent_types import AgentType
9 | from html_templates import css, user_template, bot_template
10 |
11 |
12 | def main():
13 | st.set_page_config(page_title="Pandas Agent")
14 | st.subheader("OpenAI LangChain Pandas Agent Chatbot")
15 | st.write("Upload a CSV or XLSX file and query answers from your data.")
16 |
17 | # Apply CSS
18 | st.write(css, unsafe_allow_html=True)
19 |
20 | # Define chat history session state variable
21 | st.session_state.setdefault('chat_history', [])
22 |
23 | # Temperature slider
24 | with st.sidebar:
25 | with st.expander("Settings", expanded=True):
26 | TEMP = st.slider(label="LLM Temperature", min_value=0.0, max_value=1.0, value=0.5)
27 |
28 | # Upload File
29 | file = st.file_uploader("Upload CSV file",type=["csv","xlsx"])
30 | if not file: st.stop()
31 |
32 | # Read Data as Pandas
33 | data = pd.read_csv(file)
34 |
35 | # Display Data Head
36 | st.write("Data Preview:")
37 | st.dataframe(data.head())
38 |
39 |
40 | # Define large language model (LLM)
41 | llm = OpenAI(temperature=TEMP)
42 |
43 | # Define pandas df agent
44 | agent = create_pandas_dataframe_agent(llm,
45 | data,
46 | verbose=True
47 | )
48 |
49 | # Accept input from user
50 | query = st.text_input("Enter a query:")
51 |
52 | # Execute Button Logic
53 | if st.button("Execute") and query:
54 | with st.spinner('Generating response...'):
55 | try:
56 | # Define prompt for agent
57 | prompt = f'''
58 | Consider the uploaded pandas data, respond intelligently to user input
59 | \nCHAT HISTORY: {st.session_state.chat_history}
60 | \nUSER INPUT: {query}
61 | \nAI RESPONSE HERE:
62 | '''
63 |
64 | # Get answer from agent
65 | answer = agent.run(prompt)
66 |
67 | # Store conversation
68 | st.session_state.chat_history.append(f"USER: {query}")
69 | st.session_state.chat_history.append(f"AI: {answer}")
70 |
71 | # Display conversation in reverse order
72 | for i, message in enumerate(reversed(st.session_state.chat_history)):
73 | if i % 2 == 0: st.markdown(bot_template.replace("{{MSG}}", message), unsafe_allow_html=True)
74 | else: st.markdown(user_template.replace("{{MSG}}", message), unsafe_allow_html=True)
75 |
76 | # Error Handling
77 | except Exception as e:
78 | st.error(f"An error occurred: {str(e)}")
79 |
80 |
81 | if __name__ == "__main__":
82 | load_dotenv() # Import enviornmental variables
83 | main()
84 |
85 |
--------------------------------------------------------------------------------