Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python code generated by Agent results in an error during the first pass #33

Open
msmmpts opened this issue Jan 20, 2025 · 0 comments
Open

Comments

@msmmpts
Copy link

msmmpts commented Jan 20, 2025

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:

import numpy as np
import matplotlib.pyplot as plt

# Define the range of x values
x = np.linspace(-1, 1, 100)
# Calculate y values
y = x**2

# Create the plot
plt.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.

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)

        # 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"
),
)
]

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)

`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant