Machine Learning Spot

How to Create an Amazing LangChain CSV Agent: (2024)

LangChain CSV Agent


Have you ever wondered how to interact with a CSV, talk to it, and act using it as a LangChain CSV agent? While RAG might come to mind, there’s an agent that makes interacting with a CSV straightforward – the LangChain CSV agent.

In this blog post, we’ll explore how this powerful tool can transform how you handle data, making it more intuitive and accessible. We will learn how to interact with a CSV using an LLM and how to add multiple CSV files to interact with both of them using an agent

Let’s begin the code

Setting up environment

Let’s begin by installing the requirements

%pip install langchain
%pip install langchain langchain-experimental 
%pip install langchain-fireworks

Here the LLM we are using is Mistral and we will use Fireworks AI to use this LLM of Mistral. We use the LLM to communicate with the CSV file and LangChain experimental to create a LangChain CSV agent.

from langchain.agents.agent_types import AgentType
from langchain_experimental.agents.agent_toolkits import create_csv_agent

Since we are using Fireworks, you’ll need to obtain the Fireworks API key from here. The code below will prompt you to enter the API key at runtime using getpass, and then it will save it in the environment using the os class, which we have just imported below.

import os
import getpass

if "FIREWORKS_API_KEY" not in os.environ:
    os.environ["FIREWORKS_API_KEY"] = getpass.getpass("Fireworks API Key:")

Uploading CSV and Initializing a LangChain CSV Agent

Now this is where we upload a CSV by initializing a CSV agent so first we will initialize a chat model of fireworks that we can use while initializing The CSV agent and to upload the CSV we will just provide the address of the CSV with its name as a second argument. The file that I am using here is a random CSV file downloaded from datablist.

The third argument is verbose which lets us see what’s happening inside the fourth argument tells what type of agent type we are using here we are using ZERO_SHOT_REACT_DESCRIPTION for our LangChain CSV agent.

Zero-shot learning refers to the capability of an AI model to understand and perform tasks it hasn’t been explicitly trained on, based on its ability to generalize from the knowledge it has acquired during training. So that’s why it can work with tasks without requiring prior examples or extensive retraining.

the last argument that we have used is to avoid errors in our LangChain CSV agent if we don’t use it will tell us the code is dangerous and won’t work. Don’t worry the code is safe

.from langchain_fireworks import ChatFireworks

model = ChatFireworks(model="accounts/fireworks/models/mixtral-8x7b-instruct")

agent = create_csv_agent(
    model,
    "/content/100 Sales Records.csv",
    verbose=True,
    agent_type=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
    allow_dangerous_code=True,

)

Playing with LangChain CSV Agent

Now it’s time to use the agent and see what happens when we use it let’s play it in different ways I am attaching these two only.

First, we are gonna ask what this CSV is about then let’s see what it says.

# Checking the number of rows in the CSV file
agent.invoke("explain what is this csv about ")


You can compare the file and its answer the file link is the same as above I have also pasted a screenshot of the file above for your ease as I had to trim the complete output but the verbose will also print the entire content of the file and what it sees in it.

Output:

Finished chain.
{‘input’: ‘explain what is this csv about ‘,
‘output’: ‘This dataframe contains sales data with various details about each order, including region, country, item type, sales channel, order priority, order date, order ID, ship date, units sold, unit price, unit cost, total revenue, total cost, and total profit. There are 100 orders in total, with a mean of around 5129 units sold, a mean unit price of around 276.76, a mean unit cost of around 191.05, a mean total revenue of around 1.37 million, a mean total cost of around 931,806, and a mean total profit of around 441,682.’}

# Querying specific data
agent.invoke("Tuvalu babyfood total cost is what")
LangChain CSV agent

Output:

After processing the entire file’s data this output is provided since here too verbose is true so the LangChain CSV agent will also tell how it is processing it and will show each row and what it is doing here but I have only shared the final output to keep things simple

> Finished chain.
{'input': 'order ID of Baby food',
 'output': 'The order IDs for Baby Food items are: 669165933, 547995746, 819028031, 860673511, 569662845, 688288152, 494747245.'}

Handling multiple CSVs

Now what if you have multiple CSVs instead of one and you want to make it’s agent? then you just need to add more file names separated with a comma everything else remains the same.

agent_multi = create_csv_agent(
    model,
    ["/content/100 Sales Records.csv", "/content/organizations-100.csv"],
    verbose=True,
    agent_type=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
    allow_dangerous_code=True
)

Conclusion:

Congratulations you now know how to create a langchain agent. Here we learnt how to use the langchain CSV agent and we used a free-of-cost model mistral to use the CSV agent we also saw how we can add multiple files.

I hope this guide on LangChain CSV agent was useful for you If you want to read more blogs on LangChain like LangChain CSV agent check out the machine learning guide section further if you want to check feel free to reach out for any feedback or suggestions.


Liked the Post?  You can share as well

Facebook
Twitter
LinkedIn

More From Machine Learning Spot

Get The Latest AI News and Insights

Directly To Your Inbox
Subscribe

Signup ML Spot Newsletter

What Will You Get?

Bonus

Get A Free Workshop on
AI Development