๐ป AI Coding AssistantsApril 11, 2026โ
Tests passing
AI Refactor Assistant
This Python library provides an interface for AI-assisted code refactoring. Developers can pass their scripts or functions, and the tool suggests improvements in readability, performance, or adherence to best practices. Useful for optimizing legacy codebases or improving development standards.
What It Does
- AI-driven code quality analysis: Leverages OpenAI's GPT-4 to provide intelligent suggestions for improving your code.
- Performance optimizations: Identifies potential performance bottlenecks and suggests improvements.
- Automatic formatting: Uses the Black code formatter to ensure consistent and clean code style.
Installation
- Python 3.7+
openai==0.27.8black==23.9.1pytest==7.4.2
Usage
Input Code:
def add(a, b):
return a + bRefactored Code:
def add(a, b):
return a + bSource Code
import ast
import openai
import black
import os
import argparse
def refactor_code(input_code, output_file=None, openai_api_key=None):
"""
Refactor Python code using AI suggestions and formatting.
Args:
input_code (str): Python code as a string or file path.
output_file (str, optional): Path to save the refactored code. Defaults to None.
openai_api_key (str, optional): OpenAI API key. Defaults to None.
Returns:
str: Refactored Python code.
"""
if openai_api_key is None:
openai_api_key = os.getenv("OPENAI_API_KEY")
if not openai_api_key:
raise ValueError("OpenAI API key not provided. Set it as an argument or in the OPENAI_API_KEY environment variable.")
# Load code from file if input_code is a file path
if os.path.isfile(input_code):
with open(input_code, "r") as file:
input_code = file.read()
# Validate input code using AST
try:
ast.parse(input_code)
except SyntaxError as e:
raise ValueError(f"Invalid Python code provided: {e}")
# Call OpenAI API for suggestions
openai.api_key = openai_api_key
try:
response = openai.ChatCompletion.create(
model="gpt-4",
messages=[
{"role": "system", "content": "You are a Python code refactoring assistant."},
{"role": "user", "content": f"Refactor the following Python code for readability, performance, and best practices:\n\n{input_code}"}
]
)
except openai.error.OpenAIError as e:
raise RuntimeError(f"Error communicating with OpenAI API: {e}")
# Extract the refactored code from the response
try:
refactored_code = response["choices"][0]["message"]["content"]
except (KeyError, IndexError):
raise RuntimeError("Unexpected response format from OpenAI API.")
# Format the refactored code using Black
try:
refactored_code = black.format_str(refactored_code, mode=black.Mode())
except black.InvalidInput as e:
raise ValueError(f"Error formatting code with Black: {e}")
# Save to file if output_file is specified
if output_file:
with open(output_file, "w") as file:
file.write(refactored_code)
return refactored_code
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="AI Refactor Assistant: Refactor Python code using AI.")
parser.add_argument("input_code", help="Path to the Python file to refactor or the Python code as a string.")
parser.add_argument("--output_file", help="Path to save the refactored code.", default=None)
parser.add_argument("--openai_api_key", help="OpenAI API key. If not provided, the OPENAI_API_KEY environment variable will be used.", default=None)
args = parser.parse_args()
try:
refactored_code = refactor_code(args.input_code, args.output_file, args.openai_api_key)
if not args.output_file:
print(refactored_code)
except Exception as e:
print(f"Error: {e}")
Community
Downloads
ยทยทยท
Rate this tool
No ratings yet โ be the first!
Details
- Tool Name
- ai_refactor_assistant
- Category
- AI Coding Assistants
- Generated
- April 11, 2026
- Tests
- Passing โ
Quick Install
Clone just this tool:
git clone --depth 1 --filter=blob:none --sparse \ https://github.com/ptulin/autoaiforge.git cd autoaiforge git sparse-checkout set generated_tools/2026-04-11/ai_refactor_assistant cd generated_tools/2026-04-11/ai_refactor_assistant pip install -r requirements.txt 2>/dev/null || true python ai_refactor_assistant.py