AI is flipping the game for developers, and honestly,
I got fed up of seeing people waste hours debugging code what a machine could do it in seconds. So, I thought—why not build an AI (agent type program) that does the boring stuff.
It all started when I was working on a project and constantly had to review my own code. While AI-powered coding assistants like GitHub Copilot help with writing code, I wondered, why isn’t there an AI to review my code? That’s when I decided to build one. So yeah, AI-powered code review isn’t just a convenience—it’s a lifesaver.
The plan was simple:
- Create a command-line tool where I can input a code snippet.
-
Use OpenAI’s GPT-4 to analyze and review the code.
(If you don’t know where to find your secret API key: Check Here.
-
Return a detailed review with suggestions, best practices, and potential bug fixes.
After some research, I decided on the following stack:
- Backend: Python (Standalone CLI tool)
- AI Model: OpenAI’s GPT-4 API
- Environment: Terminal-based command-line application
You can subscribe to Codexai for more such cool AI prompts.
Workflow
Here’s your premium prompt:
Please generate a Python script for a terminal-based AI code reviewer application that uses OpenAI's GPT model.The script should allow users to paste their code into the terminal, and upon submitting it, receive a detailed review of their code.The review should include feedback on:1. Code Quality2. Best Practices3. Potential Bugs4. Performance Improvements5. Security ConcernsThe script should load the OpenAI API key from a .env file and use it to call OpenAI's GPT-4 model.The program should allow the user to input code directly into the terminal, and when they press Enter twice, the review should be generated and displayed.The Python script should include proper error handling and user prompts for a smooth user experience.Also, ensure the code is clean, well-commented, and modular for easy understanding.The review output should be structured and clear, providing actionable insights for the user.Please generate a Python script for a terminal-based AI code reviewer application that uses OpenAI's GPT model. The script should allow users to paste their code into the terminal, and upon submitting it, receive a detailed review of their code. The review should include feedback on: 1. Code Quality 2. Best Practices 3. Potential Bugs 4. Performance Improvements 5. Security Concerns The script should load the OpenAI API key from a .env file and use it to call OpenAI's GPT-4 model. The program should allow the user to input code directly into the terminal, and when they press Enter twice, the review should be generated and displayed. The Python script should include proper error handling and user prompts for a smooth user experience. Also, ensure the code is clean, well-commented, and modular for easy understanding. The review output should be structured and clear, providing actionable insights for the user.Please generate a Python script for a terminal-based AI code reviewer application that uses OpenAI's GPT model. The script should allow users to paste their code into the terminal, and upon submitting it, receive a detailed review of their code. The review should include feedback on: 1. Code Quality 2. Best Practices 3. Potential Bugs 4. Performance Improvements 5. Security Concerns The script should load the OpenAI API key from a .env file and use it to call OpenAI's GPT-4 model. The program should allow the user to input code directly into the terminal, and when they press Enter twice, the review should be generated and displayed. The Python script should include proper error handling and user prompts for a smooth user experience. Also, ensure the code is clean, well-commented, and modular for easy understanding. The review output should be structured and clear, providing actionable insights for the user.
Enter fullscreen mode Exit fullscreen mode
Here’s your hands-on tutorial:
Step 1: Setting Up the Project
Install Dependencies
I started by creating a new Python project and installing the necessary libraries:
<span>mkdir </span>ai-code-reviewer <span>&&</span> <span>cd </span>ai-code-reviewerpython <span>-m</span> venv venv<span>source </span>venv/bin/activate <span># On Windows: venv\Scripts\activate</span>pip <span>install </span>openai python-dotenv<span>mkdir </span>ai-code-reviewer <span>&&</span> <span>cd </span>ai-code-reviewer python <span>-m</span> venv venv <span>source </span>venv/bin/activate <span># On Windows: venv\Scripts\activate</span> pip <span>install </span>openai python-dotenvmkdir ai-code-reviewer && cd ai-code-reviewer python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate pip install openai python-dotenv
Enter fullscreen mode Exit fullscreen mode
Next, I created a .env
file to store my OpenAI API key. You need to edit this file and add your own API key:
OPENAI_API_KEY=your_openai_api_key_hereOPENAI_API_KEY=your_openai_api_key_hereOPENAI_API_KEY=your_openai_api_key_here
Enter fullscreen mode Exit fullscreen mode
Step 2: Writing the AI Review Logic
I created reviewer.py
to handle the review process:
<span>import</span> <span>openai</span><span>import</span> <span>os</span><span>from</span> <span>dotenv</span> <span>import</span> <span>load_dotenv</span><span>def</span> <span>review_code</span><span>(</span><span>code_snippet</span><span>):</span><span>load_dotenv</span><span>()</span><span>openai</span><span>.</span><span>api_key</span> <span>=</span> <span>os</span><span>.</span><span>getenv</span><span>(</span><span>"</span><span>OPENAI_API_KEY</span><span>"</span><span>)</span><span>prompt</span> <span>=</span> <span>f</span><span>"""</span><span> You are an expert software engineer. Review the following code and provide feedback on: - Code quality - Best practices - Potential bugs - Performance improvements - Security concerns Code: </span><span>{</span><span>code_snippet</span><span>}</span><span> </span><span>"""</span><span>response</span> <span>=</span> <span>openai</span><span>.</span><span>ChatCompletion</span><span>.</span><span>create</span><span>(</span><span>model</span><span>=</span><span>"</span><span>gpt-4</span><span>"</span><span>,</span><span>messages</span><span>=</span><span>[{</span><span>"</span><span>role</span><span>"</span><span>:</span> <span>"</span><span>user</span><span>"</span><span>,</span> <span>"</span><span>content</span><span>"</span><span>:</span> <span>prompt</span><span>}]</span><span>)</span><span>return</span> <span>response</span><span>[</span><span>"</span><span>choices</span><span>"</span><span>][</span><span>0</span><span>][</span><span>"</span><span>message</span><span>"</span><span>][</span><span>"</span><span>content</span><span>"</span><span>]</span><span>if</span> <span>__name__</span> <span>==</span> <span>"</span><span>__main__</span><span>"</span><span>:</span><span>print</span><span>(</span><span>"</span><span>Paste your code below (Press Enter twice to submit):</span><span>"</span><span>)</span><span>user_code</span> <span>=</span> <span>[]</span><span>while</span> <span>True</span><span>:</span><span>try</span><span>:</span><span>line</span> <span>=</span> <span>input</span><span>()</span><span>if</span> <span>line</span> <span>==</span> <span>""</span><span>:</span><span>break</span><span>user_code</span><span>.</span><span>append</span><span>(</span><span>line</span><span>)</span><span>except</span> <span>EOFError</span><span>:</span><span>break</span><span>code_snippet</span> <span>=</span> <span>"</span><span>\n</span><span>"</span><span>.</span><span>join</span><span>(</span><span>user_code</span><span>)</span><span>print</span><span>(</span><span>"</span><span>\n</span><span>Reviewing your code...</span><span>\n</span><span>"</span><span>)</span><span>review</span> <span>=</span> <span>review_code</span><span>(</span><span>code_snippet</span><span>)</span><span>print</span><span>(</span><span>review</span><span>)</span><span>import</span> <span>openai</span> <span>import</span> <span>os</span> <span>from</span> <span>dotenv</span> <span>import</span> <span>load_dotenv</span> <span>def</span> <span>review_code</span><span>(</span><span>code_snippet</span><span>):</span> <span>load_dotenv</span><span>()</span> <span>openai</span><span>.</span><span>api_key</span> <span>=</span> <span>os</span><span>.</span><span>getenv</span><span>(</span><span>"</span><span>OPENAI_API_KEY</span><span>"</span><span>)</span> <span>prompt</span> <span>=</span> <span>f</span><span>"""</span><span> You are an expert software engineer. Review the following code and provide feedback on: - Code quality - Best practices - Potential bugs - Performance improvements - Security concerns Code: </span><span>{</span><span>code_snippet</span><span>}</span><span> </span><span>"""</span> <span>response</span> <span>=</span> <span>openai</span><span>.</span><span>ChatCompletion</span><span>.</span><span>create</span><span>(</span> <span>model</span><span>=</span><span>"</span><span>gpt-4</span><span>"</span><span>,</span> <span>messages</span><span>=</span><span>[{</span><span>"</span><span>role</span><span>"</span><span>:</span> <span>"</span><span>user</span><span>"</span><span>,</span> <span>"</span><span>content</span><span>"</span><span>:</span> <span>prompt</span><span>}]</span> <span>)</span> <span>return</span> <span>response</span><span>[</span><span>"</span><span>choices</span><span>"</span><span>][</span><span>0</span><span>][</span><span>"</span><span>message</span><span>"</span><span>][</span><span>"</span><span>content</span><span>"</span><span>]</span> <span>if</span> <span>__name__</span> <span>==</span> <span>"</span><span>__main__</span><span>"</span><span>:</span> <span>print</span><span>(</span><span>"</span><span>Paste your code below (Press Enter twice to submit):</span><span>"</span><span>)</span> <span>user_code</span> <span>=</span> <span>[]</span> <span>while</span> <span>True</span><span>:</span> <span>try</span><span>:</span> <span>line</span> <span>=</span> <span>input</span><span>()</span> <span>if</span> <span>line</span> <span>==</span> <span>""</span><span>:</span> <span>break</span> <span>user_code</span><span>.</span><span>append</span><span>(</span><span>line</span><span>)</span> <span>except</span> <span>EOFError</span><span>:</span> <span>break</span> <span>code_snippet</span> <span>=</span> <span>"</span><span>\n</span><span>"</span><span>.</span><span>join</span><span>(</span><span>user_code</span><span>)</span> <span>print</span><span>(</span><span>"</span><span>\n</span><span>Reviewing your code...</span><span>\n</span><span>"</span><span>)</span> <span>review</span> <span>=</span> <span>review_code</span><span>(</span><span>code_snippet</span><span>)</span> <span>print</span><span>(</span><span>review</span><span>)</span>import openai import os from dotenv import load_dotenv def review_code(code_snippet): load_dotenv() openai.api_key = os.getenv("OPENAI_API_KEY") prompt = f""" You are an expert software engineer. Review the following code and provide feedback on: - Code quality - Best practices - Potential bugs - Performance improvements - Security concerns Code: {code_snippet} """ response = openai.ChatCompletion.create( model="gpt-4", messages=[{"role": "user", "content": prompt}] ) return response["choices"][0]["message"]["content"] if __name__ == "__main__": print("Paste your code below (Press Enter twice to submit):") user_code = [] while True: try: line = input() if line == "": break user_code.append(line) except EOFError: break code_snippet = "\n".join(user_code) print("\nReviewing your code...\n") review = review_code(code_snippet) print(review)
Enter fullscreen mode Exit fullscreen mode
Step 3: Running the Code Reviewer
To run the script, simply execute:
python reviewer.pypython reviewer.pypython reviewer.py
Enter fullscreen mode Exit fullscreen mode
Then, paste your code into the terminal and press Enter twice to get a review.
That’s how I built my AI-powered code reviewer!
Next Steps? Try running it on different code snippets and see how it performs.
Let me know if you use it!
PS: Want more AI tips, tricks, and in-depth tutorials? Stay tuned for the next issue, where I’ll share something more useful for you.
Got a favorite AI prompt? Or an AI tool you swear by?
Let me know (@Manas Moon)—I’m always excited to learn new ways to use AI.
Once again, you can subscribe to Codexai for more such cool AI prompts.
Manas | Founder @Codexai
Cheers xx!! 🥂
原文链接:How I built an AI-Powered Code Reviewer (and you can too).
暂无评论内容