|
|
@ -2,7 +2,11 @@
|
|
|
|
from typing import Any, Dict, List, Optional
|
|
|
|
from typing import Any, Dict, List, Optional
|
|
|
|
|
|
|
|
|
|
|
|
from langchain.agents.agent import AgentExecutor
|
|
|
|
from langchain.agents.agent import AgentExecutor
|
|
|
|
from langchain.agents.agent_toolkits.pandas.prompt import PREFIX, SUFFIX
|
|
|
|
from langchain.agents.agent_toolkits.pandas.prompt import (
|
|
|
|
|
|
|
|
PREFIX,
|
|
|
|
|
|
|
|
SUFFIX_NO_DF,
|
|
|
|
|
|
|
|
SUFFIX_WITH_DF,
|
|
|
|
|
|
|
|
)
|
|
|
|
from langchain.agents.mrkl.base import ZeroShotAgent
|
|
|
|
from langchain.agents.mrkl.base import ZeroShotAgent
|
|
|
|
from langchain.base_language import BaseLanguageModel
|
|
|
|
from langchain.base_language import BaseLanguageModel
|
|
|
|
from langchain.callbacks.base import BaseCallbackManager
|
|
|
|
from langchain.callbacks.base import BaseCallbackManager
|
|
|
@ -15,7 +19,7 @@ def create_pandas_dataframe_agent(
|
|
|
|
df: Any,
|
|
|
|
df: Any,
|
|
|
|
callback_manager: Optional[BaseCallbackManager] = None,
|
|
|
|
callback_manager: Optional[BaseCallbackManager] = None,
|
|
|
|
prefix: str = PREFIX,
|
|
|
|
prefix: str = PREFIX,
|
|
|
|
suffix: str = SUFFIX,
|
|
|
|
suffix: Optional[str] = None,
|
|
|
|
input_variables: Optional[List[str]] = None,
|
|
|
|
input_variables: Optional[List[str]] = None,
|
|
|
|
verbose: bool = False,
|
|
|
|
verbose: bool = False,
|
|
|
|
return_intermediate_steps: bool = False,
|
|
|
|
return_intermediate_steps: bool = False,
|
|
|
@ -23,6 +27,7 @@ def create_pandas_dataframe_agent(
|
|
|
|
max_execution_time: Optional[float] = None,
|
|
|
|
max_execution_time: Optional[float] = None,
|
|
|
|
early_stopping_method: str = "force",
|
|
|
|
early_stopping_method: str = "force",
|
|
|
|
agent_executor_kwargs: Optional[Dict[str, Any]] = None,
|
|
|
|
agent_executor_kwargs: Optional[Dict[str, Any]] = None,
|
|
|
|
|
|
|
|
include_df_in_prompt: Optional[bool] = True,
|
|
|
|
**kwargs: Dict[str, Any],
|
|
|
|
**kwargs: Dict[str, Any],
|
|
|
|
) -> AgentExecutor:
|
|
|
|
) -> AgentExecutor:
|
|
|
|
"""Construct a pandas agent from an LLM and dataframe."""
|
|
|
|
"""Construct a pandas agent from an LLM and dataframe."""
|
|
|
@ -35,14 +40,27 @@ def create_pandas_dataframe_agent(
|
|
|
|
|
|
|
|
|
|
|
|
if not isinstance(df, pd.DataFrame):
|
|
|
|
if not isinstance(df, pd.DataFrame):
|
|
|
|
raise ValueError(f"Expected pandas object, got {type(df)}")
|
|
|
|
raise ValueError(f"Expected pandas object, got {type(df)}")
|
|
|
|
|
|
|
|
if include_df_in_prompt is not None and suffix is not None:
|
|
|
|
if input_variables is None:
|
|
|
|
raise ValueError("If suffix is specified, include_df_in_prompt should not be.")
|
|
|
|
input_variables = ["df", "input", "agent_scratchpad"]
|
|
|
|
if suffix is not None:
|
|
|
|
|
|
|
|
suffix_to_use = suffix
|
|
|
|
|
|
|
|
if input_variables is None:
|
|
|
|
|
|
|
|
input_variables = ["df", "input", "agent_scratchpad"]
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
if include_df_in_prompt:
|
|
|
|
|
|
|
|
suffix_to_use = SUFFIX_WITH_DF
|
|
|
|
|
|
|
|
input_variables = ["df", "input", "agent_scratchpad"]
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
suffix_to_use = SUFFIX_NO_DF
|
|
|
|
|
|
|
|
input_variables = ["input", "agent_scratchpad"]
|
|
|
|
tools = [PythonAstREPLTool(locals={"df": df})]
|
|
|
|
tools = [PythonAstREPLTool(locals={"df": df})]
|
|
|
|
prompt = ZeroShotAgent.create_prompt(
|
|
|
|
prompt = ZeroShotAgent.create_prompt(
|
|
|
|
tools, prefix=prefix, suffix=suffix, input_variables=input_variables
|
|
|
|
tools, prefix=prefix, suffix=suffix_to_use, input_variables=input_variables
|
|
|
|
)
|
|
|
|
)
|
|
|
|
partial_prompt = prompt.partial(df=str(df.head().to_markdown()))
|
|
|
|
if "df" in input_variables:
|
|
|
|
|
|
|
|
partial_prompt = prompt.partial(df=str(df.head().to_markdown()))
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
partial_prompt = prompt
|
|
|
|
llm_chain = LLMChain(
|
|
|
|
llm_chain = LLMChain(
|
|
|
|
llm=llm,
|
|
|
|
llm=llm,
|
|
|
|
prompt=partial_prompt,
|
|
|
|
prompt=partial_prompt,
|
|
|
|