I observe strange file loading/import behaviour, where my Agents create a python script save it to a file. And on later runs it first imports the previously created script before anything else.
minimal Example:
from crewai import Agent, Task, Crew, LLM, Process
from crewai.tools import tool
print("start")
localLlm=LLM(
model="ollama/openhermes",
api_key="ollama",
base_url="http://localhost:11434"
)
@tool("read_from_file")
def read_from_file(file_name: str) -> str:
"""
Reads the content of the specified file and returns it as a string.
Args:
file_name (str): The name of the file to read.
Returns:
str: The content of the file as a string, or an error message if an exception occurs.
"""
try:
with open(file_name, 'r', encoding='utf-8') as file:
content = file.read()
return content
except Exception as e:
return f"An error occurred while reading the file '{file_name}': {e}"
@tool("save_to_file")
def save_to_file(file_name: str, content: str) -> str:
"""
Saves the provided content to a file.
Args:
file_name (str): The name of the file to save the content in.
content (str): The content to be saved.
Returns:
str: Success message upon file creation or an error message if an exception occurs.
"""
try:
with open(file_name, 'w', encoding='utf-8') as file:
file.write(content)
return f"File '{file_name}' created successfully."
except Exception as e:
return f"An error occurred while creating the file: {e}"
# Create agents
developer = Agent(
llm=localLlm,
role="Developer",
goal="Think, create and debug Python code based on given requirements.",
backstory="You are a senior Python developer to create efficient thought through python code. You are able to run debug and design code based on best practices for python coding.",
allow_code_execution=True, # This agent focuses solely on code generation.
code_execution_mode="unsafe"
)
save_agent = Agent(
llm=localLlm,
role="File Administrator",
goal="Save only the python code to a code.py file.",
backstory="An AI agent specialized in detecting Python code and saving it to file.",
tools=[save_to_file],
)
# Task for the Code Generation Agent
generate_code_task = Task(
description="Generate a Python script that prints 'Hello, World!'.",
expected_output="A Python script that prints 'hello world!'.",
agent=developer
)
# Task for the Code Testing Agent
test_code_task = Task(
description="Test the generated Python script to ensure it prints 'Hello, World!'.",
expected_output="Test the script to ensure that it prints 'hello world!'.",
agent=developer,
async_execution=False
)
debug_code = Task(
description="Debug and improve python code.",
expected_output="improved code.",
agent=developer
)
save_code = Task(
description="Save generated code to a file naned code.py",
expected_output="Saved generated python code to code.py",
agent=save_agent,
async_execution=False
)
# Assemble a crew with planning enabled
crew = Crew(
llm=localLlm,
agents=[developer, save_agent],
tasks=[generate_code_task, debug_code,test_code_task, save_code],
process=Process.sequential, # Execute tasks in sequence.
verbose=True
)
print("before kickoff")
# Execute tasks
crew.kickoff()
print("After kickoff")
logoutput:
(venv) (main) > python minimal.py
hello world!
start
before kickoff
# Agent: Developer
## Task: Generate a Python script that prints 'Hello, World!'.
This behaviour leads to errors when calling the main.py if the generated code in (code.py) was faulty.
I.e.
File "/Users/steven/projects/custom-crew/tool_test/src/tool_test/code.py", line 1
Hello, World!
^
SyntaxError: invalid syntax
There are other .py file in the directory aswell, they don’t get imported, I can rename generated code.py file to any other name and it’s also not getting imported. So whats happening here?