Talk to your data using LangChain CSV Agents and Amazon Bedrock

thallyscostalat
3 min readMay 5, 2024

--

LangChain and Bedrock. Source.

Have you ever wished you could communicate with your data effortlessly, just like talking to a colleague? With LangChain CSV Agents, that’s exactly what you can do!

In this article, we’ll explore how you can interact with your CSV data using natural language, leveraging LangChain, an exciting new tool in the field of natural language processing, and a FM from Amazon Bedrock.

Introduction

LangChain is a powerful framework that allows you to build conversational agents tailored to your specific data tasks. By combining the capabilities of language models like Claude 3 Sonnet from Anthropic with data processing tools, LangChain enables seamless communication with your datasets.

Getting Started

First, let’s search for a CSV dataset on Kaggle to develop our test scenario. In this case, we’ll use heart_failure_clinical_records.

To make sure the informations that we have, let’s print the dataframe header:

So, to summarize, we have the following features:

  • age, anaemia, creatinine_phosphokinase, diabetes, ejection_fraction, high_blood_pressure, platelets, serum_creatinine, serum_sodium, sex, smoking, time, DEATH_EVENT.

Let’s dive into a practical example to see LangChain and Bedrock in action. We’ll start with a simple Python script that sets up a LangChain CSV Agent and interacts with this CSV file.

Import Necessary Modules

from langchain.agents.agent_types import AgentType
from langchain_experimental.agents.agent_toolkits import create_csv_agent
from langchain.llms.bedrock import Bedrock
import boto3

In this section, we import the necessary modules to create and interact with the LangChain CSV Agent. Additionally, we import Bedrock from LangChain for accessing models and boto3 for AWS SDK to communicate with Bedrock service.

Initializing Bedrock Claude 3 Sonnet FM Model

llm=Bedrock(model_id="anthropic.claude-3-sonnet-20240229-v1:0",
client=boto3.client(service_name='bedrock-runtime', region_name='us-east-1'),
model_kwargs={"temperature":0.})

Here, we initialize the Bedrock model using the Bedrock class. We specify the model ID which identifies the model version we want to use. Additionally, we provide a boto3 client for communication with the Bedrock runtime service, and specify model kwargs such as temperature.

Creating the CSV Agent

agent = create_csv_agent(
llm,
"heart_failure_clinical_records.csv",
verbose=True,
agent_type=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
handle_parsing_errors=True
)

In this section, we create the LangChain CSV Agent using the create_csv_agent() function. We pass the initialized Bedrock model, the path to the CSV file containing the data we want to analyze, and some additional options such as the agent type and whether we want to handle parsing errors.

Interacting with the Agent

agent.run("how many people have more than 50 years old?")

Finally, we interact with the agent by asking a question about the data. We call the run() method of the agent and pass our question as an argument. The agent processes the question and provides a response based on the data contained in the CSV file.

In this case, the verbose process will be printed:

CSV Agents Verbose and Answer

We see on the last line the response based on natural language:

3684 people have more than 50 years old in the dataframe df.

To confirm the result using Pandas library, use the following code:

import pandas as pd

df = pd.read_csv('heart_failure_clinical_records.csv')

df[df['age']>50].shape

Response:

(3684, 13)

LangChain CSV Agents open up exciting possibilities for interacting with your data using natural language. Whether you’re exploring a dataset, generating insights, or performing complex analyses, LangChain makes the process intuitive and efficient. Give it a try and start talking to your data today!

I hope the insights shared here prove valuable in enhancing your work, regardless of your specific area of focus.

👏🏽 I hope you enjoy the content! | Follow me on LinkedIn.

--

--