Spaces:
Sleeping
Sleeping
File size: 1,265 Bytes
472e1d4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
import pandas as pd
from langchain_experimental.agents.agent_toolkits import create_pandas_dataframe_agent
from agents.llms import LLM
from langchain.agents.agent_types import AgentType
def get_dataframe_agent(
df: pd.DataFrame,
verbose: bool = True,
allow_dangerous_code: bool = True,
agent_type=AgentType.ZERO_SHOT_REACT_DESCRIPTION
):
"""
Create a pandas DataFrame agent using the custom LLM.
Args:
df (pd.DataFrame): The pandas DataFrame to use.
verbose (bool): Whether to enable verbose output. Default is True.
allow_dangerous_code (bool): Whether to allow dangerous code execution. Default is True.
agent_type: The agent type to use. Default is ZERO_SHOT_REACT_DESCRIPTION.
Returns:
agent: The created DataFrame agent.
"""
llm = LLM().chat_model
agent = create_pandas_dataframe_agent(
llm,
df,
agent_type=agent_type,
verbose=verbose,
allow_dangerous_code=allow_dangerous_code
)
return agent
# Usage example:
# import pandas as pd
# from agents.dataframe_agent import get_dataframe_agent
# df = pd.read_csv('your_file.csv')
# agent = get_dataframe_agent(df)
# response = agent.invoke('Your question here')
# print(response)
|