You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I was making use of langchain agents along with PythonREPLTool to execute python code.
Here are the logs:
To generate a plot for the function ( y = x^2 ) over the range from -1 to 1, I will create a set of x values within this range, compute the corresponding y values, and then use matplotlib to visualize the function. After plotting, I will analyze the graph for insights.
Action: Code Interpreter with Plot Display
Action Input:
importnumpyasnpimportmatplotlib.pyplotasplt# Define the range of x valuesx=np.linspace(-1, 1, 100)
# Calculate y valuesy=x**2# Create the plotplt.figure(figsize=(8, 6))
plt.plot(x, y, label='y = x^2', color='blue')
plt.title('Plot of y = x^2')
plt.xlabel('x')
plt.ylabel('y')
plt.axhline(0, color='black',linewidth=0.5, ls='--')
plt.axvline(0, color='black',linewidth=0.5, ls='--')
plt.grid(color='gray', linestyle='--', linewidth=0.5)
plt.legend()
plt.xlim(-1.5, 1.5)
plt.ylim(-0.5, 1.5)
plt.show()
Observation: Error: invalid syntax (, line 1)
During the second pass, agent corrects the code and then runs it properly.
i.e for every code generated (starts with """```python""") generates code with syntax error and might take several iterations for it to execute and run code resulting in additional latency and tokens.
Can anyone suggest alternatives for fixing this?
Note: I have already included in the description of the agent not to start with keywords like ```python etc but still it generates python code with syntax issues.
`
import matplotlib.pyplot as plt
from langchain.agents import initialize_agent, Tool
from langchain_experimental.tools.python.tool import PythonREPLTool
from langchain_openai import AzureChatOpenAI
# Check if a plot is generated
if "plt" in exec_globals:
plt.show() # Display the plot locally
return "Plot displayed successfully."
return "Code executed successfully, but no plot was generated."
except Exception as e:
return f"Error: {str(e)}"
Initialize the Tool
code_tool = LocalPythonREPLTool()
tools = [
Tool(
name="Code Interpreter with Plot Display",
func=code_tool.run,
description=(
"A Python code interpreter capable of executing Python code locally. "
"It can generate and display plots using matplotlib or other libraries. "
"Input should be valid Python code. The python code should not start with keywords like ```python"
),
)
]
if name == "main":
query = (
"Generate a plot for y = x^2 for x in the range -1 to 1 using matplotlib and report insights from the plot"
)
response = agent.run(query)
print(response)
`
The text was updated successfully, but these errors were encountered:
Hi team,
I was making use of langchain agents along with PythonREPLTool to execute python code.
Here are the logs:
To generate a plot for the function ( y = x^2 ) over the range from -1 to 1, I will create a set of x values within this range, compute the corresponding y values, and then use matplotlib to visualize the function. After plotting, I will analyze the graph for insights.
Action: Code Interpreter with Plot Display
Action Input:
Observation: Error: invalid syntax (, line 1)
During the second pass, agent corrects the code and then runs it properly.
i.e for every code generated (starts with """```python""") generates code with syntax error and might take several iterations for it to execute and run code resulting in additional latency and tokens.
Can anyone suggest alternatives for fixing this?
Note: I have already included in the description of the agent not to start with keywords like ```python etc but still it generates python code with syntax issues.
Model tried : gpt-4o, gpt-4o-mini
Library versions installed
langchain 0.3.14
langchain-aws 0.2.10
langchain-community 0.3.14
langchain-core 0.3.29
langchain-experimental 0.3.4
langchain-google-genai 2.0.8
langchain-openai 0.3.0
langchain-text-splitters 0.3.5
Code that was used:
`
import matplotlib.pyplot as plt
from langchain.agents import initialize_agent, Tool
from langchain_experimental.tools.python.tool import PythonREPLTool
from langchain_openai import AzureChatOpenAI
class LocalPythonREPLTool(PythonREPLTool):
def run(self, code: str) -> str:
try:
# Create a secure execution environment
exec_globals = {"plt": plt}
exec_locals = {}
exec(code, exec_globals, exec_locals)
Initialize the Tool
code_tool = LocalPythonREPLTool()
tools = [
Tool(
name="Code Interpreter with Plot Display",
func=code_tool.run,
description=(
"A Python code interpreter capable of executing Python code locally. "
"It can generate and display plots using matplotlib or other libraries. "
"Input should be valid Python code. The python code should not start with keywords like ```python"
),
)
]
llm = AzureChatOpenAI()
Initialize the Agent
agent = initialize_agent(
tools=tools,
llm=llm,
agent="zero-shot-react-description",
verbose=True
)
Test the Agent
if name == "main":
query = (
"Generate a plot for y = x^2 for x in the range -1 to 1 using matplotlib and report insights from the plot"
)
response = agent.run(query)
print(response)
`
The text was updated successfully, but these errors were encountered: