Open twobitunicorn opened 2 weeks ago
This code does work.
import pandas as pd
from langchain_openai import ChatOpenAI
from langchain_core.output_parsers import JsonOutputParser
from langchain_experimental.agents.agent_toolkits import create_pandas_dataframe_agent
# Step 1: Load your DataFrame
df = pd.read_csv("https://raw.githubusercontent.com/pandas-dev/pandas/main/doc/data/titanic.csv")
# Step 2: Initialize the LLM
model = ChatOpenAI(temperature=0)
# Step 3: Create a Pandas DataFrame Agent
agent = create_pandas_dataframe_agent(model, df, verbose=True)
# Step 4: Define the JsonOutputParser
parser = JsonOutputParser()
# Step 5: Invoke the agent with a query
query = "How many people survived?"
result = agent.invoke(query)
# Step 6: Ensure the result is a string before parsing
if isinstance(result, dict) and 'output' in result:
output_text = result['output']
else:
output_text = result
# Step 7: Parse the output
parsed_result = parser.parse(output_text)
# Output the result
print(parsed_result)
Hi @twobitunicorn where did you find the example?
Checked other resources
Example Code
Error Message and Stack Trace (if applicable)
new AgentExecutor chain... Thought: To find out how many people survived, I need to sum the 'Survived' column in the dataframe. Action: python_repl_ast Action Input: df['Survived'].sum()342I now know the final answer Final Answer: 342 people survived.
Description
I was getting that failure in my code and went to the site and chatted with the input
how can I chain an Agent with a JsonOutputParser
System Info
System Information
Package Information
Optional packages not installed
Other Dependencies