Skip to content

Latest commit

 

History

History
63 lines (42 loc) · 2.52 KB

LangChain_Pandas_Dataframe_Agent.md

File metadata and controls

63 lines (42 loc) · 2.52 KB



Template request | Bug report | Generate Data Product

Tags: #langchain #pandas #dataframe #agent #python #toolkit

Author: Florent Ravenel

Description: This notebook shows how to use agents to interact with a pandas dataframe. It is mostly optimized for question answering.

References:

Input

Import libraries

Note: You may need to restart the kernel to use updated packages.

try:
    import langchain
except:
    !pip install langchain tabulate typing-inspect==0.8.0 typing_extensions==4.5.0 --user
from langchain.agents import create_pandas_dataframe_agent
from langchain.llms import OpenAI
import pandas as pd
import naas

Setup Variables

  • api_key: OpenAI API key. Get your API key here.
  • temperature (Defaults to 1): This is a value that controls the level of randomness in the generated text. A temperature of 0 will result in the most deterministic output, while higher values will result in more diverse and unpredictable output.
  • df: pandas dataframe
  • question: question to be asked to agent
api_key = naas.secret.get(name="OPENAI_API_KEY") or "ENTER_YOUR_OPENAI_API_KEY"
temperature = 0
df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})
question = "what is the sum of column B?"

Model

Create Agent

# Create Agent
agent = create_pandas_dataframe_agent(OpenAI(temperature=temperature, openai_api_key=api_key), [df], verbose=True)

Output

Display result

res = agent.run(question)
print("Result:", res)