Creating an AI Agent, that drives Data Analysis through ML Model Creation
Saikat Chakraborty
Managing Director @ Accenture | Enterprise AI Value Strategy Executive
All opinions and contents expressed in this article are mine and not of the organization I work for
AI agents can encompass a wide range of functionalities beyond natural language processing including decision-making, problem-solving, interacting with external environments and executing actions.These agents can be deployed in various applications to solve complex tasks.
Today we will see how quickly we can create an AI agent that utilizes the tools available in it's repertoire and takes some data,quickly analyzes it based on user need and then builds a predictive model to answer some questions. We will use langchain to demonstrate the idea. We will use a combination of tools created from custom functions and some of the tools readily available within the library on Python platform using Spyder IDE.
Let's start by doing the essentials and loading the .env file, which should contain your OpenAI API key.
import warnings
warnings.filterwarnings("ignore")
from dotenv import load_dotenv
load_dotenv(dotenv_path='C://Users//chakr//Desktop//llms//agents//.env')
Now, let's import the required libraries
from langchain_openai import OpenAI
from langchain_experimental.tools import PythonREPLTool
from langchain_core.tools import Tool
from langchain import hub
from langchain.agents import AgentExecutor, create_react_agent
As it says in langchain documentation, sometimes, for complex calculations, rather than have an LLM generate the answer directly, it can be better to have the LLM generate code to calculate the answer, and then run that code to get the answer. In order to easily do that, this library has provide a simple Python REPL to execute commands. Just to mention,the Python standard shell, or REPL (Read-Eval-Print Loop), allows you to run Python code interactively while working on a project or learning the language. This tool is available in every Python installation, so you can use it at any moment.
Let's initiate the llm and create the tools
领英推荐
llm = OpenAI()
def csvreader(file_path):
import pandas as pd
df = pd.read_csv(file_path)
variables = df.columns.to_list()
return df,variables
readertool = Tool.from_function(
name="csvreader",
description="imports a csv file and returns a data frame and the variables",
func = csvreader
)
tools = [readertool, PythonREPLTool()]
Now we need to create the instructions for the llm. See how it is crafted below:
instructions = """You are an agent designed to write and execute python code to answer questions.
You have access to a python REPL, which you can use to execute python code.
If you get an error, debug your code and try again.
Only use the output of your code to answer the question.
You might know the answer without running any code, but you should still run the code to get the answer.
If it does not seem like you can write code to answer the question, just return "I don't know" as the answer.
"""
Now,let's create the agent and point to the file you want to analyze.
base_prompt = hub.pull("langchain-ai/react-agent-template")
prompt = base_prompt.partial(instructions=instructions)
agent = create_react_agent(llm, tools, prompt)
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
df = "G://pythoncodes//taxifare.csv"
Finally deploy the agent. That's it!
agent_executor.invoke({"input": f"read csv file {df},get the variable names and build a random forest model to \
determine which is the most significant variable for determining total fare?"},handle_parsing_errors=True)
Let's look at the output. It's amazing to see the agent in action. How it invokes thought,asks question and determines the best course of action! Finally it comes up with the right code to answer our question!
This is just a tiny example to demonstrate the potential of agentic framework! Happy exploring!!
Director Analytics and insights
3 个月Great article ?? Thanks for spreading knowledge
Data Scientist Specialist at Accenture
3 个月Interesting
Vice President - Data and Business Solutions. Analytics | AI | Digital. HEC Paris | London Business School.
3 个月Extremely insightful and the explanation is lucid to get your insights. Great work Saikat Chakraborty ????
Excellent Saikat Chakraborty . Thanks for sharing ??