Skip to content

Commit

Permalink
auto-format migrate-csv-agent page
Browse files Browse the repository at this point in the history
  • Loading branch information
Max Shkutnyk committed Jan 15, 2025
1 parent a8f9d63 commit 52179d4
Showing 1 changed file with 34 additions and 13 deletions.
47 changes: 34 additions & 13 deletions fern/pages/cookbooks/migrate-csv-agent.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ from pydantic import BaseModel, Field

from langchain.agents import AgentExecutor, create_tool_calling_agent
from langchain_core.language_models import BaseLanguageModel
from langchain_core.messages import BaseMessage, HumanMessage, SystemMessage
from langchain_core.messages import (
BaseMessage,
HumanMessage,
SystemMessage,
)
from langchain_core.prompts import (
ChatPromptTemplate,
MessagesPlaceholder,
Expand Down Expand Up @@ -111,6 +115,7 @@ The above cell introduces a suite of tools that are provided to the csv agent. T
```python
# Define tools that we want the csv agent to have access to


def get_file_peek_tool() -> Tool:
def file_peek(filename: str, num_rows: int = 5) -> str:
"""Returns the first textual contents of an uploaded file
Expand Down Expand Up @@ -152,7 +157,9 @@ def get_file_read_tool() -> Tool:
return "the table_path was not recognised"

class file_read_inputs(BaseModel):
filename: str = Field(description="The name of the attached file to read.")
filename: str = Field(
description="The name of the attached file to read."
)

file_read_tool = Tool(
name="file_read",
Expand Down Expand Up @@ -198,7 +205,9 @@ def create_prompt(
system_message: Optional[BaseMessage] = SystemMessage(
content="You are a helpful AI assistant."
),
extra_prompt_messages: Optional[List[BaseMessagePromptTemplate]] = None,
extra_prompt_messages: Optional[
List[BaseMessagePromptTemplate]
] = None,
) -> ChatPromptTemplate:
"""Create prompt for this agent.
Expand Down Expand Up @@ -254,7 +263,9 @@ def _get_prompt(
)
prompt_message = " ".join(prompt_messages)

prompt = create_prompt(system_message=HumanMessage(prompt_message))
prompt = create_prompt(
system_message=HumanMessage(prompt_message)
)
return prompt


Expand Down Expand Up @@ -335,10 +346,14 @@ def create_csv_agent(
df = []
for item in path:
if not isinstance(item, (str, IOBase)):
raise ValueError(f"Expected str or file-like object, got {type(path)}")
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)}")
raise ValueError(
f"Expected str, list, or file-like object, got {type(path)}"
)

if not prompt:
prompt = _get_prompt(path, number_of_head_rows)
Expand All @@ -348,13 +363,19 @@ def create_csv_agent(
get_file_peek_tool(),
get_python_tool(),
] + extra_tools
if "preamble" in llm.__dict__ and not llm.__dict__.get("preamble"):
if "preamble" in llm.__dict__ and not llm.__dict__.get(
"preamble"
):
llm = ChatCohere(**llm.__dict__)
llm.preamble = CSV_PREAMBLE.format(
current_date=datetime.now().strftime("%A, %B %d, %Y %H:%M:%S")
current_date=datetime.now().strftime(
"%A, %B %d, %Y %H:%M:%S"
)
)

agent = create_tool_calling_agent(llm=llm, tools=final_tools, prompt=prompt)
agent = create_tool_calling_agent(
llm=llm, tools=final_tools, prompt=prompt
)
agent_executor = AgentExecutor(
agent=agent,
tools=final_tools,
Expand Down Expand Up @@ -403,10 +424,10 @@ Let's use our CSV Agent to interact with the CSV file
```python
# Try out an example
llm = ChatCohere(model="command-r-plus", temperature=0)
agent_executor = create_csv_agent(
llm,
"movies_tickets.csv")
resp = agent_executor.invoke({"input":"Who all watched Shawshank redemption?"})
agent_executor = create_csv_agent(llm, "movies_tickets.csv")
resp = agent_executor.invoke(
{"input": "Who all watched Shawshank redemption?"}
)
print(resp.get("output"))
```

Expand Down

0 comments on commit 52179d4

Please sign in to comment.