In-Depth

Gemini Code Assist AI: How to Get Started, Compare to GitHub Copilot

Google's new AI-powered code assistant looks to be a hit early on, already surpassing 1 million downloads a week after its debut.

That debut last week came during the Google Cloud Next '24 event, when the company announced "Gemini for Google Cloud," along with the Gemini Code Assist application development component that can be used in the open-source-based Visual Studio Code editor other editors/IDEs.

Gemini + Google Cloud Code
[Click on image for larger view.] Gemini + Google Cloud Code (source: Google).

"We are announcing Gemini Code Assist, the evolution of the Duet AI for Developers, which now uses our latest Gemini models," Google said. "It offers AI-powered assistance to help developers build applications with higher velocity and quality in popular code editors like VS Code and JetBrains. Gemini Code Assist is an enterprise-grade coding assistance solution that supports your private codebase wherever it lives — on-premises, Gitlab, Github, Bitbucket, or even across multiple repositories. Built with robust enterprise features, it enables organizations to adopt AI assistance at scale while meeting security, privacy, and compliance requirements."

The tool is free to use until July 11, requiring just a Google account. Here's how to get started with the tool, and how it compares to GitHub Copilot for some simple generative AI tasks.

Get Started with Gemini Code Assist
Upon installing the Gemini + Google Cloud Code tool from the VS Code marketplace, you are presented with a "Get Started" page that includes a walkthrough of the tool's features.

Welcome Page
[Click on image for larger view.] Welcome Page (source: Ramel).

The first step of the walkthrough is to sign in to your Google Cloud account.

Connect with Google Cloud
[Click on image for larger view.] Connect with Google Cloud (source: Ramel).

The next step is to Activate Gemini, which involves hooking up with a Google Cloud project with the Cloud AI Companion API enabled. If you don't have such a project, you can create one.

Activate Gemini
[Click on image for larger view.] Activate Gemini (source: Ramel).

After that setup work is done, you can start putting the tool through its paces, following documentation such as Use Gemini Code Assist in your IDE.

Comparing to GitHub Copilot
I tried a few simple tasks with Gemini Code Assist and GitHub Copilot to compare the two tools.

I asked each tool to create a Cloud Storage bucket with Python code, using this prompt: "generate python code to create a cloud storage bucket."

Here's Gemini's response:
import google.cloud.storage


def create_bucket(bucket_name):
    """Creates a new Cloud Storage bucket."""
    # bucket_name = "your-new-bucket-name"

    storage_client = google.cloud.storage.Client()
    bucket = storage_client.create_bucket(bucket_name)

    print(f"Bucket {bucket.name} created.")

Here's GitHub Copilot's response:

import google.cloud.storage

def create_bucket(bucket_name):
  """Creates a new Cloud Storage bucket."""
  # Create a client for Google Cloud Storage
  storage_client = google.cloud.storage.Client()
  
  # Use the client to create a new bucket with the provided name
  bucket = storage_client.create_bucket(bucket_name)
  
  # Print a success message with the name of the created bucket
  print(f"Bucket {bucket.name} created.")

Not being a Python coder, I can't say which is better, but I asked ChatGPT to weigh in on the comparison, and it said both were syntactically correct, adding: "Given that the functional part of both responses is the same, the choice between them comes down to personal preference regarding documentation style. If you value inline comments for clarity, Copilot's response might be seen as slightly better. However, both are effective and correctly implement the task."

I also asked each tool to explain this simple function to check if a number is prime:

def is_prime(num):
    """Check if a number is prime."""
    if num <= 1:
        return False
    for i in range(2, int(num**0.5) + 1):
        if num % i == 0:
            return False
    return True

Here's Gemini's response:

The is_prime function checks if a given number is prime. A prime number is a natural number greater than 1 that is not a product of two smaller natural numbers.

The function works by iterating over all numbers from 2 to the square root of the given number. If any of these numbers divides the given number without leaving a remainder, then the given number is not prime. Otherwise, the given number is prime.

Here is a step-by-step explanation of the function:

The function first checks if the given number is less than or equal to 1. If it is, then the function returns False because prime numbers are greater than 1.
The function then iterates over all numbers from 2 to the square root of the given number. This is because if a number is not prime, then it must have at least one factor that is less than or equal to its square root.
For each number in the range, the function checks if the given number is divisible by that number without leaving a remainder. If it is, then the function returns False because the given number is not prime.
If the function reaches the end of the range without finding any factors of the given number, then the function returns True because the given number is prime.
Here is an example of how the function works:

>>> is_prime(7)
True
>>> is_prime(10)
False
>>> is_prime(13)
True

Here's GitHub Copilot's response:

This Python function checks if a given number is a prime number. Here's a breakdown of how it works:

def is_prime(num):
    """Check if a number is prime."""
    # If the number is less than or equal to 1, it's not prime by definition
    if num <= 1:
        return False

    # Check divisibility for all numbers from 2 to the square root of the number
    # The square root is used for optimization: a larger factor of the number would be a multiple of smaller factor that has already been checked
    for i in range(2, int(num**0.5) + 1):
        # If the number is divisible by any number in this range, it's not prime
        if num % i == 0:
            return False

    # If no factors were found, the number is prime
    return True

My expert Python coder friend, ChatGPT, said:

Both responses are effective, but they cater to slightly different preferences or needs:

Gemini's response is better if the goal is to provide a thorough understanding of the problem domain (prime numbers) along with the function's logic. The inclusion of examples makes it excellent for educational purposes or for readers who appreciate seeing the function in action.

Copilot's response excels in situating the explanation within the code, which can be especially helpful for developers who prefer to see comments and explanations as they look at each line of code, making it a bit more practical for immediate coding use.

Finally, I asked each tool to create a simple to-do list with this prompt:

Create a simple To-Do List application in Python that allows a user to:

Add new tasks.
View all tasks.
Mark a task as completed.
Delete a task.
Use classes and functions to structure the code for clarity and reusability.

Here's Gemini's response:

class Task:
    def __init__(self, description):
        self.description = description
        self.completed = False

    def __str__(self):
        return f"{self.description} {'(completed)' if self.completed else ''}"


class ToDoList:
    def __init__(self):
        self.tasks = []

    def add_task(self, task):
        self.tasks.append(task)

    def view_tasks(self):
        for task in self.tasks:
            print(task)

    def mark_task_completed(self, task_description):
        for task in self.tasks:
            if task.description == task_description:
                task.completed = True

    def delete_task(self, task_description):
        self.tasks = [task for task in self.tasks if task.description != task_description]


if __name__ == "__main__":
    todo_list = ToDoList()

    while True:
        command = input("Enter a command (add, view, complete, delete, quit): ")

        if command == "add":
            description = input("Enter a task description: ")
            todo_list.add_task(Task(description))
        elif command == "view":
            todo_list.view_tasks()
        elif command == "complete":
            description = input("Enter the description of the task to mark as completed: ")
            todo_list.mark_task_completed(description)
        elif command == "delete":
            description = input("Enter the description of the task to delete: ")
            todo_list.delete_task(description)
        elif command == "quit":
            break
        else:
            print("Invalid command. Please enter one of the following: add, view, complete, delete, quit")  

Here's GitHub Copilot's response:

class TodoList:
  def __init__(self):
    self.tasks = []

  def add_task(self, task):
    self.tasks.append(task)

  def view_tasks(self):
    if not self.tasks:
      print("No tasks found.")
    else:
      for index, task in enumerate(self.tasks, start=1):
        print(f"{index}. {task}")

  def mark_task_completed(self, task_index):
    if task_index <1 or task_index > len(self.tasks):
      print("Invalid task index.")
    else:
      self.tasks[task_index - 1] += " (Completed)"

  def delete_task(self, task_index):
    if task_index <1 or task_index > len(self.tasks):
      print("Invalid task index.")
    else:
      del self.tasks[task_index - 1]


def main():
  todo_list = TodoList()

  while True:
    print("\n--- To-Do List ---")
    print("1. Add new task")
    print("2. View all tasks")
    print("3. Mark a task as completed")
    print("4. Delete a task")
    print("5. Exit")

    choice = input("Enter your choice: ")

    if choice == "1":
      task = input("Enter the task: ")
      todo_list.add_task(task)
      print("Task added successfully.")
    elif choice == "2":
      todo_list.view_tasks()
    elif choice == "3":
      task_index = int(input("Enter the task index: "))
      todo_list.mark_task_completed(task_index)
      print("Task marked as completed.")
    elif choice == "4":
      task_index = int(input("Enter the task index: "))
      todo_list.delete_task(task_index)
      print("Task deleted successfully.")
    elif choice == "5":
      print("Exiting...")
      break
    else:
      print("Invalid choice. Please try again.")


if __name__ == "__main__":
  main()

When asked which is better, the fence-riding, non-committal ChatGPT said:

If the priority is code clarity, reusability, and scalability, Gemini's response is better. The use of a Task class provides a clean and expandable structure, which is beneficial for more complex or evolving projects.

If ease of use and user interface are more critical, Copilot's approach could be preferred due to its menu-driven interaction model and simpler task handling through indices.

ChatGPT's overall conclusion is:

Gemini appears to be the better AI code assistant when the focus is on creating robust, scalable, and well-documented code. Its explanations and structured approach make it ideal for educational purposes or complex project development.

Copilot shines in scenarios requiring quick solutions that are easy to implement and understand, making it suitable for rapid prototyping and applications where user interaction is a priority.

So, it looks like Google's new tool hit the mark right off the bat, considering how much newer it is than GitHub Copilot, with many features still in preview. Pricing for Gemini Code Assist is basically the same as GitHub Copilot Business, about $19 per month.

Microsoft got a big jump in the AI code assistant space with GitHub Copilot, but Google's Gemini Code Assist looks to be a strong contender. When it matures, things will get really interesting.

Featured

Subscribe on YouTube