š§ AI-Powered Code GenerationApril 25, 2026ā
Tests passing
AI Code Review Assistant
This Python script acts as an AI-powered code review assistant by integrating with Claude Code or similar tools. It allows developers to provide a piece of code and receive a detailed review with suggestions for optimization, error fixes, and improvementsāall from the CLI.
What It Does
- Analyze code snippets or files using OpenAI's GPT-4 model.
- Specify the programming language of the code for tailored feedback.
- Save the AI-generated review to a JSON file.
Installation
- Python 3.7+
openaiPython package
Install the required package using pip:
pip install openaiUsage
Run the script from the command line with the following options:
python ai_code_review_assistant.py --api-key YOUR_API_KEY --file path/to/code.py --language Python --save review.jsonArguments
--file: Path to the code file to review.--code: Code snippet to review (alternative to--file).--save: Path to save the review output as a JSON file.--api-key: Your OpenAI API key (required).--language: Programming language of the code (default: Python).
Examples
1. Review a code snippet directly:
python ai_code_review_assistant.py --api-key YOUR_API_KEY --code "print('Hello, world!')"2. Review a code file and save the output:
python ai_code_review_assistant.py --api-key YOUR_API_KEY --file example.py --save review.jsonSource Code
import argparse
import json
import os
from openai.error import OpenAIError
from openai import ChatCompletion
import openai
def analyze_code_with_ai(api_key, code_snippet, language="Python"):
"""
Analyze the provided code snippet using OpenAI's API.
Args:
api_key (str): OpenAI API key.
code_snippet (str): The code snippet to analyze.
language (str): Programming language of the code.
Returns:
str: AI-generated review and suggestions.
"""
try:
openai.api_key = api_key
response = ChatCompletion.create(
model="gpt-4",
messages=[
{"role": "system", "content": f"You are an expert code reviewer for {language} code."},
{"role": "user", "content": f"Please review the following {language} code and provide suggestions for improvement, optimization, and error fixes:\n\n{code_snippet}"}
]
)
return response["choices"][0]["message"]["content"]
except OpenAIError as e:
return f"Error communicating with OpenAI API: {str(e)}"
def load_code_from_file(file_path):
"""
Load code from a file.
Args:
file_path (str): Path to the file containing the code.
Returns:
str: Content of the file.
"""
if not os.path.exists(file_path):
raise FileNotFoundError(f"The file '{file_path}' does not exist.")
with open(file_path, "r", encoding="utf-8") as file:
return file.read()
def main():
parser = argparse.ArgumentParser(description="AI Code Review Assistant")
parser.add_argument("--file", type=str, help="Path to the code file to review.")
parser.add_argument("--code", type=str, help="Code snippet to review.")
parser.add_argument("--save", type=str, help="Path to save the review output as a JSON file.")
parser.add_argument("--api-key", type=str, required=True, help="OpenAI API key.")
parser.add_argument("--language", type=str, default="Python", help="Programming language of the code (default: Python).")
args = parser.parse_args()
if not args.file and not args.code:
print("Error: Either --file or --code must be provided.")
return
if args.file:
try:
code_snippet = load_code_from_file(args.file)
except FileNotFoundError as e:
print(e)
return
else:
code_snippet = args.code
print("Analyzing code... This may take a moment.")
review = analyze_code_with_ai(args.api_key, code_snippet, args.language)
if args.save:
try:
with open(args.save, "w", encoding="utf-8") as file:
json.dump({"review": review}, file, indent=4)
print(f"Review saved to {args.save}")
except Exception as e:
print(f"Error saving the review: {str(e)}")
else:
print("\nAI Code Review:\n")
print(review)
if __name__ == "__main__":
main()
Community
Downloads
Ā·Ā·Ā·
Rate this tool
No ratings yet ā be the first!
Details
- Tool Name
- ai_code_review_assistant
- Category
- AI-Powered Code Generation
- Generated
- April 25, 2026
- Tests
- Passing ā
- Fix Loops
- 3
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-25/ai_code_review_assistant cd generated_tools/2026-04-25/ai_code_review_assistant pip install -r requirements.txt 2>/dev/null || true python ai_code_review_assistant.py