Skip to content

Added new notebook Basic_Code_Review to examples #689

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
268 changes: 268 additions & 0 deletions examples/prompting/Basic_Code_Review.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,268 @@
{
Copy link
Contributor

@andycandy andycandy Apr 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better to move the imports closer to where they are first used, ideally placing them in the same cell where they're invoked


Reply via ReviewNB

Copy link
Contributor

@andycandy andycandy Apr 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line #14.    programmer_response = client.models.generate_content(

The string formatting in this block could be improved for better readability. Here’s an example of a cleaner format that aligns with common conventions:

programmer_response = client.models.generate_content(
  	model=MODEL_ID,
  	contents= """
   		Write code to take a list of integers, remove any duplicates,
   		filter out all numbers less than 30,
   		and return the remaining numbers sorted in descending order.
	""",
  	config=types.GenerateContentConfig(
    system_instruction=buggy_code_prompt
  ),
)

Reply via ReviewNB

"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "qVoQ1dMTW2Ak"
},
"source": [
"##### Copyright 2025 Google LLC."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"cellView": "form",
"id": "_KOwXrFSGiUv"
},
"outputs": [],
"source": [
"#@title Licensed under the Apache License, Version 2.0 (the \"License\");\n",
"# you may not use this file except in compliance with the License.\n",
"# You may obtain a copy of the License at\n",
"#\n",
"# https://www.apache.org/licenses/LICENSE-2.0\n",
"#\n",
"# Unless required by applicable law or agreed to in writing, software\n",
"# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
"# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
"# See the License for the specific language governing permissions and\n",
"# limitations under the License."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "tr7ER0zfZCfY"
},
"source": [
"# Gemini API: Basic Code Review"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "UQCYh9zIgoh2"
},
"source": [
"<a target=\"_blank\" href=\"https://colab.research.google.com/github/google-gemini/cookbook/blob/main/examples/prompting/Basic_Code_Review.ipynb\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" height=30/>"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "JwG4fNINmdhe"
},
"source": [
"Developed by [Shipra](https://github.com/Shi-pra-19)."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "31H1nJPKHwuk"
},
"source": [
"Gemini API can be a great tool to save you time during the development process by assisting with tasks such as code review, debugging, and optimization.\n",
"\n",
"Gemini API’s Python SDK can be used to perform various forms of code review tasks, including:\n",
"- Providing feedback on code quality, readability, or best practices\n",
"- Identifying weak areas in the code such as poor variable naming, inefficient loops, or security vulnerabilities\n",
"- Suggesting optimizations to improve performance and maintainability\n",
"\n",
"\n",
"Below is an example of using the Gemini model to enhance code quality through feedback."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"id": "JU-CGvNaKIrE"
},
"outputs": [],
"source": [
"%pip install -U -q \"google-genai>=1.7.0\""
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"id": "m-giCd-DKKeH"
},
"outputs": [],
"source": [
"from google import genai\n",
"from google.genai import types\n",
"from IPython.display import Markdown"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "4iFZqZT0K-L3"
},
"source": [
"## Configure your API key\n",
"\n",
"To run the following cell, your API key must be stored it in a Colab Secret named `GOOGLE_API_KEY`. If you don't already have an API key, or you're not sure how to create a Colab Secret, see [Authentication](https://github.com/google-gemini/cookbook/blob/main/quickstarts/Authentication.ipynb) for an example."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"id": "B1BWAbqvLCB1"
},
"outputs": [],
"source": [
"from google.colab import userdata\n",
"GOOGLE_API_KEY=userdata.get('GOOGLE_API_KEY')\n",
"\n",
"client = genai.Client(api_key=GOOGLE_API_KEY)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "qNmL9WxGNk9C"
},
"source": [
"## Example\n",
"\n",
"Start by defining some system instructions for this problem. For demonstration purposes, this use case involves prompting the model to generate a piece of code that contains a mix of common and critical mistakes."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"id": "nJuIQ-0bfcZP"
},
"outputs": [],
"source": [
"MODEL_ID=\"gemini-2.0-flash\" # @param [\"gemini-2.0-flash-lite\",\"gemini-2.0-flash\",\"gemini-2.5-pro-exp-03-25\"] {\"allow-input\":true, isTemplate: true}"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"id": "S0d95lm2NyAE"
},
"outputs": [
{
"data": {
"text/markdown": "```python\ndef process_data(list_of_nums):\n # Remove dups\n unique_nums = []\n for num in list_of_nums:\n if num not in unique_nums:\n unique_nums.append(num)\n\n # Filter nums < 30\n filtered_list = []\n i = 0\n while i < len(unique_nums):\n if unique_nums[i] >= 30:\n filtered_list.append(unique_nums[i])\n i += 1\n\n # Sort decending\n n = len(filtered_list)\n for i in range(n):\n for j in range(0, n-i-1):\n if filtered_list[j] < filtered_list[j+1]:\n filtered_list[j], filtered_list[j+1] = filtered_list[j+1], filtered_list[j]\n return filtered_list\n```",
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"buggy_code_prompt = \"\"\"\n",
" You are a beginner programmer.\n",
" You are tasked with writing code that include common errors:\n",
"\n",
" 1. Logical flaws\n",
" 2. Inefficient looping constructs\n",
" 3. Inconsistent formatting.\n",
" 4. Poor naming conventions\n",
"\n",
" Only output the code—no explanations.\n",
" If the task doesn’t specify a programming language, default to Python.\n",
"\"\"\"\n",
"\n",
"programmer_response = client.models.generate_content(\n",
" model=MODEL_ID,\n",
" contents= \"\"\"\n",
" Write code to take a list of integers, remove any duplicates,\n",
" filter out all numbers less than 30,\n",
" and return the remaining numbers sorted in descending order.\n",
"\"\"\",\n",
" config=types.GenerateContentConfig(\n",
" system_instruction=buggy_code_prompt\n",
" ),\n",
")\n",
"\n",
"code = programmer_response.text\n",
"Markdown(code)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"id": "N4W5TGtfSugH"
},
"outputs": [
{
"data": {
"text/markdown": "Okay, I will review the provided Python code, `process_data`, offering detailed feedback and a corrected version.\n\n**Review of the Original Code:**\n\nThe code aims to remove duplicates from a list of numbers, filter out numbers less than 30, and then sort the remaining numbers in descending order. Here's a breakdown of the issues:\n\n1. **Logic Errors:**\n\n * The code filters for numbers *greater than or equal to* 30, but the prompt asks for filtering *less than* 30. This is a logical error that inverts the intended filtering behavior.\n\n2. **Code Readability and Naming Conventions:**\n\n * **Variable Names:** While `list_of_nums` is descriptive, names like `unique_nums` and `filtered_list` are okay but could be slightly more informative (e.g., `unique_numbers`, `numbers_above_30`).\n * **Comments:** The comments are helpful in explaining the steps, but could be more concise.\n * **Whitespace:** Consistent use of whitespace makes the code more readable. The original code is reasonably good in this regard.\n\n3. **Code Efficiency and Best Practices:**\n\n * **Duplicate Removal:** The duplicate removal using `if num not in unique_nums:` is inefficient for large lists. Using a `set` is significantly faster for checking membership.\n * **Filtering:** Using a list comprehension for filtering is more concise and often faster than a `while` loop.\n * **Sorting:** The bubble sort algorithm used is very inefficient (O(n^2)) for larger lists. Python's built-in `sorted()` function (or the `list.sort()` method) uses a much more efficient sorting algorithm (Timsort, which is O(n log n)).\n * **In-place Modification:** The original code creates several new lists. Depending on the use case and memory constraints, it might be more efficient to modify the list in-place where possible (though this can sometimes make the code harder to reason about).\n\n**Corrected Version of the Code:**\n\n```python\ndef process_data(numbers):\n \"\"\"\n Processes a list of numbers by removing duplicates,\n filtering out numbers less than 30, and sorting the\n remaining numbers in descending order.\n\n Args:\n numbers: A list of numbers.\n\n Returns:\n A new list containing the processed numbers.\n \"\"\"\n\n # Remove duplicates using a set (more efficient)\n unique_numbers = list(set(numbers))\n\n # Filter numbers greater than or equal to 30 using list comprehension\n filtered_numbers = [num for num in unique_numbers if num >= 30] # Corrected filtering condition\n\n # Sort in descending order using sorted() with reverse=True\n sorted_numbers = sorted(filtered_numbers, reverse=True)\n\n return sorted_numbers\n```\n\n**Explanation of Changes and Improvements:**\n\n1. **Docstring:** Added a docstring to explain what the function does, its arguments, and what it returns. This significantly improves readability and maintainability.\n2. **Variable Naming:** Renamed `list_of_nums` to `numbers` for brevity. Added `_numbers` suffix to other variables.\n3. **Duplicate Removal:** Replaced the `for` loop and `if num not in unique_nums:` with `unique_numbers = list(set(numbers))`. This leverages the fact that `set` objects in Python only store unique elements. Converting back to a list maintains the desired output type. This is much more efficient, especially for large lists, as set membership checking is O(1) on average, compared to O(n) for lists.\n4. **Filtering:** Replaced the `while` loop with a list comprehension: `filtered_numbers = [num for num in unique_numbers if num >= 30]`. This is more concise and readable, and often more efficient. **Crucially, the filtering condition was corrected to `num >= 30` to match the intended logic (keeping numbers greater than or equal to 30).**\n5. **Sorting:** Replaced the bubble sort implementation with `sorted_numbers = sorted(filtered_numbers, reverse=True)`. This uses Python's built-in `sorted()` function, which is highly optimized (Timsort) and much more efficient than bubble sort. The `reverse=True` argument sorts the numbers in descending order.\n6. **Clarity and Readability:** Improved comments and whitespace for better readability.\n\nThese changes result in code that is more efficient, readable, and maintainable, while also correcting the logical error in the filtering process. Using built-in functions like `set()` and `sorted()` is almost always preferable to implementing these algorithms from scratch, unless there's a very specific performance requirement that necessitates a custom implementation.\n",
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"review_prompt = \"\"\"\n",
" As a senior programmer, you are tasked with reviewing beginner-level code.\n",
" Please follow these instructions for the evaluation:\n",
"\n",
" 1. Provide detailed feedback on the code, focusing on:\n",
" - Logic errors\n",
" - Code readability and naming conventions\n",
" - Code efficiency and best practices\n",
"\n",
" 2. Write a corrected version of the code, addressing the identified issues.\n",
"\n",
" 3. Clearly point out the changes you made and explain why they improve the original code.\n",
"\"\"\"\n",
"\n",
"review = client.models.generate_content(\n",
" model=MODEL_ID,\n",
" contents=code,\n",
" config=types.GenerateContentConfig(\n",
" system_instruction=review_prompt\n",
" ),\n",
")\n",
"\n",
"\n",
"Markdown(review.text)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "nTc3APHpWGys"
},
"source": [
"## Next steps\n",
"\n",
"Be sure to explore other examples of prompting in the repository. Try writing prompts for reviewing your own code as well using the example in this notebook."
]
}
],
"metadata": {
"colab": {
"name": "Basic_Code_Review.ipynb",
"toc_visible": true
},
"kernelspec": {
"display_name": "Python 3",
"name": "python3"
}
},
"nbformat": 4,
"nbformat_minor": 0
}