CodeFixer is a tool for automatic analysis and fixing of errors in Python code using Large Language Models (LLM) via the OpenRouter API. The tool identifies syntax and logical errors, coding style issues, and potential vulnerabilities, then suggests and applies corrections.
- Automatic detection and fixing of syntax errors
- Correction of logical errors and suboptimal solutions
- Improvement of coding style according to PEP 8
- Addition of exception handling in problematic areas
- Creation of file backups before making changes
- Support for various models through OpenRouter
- Clone the repository:
git clone https://github.com/yourusername/codefixer.git
cd codefixer- Install dependencies:
pip install -r requirements.txt- Create a
.envfile in the project root and add your API key:
OPENROUTER_API_KEY=your_api_key
To analyze and fix a file:
python main.py path_to_file.pyWith additional parameters:
python main.py path_to_file.py --model="anthropic/claude-3-haiku-20240307" --debugfile- path to the file to check (required parameter)--model- LLM model to use (default: mistralai/mistral-small-24b-instruct-2501)--debug- enable debug mode with detailed logging
CodeFixer employs an innovative approach to interacting with language models through prompt-tools. Instead of using the standard OpenAI functions API, it uses a prompt engineering system that allows:
- Formulating requests to the model with instructions for code analysis
- Processing model responses, extracting structured data
- Applying suggested changes to files with validity checking
- Reading the source code of the Python file
- Creating a specialized prompt for the language model
- Sending a request to the LLM via OpenRouter API
- Processing the response and extracting JSON with suggested changes
- Checking and filtering changes
- Creating a backup of the original file
- Applying changes to the file
- Outputting a report on the fixes made
CodeFixer supports three types of changes:
replace- replacing a part of the codeinsert- inserting new codedelete- removing a part of the code
Starting from version 2.0, CodeFixer uses a prompt-tools system for more flexible interaction with LLM. This allows:
- Breaking down complex tasks into sequential steps
- Performing deeper code analysis
- Supporting an interactive analysis process
- Ensuring compatibility with various models
Prompt tools in CodeFixer include:
analyze_code()- analyzes the provided code and finds issuessuggest_fixes()- suggests fixes for the identified problemsformat_changes()- formats the suggested fixes into a JSON structure
- Accuracy of fixes depends on the model used
- Complex logical errors may require manual verification
- Some specific code features or non-standard libraries might not be recognized
You can configure the following parameters at the beginning of the main.py file:
DEFAULT_MODEL- default model for code analysisSITE_URL- your site URL (for OpenRouter statistics)SITE_NAME- your project name (for OpenRouter statistics)
We welcome contributions to the project! If you have suggestions for improvements or have found a bug:
- Create an issue describing the problem or suggestion
- Fork the repository and create a branch for your changes
- Make changes and create a pull request
This project is distributed under the MIT license. Detailed information can be found in the LICENSE file.