A powerful AI-driven coding agent built with Google Gemini 2.0 Flash that can autonomously interact with codebases, analyze code, fix bugs, and execute Python programs.
PyAIgent is designed as a true "agent" with a feedback loop that allows it to:
- Analyze codebases by reading files and understanding project structure
- Execute Python code and analyze outputs
- Fix bugs by modifying source code
- Create new files and directory structures
- Iteratively solve complex problems through multi-step reasoning
-
File System Operations
- List files and directories with size information
- Read file contents (up to 10,000 characters per file)
- Write and overwrite files with automatic directory creation
-
Code Execution
- Execute Python files with timeout protection (30 seconds)
- Capture both stdout and stderr
- Report exit codes for debugging
-
Intelligent Agent Loop
- Up to 20 iterations per task to prevent infinite loops
- Maintains conversation context between iterations
- Automatically stops when task is completed
-
Security Features
- All operations are sandboxed to the
./calculatordirectory - Path traversal protection
- Input validation and error handling
- All operations are sandboxed to the
-
Clone the repository
git clone <repository-url> cd pyaigent
-
Install dependencies
pip install -r requirements.txt
-
Set up environment variables Create a
.envfile in the project root:GEMINI_API_KEY=your_gemini_api_key_here
Get your API key from Google AI Studio
python main.py "your prompt here" [--verbose]--verbose: Enable detailed logging showing function calls, token usage, and iteration details
# Analyze how a function works
python main.py "explain how the calculator renders results to the console"
# Understand project structure
python main.py "read the files in the current directory and explain the project structure"
# Analyze specific functionality
python main.py "how does the calculator handle operator precedence?"# Fix mathematical bugs
python main.py "fix the bug: 3 + 7 * 2 shouldn't be 20"
# Debug execution issues
python main.py "the calculator app outputs wrong results, find and fix the bug"
# Fix syntax or logic errors
python main.py "there's an error in the calculator code, please fix it"# Add new features
python main.py "add support for parentheses in mathematical expressions"
# Create new functionality
python main.py "create a function to validate mathematical expressions before evaluation"
# Refactor code
python main.py "improve the error handling in the calculator"# Run tests
python main.py "run the tests and fix any failing ones"
# Create test cases
python main.py "create comprehensive tests for the calculator functionality"
# Validate specific scenarios
python main.py "test the calculator with complex expressions and edge cases"# Generate documentation
python main.py "add detailed comments to the calculator code"
# Code review
python main.py "review the code for potential improvements and best practices"
# Create examples
python main.py "create example usage scenarios for the calculator"Use --verbose flag to see detailed execution:
# See step-by-step execution
python main.py "fix the calculator bug" --verbose
# Monitor token usage and function calls
python main.py "analyze the render function" --verboseExample verbose output:
User prompt: fix the calculator bug
--- Iteration 1 ---
Prompt tokens: 338
Response tokens: 76
- Calling function: get_files_info
-> {'result': '- README.md: file_size=12 bytes, is_dir=False\n...'}
--- Iteration 2 ---
Prompt tokens: 517
Response tokens: 47
- Calling function: get_file_content
-> {'result': 'class Calculator:\n def __init__(self):\n...'}
Final response:
I have fixed the operator precedence bug in the calculator...
PyAIgent implements a feedback loop architecture:
- User Input: Receives a natural language prompt
- LLM Processing: Uses Google Gemini 2.0 Flash to understand the request
- Function Calling: Executes appropriate functions based on the task
- Context Maintenance: Keeps conversation history for multi-step reasoning
- Iteration: Continues until task completion or maximum iterations reached
The agent has access to four core functions:
Lists files and directories with metadata:
# Lists files in current directory
get_files_info(".")
# Lists files in subdirectory
get_files_info("pkg")Reads and returns file contents:
# Read a Python file
get_file_content("main.py")
# Read a configuration file
get_file_content("config.json")Executes Python files and captures output:
# Run the main calculator
run_python_file("main.py")
# Execute tests
run_python_file("tests.py")Creates or overwrites files:
# Create a new Python file
write_file("new_feature.py", "def new_function():\n pass")
# Fix an existing file
write_file("calculator.py", updated_code)All operations are constrained to the ./calculator directory for security. The agent can:
- Read any file within the calculator directory
- Execute Python files in the calculator directory
- Create new files and directories within the calculator directory
- Cannot access files outside this sandbox
The agent includes robust error handling:
- File not found: Clear error messages for missing files
- Permission errors: Graceful handling of access issues
- Execution timeouts: 30-second timeout for Python execution
- Invalid paths: Prevention of directory traversal attacks
- API limits: Graceful handling of rate limits and quotas
- Working Directory: Limited to
./calculatordirectory - File Size: File reading limited to 10,000 characters
- Execution Time: Python execution timeout of 30 seconds
- Iterations: Maximum 20 iterations per task
- Language: Currently optimized for Python code
python main.py "the calculator gives wrong results for '3 + 7 * 2', investigate and fix"This will typically:
- List files to understand structure
- Read calculator code
- Identify the precedence bug
- Fix the operator precedence
- Test the fix
- Confirm the correction
python main.py "explain how the calculator renders output with examples"This will typically:
- Explore the file structure
- Read the main calculator file
- Read the render module
- Explain the rendering process
- Provide examples of the output format
-
API Key Not Set
Error: Please set GEMINI_API_KEY in your .env file -
Rate Limits
Error: 429 RESOURCE_EXHAUSTEDWait for the rate limit to reset (usually 1 minute)
-
File Not Found
Error: File not found or is not a regular fileEnsure the file exists in the calculator directory
- Use
--verboseflag to see detailed execution steps - Check the error messages for specific issues
- Ensure your API key has sufficient quota
- Verify file paths are relative to the calculator directory
- Fork the repository
- Create a feature branch
- Make your changes
- Test thoroughly
- Submit a pull request
This project is open source. Please check the license file for details.