StructuredDataChat

Open in ColabOpen in GitHub

Overview

This notebook demonstrates various data analysis and query techniques, combining traditional data processing with advanced AI-driven insights. By integrating Pandas for data handling and LangChain for LLM-powered query resolution, it explores interactive and automated approaches to data exploration.

  • Data Loading and Preparation The notebook begins by loading a Titanic dataset using pandas. The data is preprocessed and stored in a DataFrame (df). This section demonstrates how to read CSV files and prepare data for analysis.

  • Interactive Query with LangChain Tools Using LangChain's PythonAstREPLTool, the notebook allows direct interaction with the DataFrame via Python commands. This tool provides an intuitive way to execute queries and retrieve results dynamically within the notebook environment.

  • Agent-Based Query System A key highlight of the notebook is the creation of an agent powered by LangChain and OpenAI models. This agent processes natural language queries and translates them into actionable insights using the dataset. For instance, it calculates survival rates based on specific conditions and provides context-aware responses.

  • Advanced Query Scenarios The notebook demonstrates the handling of more complex queries, such as analyzing survival rates for subsets of passengers (e.g., male passengers, children in specific classes). This is achieved by dynamically constructing and executing queries through the LLM-driven agent.

  • Streamlined Query Execution By integrating a callback system, the notebook ensures seamless real-time interaction with the LLM during query execution. This feature enhances the user experience by providing immediate feedback and results.

Table of Contents

References


Environment Setup

Set up the environment. You may refer to Environment Setup for more details.

[Note]

  • langchain-opentutorial is a package that provides a set of easy-to-use environment setup, useful functions and utilities for tutorials.

  • You can checkout the langchain-opentutorial for more details.

Alternatively, environment variables can also be set using a .env file.

[Note]

  • This is not necessary if you've already set the environment variables in the previous step.

PassengerId
Survived
Pclass
Name
Sex
Age
SibSp
Parch
Ticket
Fare
Cabin
Embarked

0

1

0

3

Braund, Mr. Owen Harris

male

22.0

1

0

A/5 21171

7.2500

NaN

S

1

2

1

1

Cumings, Mrs. John Bradley (Florence Briggs Th...

female

38.0

1

0

PC 17599

71.2833

C85

C

2

3

1

3

Heikkinen, Miss. Laina

female

26.0

0

0

STON/O2. 3101282

7.9250

NaN

S

3

4

1

1

Futrelle, Mrs. Jacques Heath (Lily May Peel)

female

35.0

1

0

113803

53.1000

C123

S

4

5

0

3

Allen, Mr. William Henry

male

35.0

0

0

373450

8.0500

NaN

S

...

...

...

...

...

...

...

...

...

...

...

...

...

886

887

0

2

Montvila, Rev. Juozas

male

27.0

0

0

211536

13.0000

NaN

S

887

888

1

1

Graham, Miss. Margaret Edith

female

19.0

0

0

112053

30.0000

B42

S

888

889

0

3

Johnston, Miss. Catherine Helen "Carrie"

female

NaN

1

2

W./C. 6607

23.4500

NaN

S

889

890

1

1

Behr, Mr. Karl Howell

male

26.0

0

0

111369

30.0000

C148

C

890

891

0

3

Dooley, Mr. Patrick

male

32.0

0

0

370376

7.7500

NaN

Q

891 rows × 12 columns

PassengerId
Survived
Pclass
Name
Sex
Age
SibSp
Parch
Ticket
Fare
Cabin
Embarked

0

1

0

3

Braund, Mr. Owen Harris

male

22.0

1

0

A/5 21171

7.2500

NaN

S

1

2

1

1

Cumings, Mrs. John Bradley (Florence Briggs Th...

female

38.0

1

0

PC 17599

71.2833

C85

C

2

3

1

3

Heikkinen, Miss. Laina

female

26.0

0

0

STON/O2. 3101282

7.9250

NaN

S

3

4

1

1

Futrelle, Mrs. Jacques Heath (Lily May Peel)

female

35.0

1

0

113803

53.1000

C123

S

4

5

0

3

Allen, Mr. William Henry

male

35.0

0

0

373450

8.0500

NaN

S

Data Loading

The pd.read_csv function is used to read the CSV file and store its contents in a structured tabular format. By calling df.head, we can preview the first few rows of the dataset to understand its structure and ensure the data has been loaded correctly.

PassengerId
Survived
Pclass
Name
Sex
Age
SibSp
Parch
Ticket
Fare
Cabin
Embarked

0

1

0

3

Braund, Mr. Owen Harris

male

22.0

1

0

A/5 21171

7.2500

NaN

S

1

2

1

1

Cumings, Mrs. John Bradley (Florence Briggs Th...

female

38.0

1

0

PC 17599

71.2833

C85

C

2

3

1

3

Heikkinen, Miss. Laina

female

26.0

0

0

STON/O2. 3101282

7.9250

NaN

S

3

4

1

1

Futrelle, Mrs. Jacques Heath (Lily May Peel)

female

35.0

1

0

113803

53.1000

C123

S

4

5

0

3

Allen, Mr. William Henry

male

35.0

0

0

373450

8.0500

NaN

S

Pandas DataFrame Agent

The Pandas DataFrame Agent allows you to interact with tabular data in a conversational and dynamic way. Using LangChain’s integration with tools like ChatOpenAI and create_pandas_dataframe_agent, you can perform complex queries on your DataFrame with natural language instructions. Below, we create an agent capable of analyzing the Titanic dataset and answering questions like survival rates based on specific criteria or data summary statistics.

Two or More DataFrames

You can perform LLM-based queries based on two or more DataFrames. When entering two or more DataFrames, enclose them in [].

PassengerId
Survived
Pclass
Name
Sex
Age
SibSp
Parch
Ticket
Fare
Cabin
Embarked

0

1

0

3

Braund, Mr. Owen Harris

male

22.0

1

0

A/5 21171

7.2500

0

S

1

2

1

1

Cumings, Mrs. John Bradley (Florence Briggs Th...

female

38.0

1

0

PC 17599

71.2833

C85

C

2

3

1

3

Heikkinen, Miss. Laina

female

26.0

0

0

STON/O2. 3101282

7.9250

0

S

3

4

1

1

Futrelle, Mrs. Jacques Heath (Lily May Peel)

female

35.0

1

0

113803

53.1000

C123

S

4

5

0

3

Allen, Mr. William Henry

male

35.0

0

0

373450

8.0500

0

S

Last updated