Skip to content

Latest commit

 

History

History
78 lines (54 loc) · 2.72 KB

LangChain_CSV_Agent.md

File metadata and controls

78 lines (54 loc) · 2.72 KB



Template request | Bug report | Generate Data Product

Tags: #csv #agent #langchain #questionanswering #toolkit #example

Author: Hamid Mukhtar

Description: This notebook shows how to use agents to interact with a csv. 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_csv_agent
from langchain.llms import OpenAI
from langchain.chat_models import ChatOpenAI
from langchain.agents.agent_types import AgentType
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.
  • csv_path: CSV path
  • question: question to be asked to agent
api_key = naas.secret.get(name="OPENAI_API_KEY") or "ENTER_YOUR_OPENAI_API_KEY"
temperature = 0
csv_path = "https://covid.ourworldindata.org/data/owid-covid-data.csv"
question = "what is the number of total_cases in France?"

Model

Read CSV

df = pd.read_csv(csv_path)
df.head(1)

Create Agent

# Create Agent
agent = create_csv_agent(
    OpenAI(temperature=temperature, openai_api_key=api_key),
    csv_path,
    verbose=True
)

Output

Display result

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