forked from langchain-ai/langchain
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move csv agent to langchain experimental (langchain-ai#12113)
- Loading branch information
Showing
2 changed files
with
38 additions
and
0 deletions.
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
libs/experimental/langchain_experimental/agents/agent_toolkits/csv/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
"""CSV toolkit.""" |
37 changes: 37 additions & 0 deletions
37
libs/experimental/langchain_experimental/agents/agent_toolkits/csv/base.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from io import IOBase | ||
from typing import Any, List, Optional, Union | ||
|
||
from langchain.agents.agent import AgentExecutor | ||
from langchain.schema.language_model import BaseLanguageModel | ||
|
||
from langchain_experimental.agents.agent_toolkits.pandas.base import ( | ||
create_pandas_dataframe_agent, | ||
) | ||
|
||
|
||
def create_csv_agent( | ||
llm: BaseLanguageModel, | ||
path: Union[str, IOBase, List[Union[str, IOBase]]], | ||
pandas_kwargs: Optional[dict] = None, | ||
**kwargs: Any, | ||
) -> AgentExecutor: | ||
"""Create csv agent by loading to a dataframe and using pandas agent.""" | ||
try: | ||
import pandas as pd | ||
except ImportError: | ||
raise ImportError( | ||
"pandas package not found, please install with `pip install pandas`" | ||
) | ||
|
||
_kwargs = pandas_kwargs or {} | ||
if isinstance(path, (str, IOBase)): | ||
df = pd.read_csv(path, **_kwargs) | ||
elif isinstance(path, list): | ||
df = [] | ||
for item in path: | ||
if not isinstance(item, (str, IOBase)): | ||
raise ValueError(f"Expected str or file-like object, got {type(path)}") | ||
df.append(pd.read_csv(item, **_kwargs)) | ||
else: | ||
raise ValueError(f"Expected str, list, or file-like object, got {type(path)}") | ||
return create_pandas_dataframe_agent(llm, df, **kwargs) |