0

The code is running smoothly on Jupyter notebook. It is generating a count plot or any chart I want. However, while I run it on VS Code, it is not giving me any kind of charts. The code gives textual answers as expected but it does not display any chart. VS Code does not even give any error.

Does anyone know how to resolve it?

from langchain.agents import create_csv_agent
from langchain.llms import OpenAI

import os
import pandas
os.environ["OPENAI_API_KEY"] = "Your-API-Key"

agent = create_csv_agent(OpenAI(temperature=0.2), "supermarket_sales - Sheet1.csv", verbose=True)

agent.run("Generate a count plot of Payment column?")

If anyone can try out the code at their end with your choice of dataset and question, do reach out if you are able to generate any plot/graph on VS Code.

starball
  • 20,030
  • 7
  • 43
  • 238

2 Answers2

1

I ran the following with VSCode Interactive Python:

agent = create_csv_agent(OpenAI(temperature=0.2), "https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2023/2023-03-07/numbats.csv", verbose=True)

agent.run("Generate a count plot of scientificName column")
> Entering new AgentExecutor chain...
Thought: I need to count the number of occurrences of each scientificName
Action: python_repl_ast
Action Input: df['scientificName'].value_counts().plot(kind='bar')
Observation: matplotlib is required for plotting when the default backend "matplotlib" is selected.
Thought: I need to import matplotlib
Action: python_repl_ast
Action Input: import matplotlib.pyplot as plt
Observation: No module named 'matplotlib'
Thought: I need to install matplotlib
Action: python_repl_ast
Action Input: pip install matplotlib
Observation: invalid syntax (<unknown>, line 1)
Thought: I need to run this command in the terminal
Action: terminal
Action Input: pip install matplotlib
Observation: terminal is not a valid tool, try another one.
Thought: I need to run this command in the command line
Action: command_line
Action Input: pip install matplotlib
Observation: command_line is not a valid tool, try another one.
Thought: I need to run this command in the command prompt
Action: command_prompt
Action Input: pip install matplotlib
Observation: command_prompt is not a valid tool, try another one.
Thought: I need to run this command in the command line interface
Action: command_line_interface
Action Input: pip install matplotlib
Observation: command_line_interface is not a valid tool, try another one.
Thought: I need to run this command in the terminal
Action: terminal
Action Input: pip install matplotlib
Observation: terminal is not a valid tool, try another one.
Thought: I need to run this command in the command line
Action: command_line
Action Input: pip install matplotlib
Observation: command_line is not a valid tool, try another one.
Thought: I need to run this command in the command prompt
Action: command_prompt
Action Input: pip install matplotlib
Observation: command_prompt is not a valid tool, try another one.
Thought: I need to run this command in the command line interface
Action: command_line_interface
...
Observation: command_line_interface is not a valid tool, try another one.
Thought:

> Finished chain.
'Agent stopped due to iteration limit or time limit.'

I didn't have matplotlib installed. After running pip install matplotlib in terminal, the agent was successfully able to generate a plot:

> Entering new AgentExecutor chain...
Thought: I need to count the number of times each scientificName appears in the dataframe
Action: python_repl_ast
Action Input: df['scientificName'].value_counts().plot(kind='bar')
Observation: Axes(0.125,0.11;0.775x0.77)
Thought: I now know the final answer
Final Answer: A count plot of the scientificName column.

enter image description here

Desmond
  • 1,047
  • 7
  • 14
  • Hello @Desmond, Thank you for answering my question. I tried changing the interpreter according to Python interactive Window link you provided. In my case matplotlib was already installed. But the error stills exists. I tried asking a different question. It worked. However, when I tried few more test cases, it is again running without errors but NO GRAPHS. As expected all these test cases are running correctly along with displaying graphs on Jupyter notebook. If you don't mind, could we connect over a video call so that you can see the error? I appreciate your valuable time. Regards. – Anand Varrier Apr 17 '23 at 07:06
  • Hi Anand, I suggest you screenshot those errors and edit your original question to include these errors. That way it'll be more helpful to future folks looking for a solution with similar errors. Do provide the exact prompts and code you used too. – Desmond Apr 17 '23 at 07:51
0

try to import pyplot and show the plot after the call to ask:

import matplotlib.pyplot as plt

...

agent.run("Generate a count plot of Payment column?")
plt.show()