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:
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
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 pathquestion
: 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?"
df = pd.read_csv(csv_path)
df.head(1)
# Create Agent
agent = create_csv_agent(
OpenAI(temperature=temperature, openai_api_key=api_key),
csv_path,
verbose=True
)
res = agent.run(question)
print("Result:", res)