├── .gitignore ├── .vscode └── settings.json ├── README.md ├── __pycache__ ├── app.cpython-311.pyc └── gpt4all.cpython-311.pyc ├── agent.py ├── interface.py ├── requirements.txt └── screenshots ├── bar graph.png ├── create a table.png ├── interace.png ├── line chart.png └── prompt.png /.gitignore: -------------------------------------------------------------------------------- 1 | .env 2 | *.csv 3 | bin -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "[python]": { 3 | "editor.defaultFormatter": "ms-python.black-formatter" 4 | }, 5 | "python.formatting.provider": "none" 6 | } 7 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Chat with your CSV: Visualize Your Data with Langchain and Streamlit 2 | 3 | Repository for the application built in [this](https://dev.to/ngonidzashe/chat-with-your-csv-visualize-your-data-with-langchain-and-streamlit-ej7) article. 4 | 5 | ## Requirements 6 | 7 | Install the required packages by running 8 | 9 | ``` 10 | pip install -r requirements.txt 11 | ``` 12 | 13 | ## Interface 14 | ![interace](https://github.com/Ngonie-x/langchain_csv/assets/28601809/0f27a2da-1128-4b23-9d01-b509b55761eb) 15 | --- 16 | ![prompt](https://github.com/Ngonie-x/langchain_csv/assets/28601809/9e90ba35-c45e-4ea4-b632-2c9203b373d2) 17 | --- 18 | ![bar graph](https://github.com/Ngonie-x/langchain_csv/assets/28601809/2fb4f9fe-cd6e-46ed-afad-66e8606fca3c) 19 | --- 20 | ![create a table](https://github.com/Ngonie-x/langchain_csv/assets/28601809/b49c50d2-c12e-43a0-a593-33e508dbf4a6) 21 | --- 22 | ![line chart](https://github.com/Ngonie-x/langchain_csv/assets/28601809/f4e94c50-e505-4f32-a4e4-f0ede5158b3b) 23 | -------------------------------------------------------------------------------- /__pycache__/app.cpython-311.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ngonie-x/langchain_csv/d66435b967ade87acdcea992fdd2bcda7f185e20/__pycache__/app.cpython-311.pyc -------------------------------------------------------------------------------- /__pycache__/gpt4all.cpython-311.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ngonie-x/langchain_csv/d66435b967ade87acdcea992fdd2bcda7f185e20/__pycache__/gpt4all.cpython-311.pyc -------------------------------------------------------------------------------- /agent.py: -------------------------------------------------------------------------------- 1 | from langchain import OpenAI 2 | from langchain.agents import create_pandas_dataframe_agent 3 | import pandas as pd 4 | 5 | # Setting up the api key 6 | import environ 7 | 8 | env = environ.Env() 9 | environ.Env.read_env() 10 | 11 | API_KEY = env("apikey") 12 | 13 | 14 | def create_agent(filename: str): 15 | """ 16 | Create an agent that can access and use a large language model (LLM). 17 | 18 | Args: 19 | filename: The path to the CSV file that contains the data. 20 | 21 | Returns: 22 | An agent that can access and use the LLM. 23 | """ 24 | 25 | # Create an OpenAI object. 26 | llm = OpenAI(openai_api_key=API_KEY) 27 | 28 | # Read the CSV file into a Pandas DataFrame. 29 | df = pd.read_csv(filename) 30 | 31 | # Create a Pandas DataFrame agent. 32 | return create_pandas_dataframe_agent(llm, df, verbose=False) 33 | 34 | 35 | def query_agent(agent, query): 36 | """ 37 | Query an agent and return the response as a string. 38 | 39 | Args: 40 | agent: The agent to query. 41 | query: The query to ask the agent. 42 | 43 | Returns: 44 | The response from the agent as a string. 45 | """ 46 | 47 | prompt = ( 48 | """ 49 | For the following query, if it requires drawing a table, reply as follows: 50 | {"table": {"columns": ["column1", "column2", ...], "data": [[value1, value2, ...], [value1, value2, ...], ...]}} 51 | 52 | If the query requires creating a bar chart, reply as follows: 53 | {"bar": {"columns": ["A", "B", "C", ...], "data": [25, 24, 10, ...]}} 54 | 55 | If the query requires creating a line chart, reply as follows: 56 | {"line": {"columns": ["A", "B", "C", ...], "data": [25, 24, 10, ...]}} 57 | 58 | There can only be two types of chart, "bar" and "line". 59 | 60 | If it is just asking a question that requires neither, reply as follows: 61 | {"answer": "answer"} 62 | Example: 63 | {"answer": "The title with the highest rating is 'Gilead'"} 64 | 65 | If you do not know the answer, reply as follows: 66 | {"answer": "I do not know."} 67 | 68 | Return all output as a string. 69 | 70 | All strings in "columns" list and data list, should be in double quotes, 71 | 72 | For example: {"columns": ["title", "ratings_count"], "data": [["Gilead", 361], ["Spider's Web", 5164]]} 73 | 74 | Lets think step by step. 75 | 76 | Below is the query. 77 | Query: 78 | """ 79 | + query 80 | ) 81 | 82 | # Run the prompt through the agent. 83 | response = agent.run(prompt) 84 | 85 | # Convert the response to a string. 86 | return response.__str__() 87 | -------------------------------------------------------------------------------- /interface.py: -------------------------------------------------------------------------------- 1 | import streamlit as st 2 | import pandas as pd 3 | import json 4 | 5 | from agent import query_agent, create_agent 6 | 7 | 8 | def decode_response(response: str) -> dict: 9 | """This function converts the string response from the model to a dictionary object. 10 | 11 | Args: 12 | response (str): response from the model 13 | 14 | Returns: 15 | dict: dictionary with response data 16 | """ 17 | return json.loads(response) 18 | 19 | 20 | def write_response(response_dict: dict): 21 | """ 22 | Write a response from an agent to a Streamlit app. 23 | 24 | Args: 25 | response_dict: The response from the agent. 26 | 27 | Returns: 28 | None. 29 | """ 30 | 31 | # Check if the response is an answer. 32 | if "answer" in response_dict: 33 | st.write(response_dict["answer"]) 34 | 35 | # Check if the response is a bar chart. 36 | if "bar" in response_dict: 37 | data = response_dict["bar"] 38 | df = pd.DataFrame(data) 39 | df.set_index("columns", inplace=True) 40 | st.bar_chart(df) 41 | 42 | # Check if the response is a line chart. 43 | if "line" in response_dict: 44 | data = response_dict["line"] 45 | df = pd.DataFrame(data) 46 | df.set_index("columns", inplace=True) 47 | st.line_chart(df) 48 | 49 | # Check if the response is a table. 50 | if "table" in response_dict: 51 | data = response_dict["table"] 52 | df = pd.DataFrame(data["data"], columns=data["columns"]) 53 | st.table(df) 54 | 55 | 56 | st.title("👨‍💻 Chat with your CSV") 57 | 58 | st.write("Please upload your CSV file below.") 59 | 60 | data = st.file_uploader("Upload a CSV") 61 | 62 | query = st.text_area("Insert your query") 63 | 64 | if st.button("Submit Query", type="primary"): 65 | # Create an agent from the CSV file. 66 | agent = create_agent(data) 67 | 68 | # Query the agent. 69 | response = query_agent(agent=agent, query=query) 70 | 71 | # Decode the response. 72 | decoded_response = decode_response(response) 73 | 74 | # Write the response to the Streamlit app. 75 | write_response(decoded_response) 76 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | aiohttp==3.8.4 2 | aiosignal==1.3.1 3 | altair==5.0.1 4 | async-timeout==4.0.2 5 | attrs==23.1.0 6 | blinker==1.6.2 7 | cachetools==5.3.1 8 | certifi==2023.5.7 9 | charset-normalizer==3.1.0 10 | click==8.1.3 11 | colorama==0.4.6 12 | dataclasses-json==0.5.7 13 | decorator==5.1.1 14 | frozenlist==1.3.3 15 | gitdb==4.0.10 16 | GitPython==3.1.31 17 | greenlet==2.0.2 18 | idna==3.4 19 | importlib-metadata==6.6.0 20 | Jinja2==3.1.2 21 | jsonschema==4.17.3 22 | langchain==0.0.188 23 | markdown-it-py==2.2.0 24 | MarkupSafe==2.1.2 25 | marshmallow==3.19.0 26 | marshmallow-enum==1.5.1 27 | mdurl==0.1.2 28 | multidict==6.0.4 29 | mypy-extensions==1.0.0 30 | numexpr==2.8.4 31 | numpy==1.24.3 32 | openai==0.27.7 33 | openapi-schema-pydantic==1.2.4 34 | packaging==23.1 35 | pandas==2.0.2 36 | Pillow==9.5.0 37 | protobuf==4.23.2 38 | pyarrow==12.0.0 39 | pydantic==1.10.8 40 | pydeck==0.8.1b0 41 | Pygments==2.15.1 42 | Pympler==1.0.1 43 | pyrsistent==0.19.3 44 | python-dateutil==2.8.2 45 | python-environ==0.4.54 46 | pytz==2023.3 47 | pytz-deprecation-shim==0.1.0.post0 48 | PyYAML==6.0 49 | requests==2.31.0 50 | rich==13.4.1 51 | six==1.16.0 52 | smmap==5.0.0 53 | SQLAlchemy==2.0.15 54 | streamlit==1.23.1 55 | tabulate==0.9.0 56 | tenacity==8.2.2 57 | toml==0.10.2 58 | toolz==0.12.0 59 | tornado==6.3.2 60 | tqdm==4.65.0 61 | typing-inspect==0.9.0 62 | typing_extensions==4.6.3 63 | tzdata==2023.3 64 | tzlocal==4.3 65 | urllib3==2.0.2 66 | validators==0.20.0 67 | watchdog==3.0.0 68 | yarl==1.9.2 69 | zipp==3.15.0 70 | -------------------------------------------------------------------------------- /screenshots/bar graph.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ngonie-x/langchain_csv/d66435b967ade87acdcea992fdd2bcda7f185e20/screenshots/bar graph.png -------------------------------------------------------------------------------- /screenshots/create a table.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ngonie-x/langchain_csv/d66435b967ade87acdcea992fdd2bcda7f185e20/screenshots/create a table.png -------------------------------------------------------------------------------- /screenshots/interace.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ngonie-x/langchain_csv/d66435b967ade87acdcea992fdd2bcda7f185e20/screenshots/interace.png -------------------------------------------------------------------------------- /screenshots/line chart.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ngonie-x/langchain_csv/d66435b967ade87acdcea992fdd2bcda7f185e20/screenshots/line chart.png -------------------------------------------------------------------------------- /screenshots/prompt.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ngonie-x/langchain_csv/d66435b967ade87acdcea992fdd2bcda7f185e20/screenshots/prompt.png --------------------------------------------------------------------------------