Search

Wednesday, 11 October 2023

Using Pandas Agent with OpenAI and Langchain for reading CSV

 import pandas as pd

from langchain.chat_models import ChatOpenAI
from langchain.agents import create_pandas_dataframe_agent, AgentType
#Load the .env file
from dotenv import load_dotenv,find_dotenv
load_dotenv(find_dotenv())

customersDf = pd.read_csv('Customers.csv')
salesDf = pd.read_csv('sales.csv')

chat = ChatOpenAI(model_name="gpt-3.5-turbo",temperature=0.0)
#Initialize pandas dataframe agent
agent = create_pandas_dataframe_agent(agent_type=AgentType.OPENAI_FUNCTIONS,
                                      llm=chat,
                                      df=[customersDf,salesDf],
                                      verbose=True)

agent.run("What is the name of the first customer?  using tool python_repl_ast")


agent.run("Has JON placed any orders?")