Creating Worlds, One Line at a Time: The Power of Duet AI-Generative AI for Developers

Sonia Khasanah Amalia
8 min readMar 9, 2024

--

In a world where creativity is often stifled by routine and predictability, a new force is emerging that promises to revolutionize the way we approach problem-solving, content creation, and even art. This force is generative AI, a type of artificial intelligence technology that can produce various types of content, including text, imagery, audio, and synthetic data.

Generative AI is built on a vertically integrated technology stack

In this section, we’ll talk about Duet AI.

What is Duet AI?

Duet AI is a generative AI solution developed by Google that integrates conversational intelligence into the Google Workspace and Google Cloud portfolio. It is designed to act as a source of inspiration, a coach, and a productivity booster.

Duet AI for developers helps you to get more done faster:

  1. AI-assisted development & operations. It can helps you to build, deploy, and operate faster and more efficiently.
  2. Enterprise security, privacy & IP protection. Helps protect your environment, data and IP with built-in security features.
  3. Powered by Google AI foundation models. Leverage state-of-the-art models like Gemini.

Duet AI Features

Now, let’s take a deep dive into how to use Duet AI, an AI-powered collaborator in Google Cloud. We’ll get familiar with how you can use Duet AI Chat and inline code assistance to generate code, understand code and more.

Enable Duet AI in the Google Cloud Project

  • Firstly, we must enable Duet AI API in our Google Cloud Project.
  • Duet AI chat window will open up on the right side of the console. Click on the Enable button as shown below. If you do not see the Enable button and instead see a Chat interface, it is likely that you have already enabled Duet AI for the project and you can directly go to the next step.

Enable Duet AI in Cloud Shell IDE

We will be using Cloud Shell IDE for the rest of the codelab. We need to enable and configure Duet AI in the Cloud Shell IDE and the steps are given below.

  • Activate cloud shell.
  • Once cloud shell activated, click Open Editor.
  • Then click Try the new Editor
  • Click on the Cloud Code — Sign in button in the bottom status bar as shown. Authorize the plugin as instructed. If you see “Cloud Code — no project” in the status bar, select that and then select the specific Google Cloud Project from the list of projects that you plan to work with.
  • (Optional). If you do not see the Duet AI in the status bar in the right bottom, you will need to enable Duet AI in Cloud Code. Before you do that ensure that Duet AI is enabled in the IDE by going to the Cloud Code Extension → Settings and then enter the text Duet AI: Enable as shown below. Ensure that the checkbox is selected. You should reload your IDE. This enables Duet AI in Cloud Code, and the Duet AI status bar will appear in your IDE.
  • Click on the Duet AI button in the bottom right corner as shown and select the correct Google Cloud project for which we had enabled the Cloud AI Companion API.
  • Once you select the your Google Cloud project, ensure that you are able to see that in the Cloud Code status message in the status bar and that you also have Duet AI enabled on the right, in the status bar as shown below.

Duet AI is ready to use!

Create the Sample Application

We will create a sample application (a Python Flask application) that we will use to explore Duet AI’s features by following these steps.

  • Click on the Google Cloud project name in the status bar below.
  • A list of options will appear. Click on New Application from the list below.
  • Select Cloud Run application.
  • Select the Python (Flask): Cloud Run application template.
  • Save the new application in your preferred location.
  • A notification confirms that your application was created, and a new window opens with your application loaded as shown below. A README.md file is opened. You can close that view for now.

Chat with Duet AI

We’re going to start by learning how to chat with Duet AI. Duet AI is available as a chat assistant within the Cloud Shell IDE as part of Cloud Code extension in VS Code.

Click on the Duet AI button in the left navigation bar.

Let us use the Duet AI pane to enter a prompt and view the response from Duet AI.

Prompts are questions or statements that describe the help that you need. Prompts can include context from existing code that Google Cloud analyzes to provide more useful or complete responses.

Code Explanation

To help you be more productive while minimizing context switching, Duet AI provides AI-powered smart actions directly in your code editor. When you select your code in your code editor, you can view and select from a list of actions relevant to your context.

Keep app.py as the currently opened file in the IDE. Go to the Duet AI chat pane in the left navigation bar in VS Code and enter the prompt Explain this file. This will consider the whole file and explain it to you.

You can also select any part of the file and then use the Duet AI chat pane on the left, enter the prompt Explain this. This will explain only the selected part of the file. This is similar to you clicking the bulb

and then clicking Explain This.

You can also select a part of the text and click on the Duet AI: Smart Actions from the top right in the IDE as shown below to get a list of actions that can be performed on the selected text. One of these options will be the Explain this action.

Generate Code with Prompts

This section shows you how to use Duet AI to generate code from comments.

  • Create a new file named utils.py.
  • In your new code file, on a new line, enter # Function to print a list of blobs in a Google Cloud Storage bucket, and then press Enter (for Windows and Linux) or Return (for MacOS). Duet may try to generate code as you type but don't accept it. We will specifically trigger the code generation in the next step.
  • To trigger code generation, press Control + Enter (for Windows and Linux) or Control + Return (for MacOS). Next to your prompt text in the Python file, Duet AI generates the code in the form of ghost text. It will provide you one or more suggestions for code with a toolbar, through which you can navigate from one code suggestion to another (in case there are multiple suggestions). You can even accept any of the code suggestions via the toolbar. In case you cannot see the toolbar or it disappears, hover over the generated code for it to appear again.
  • (Optional) To accept the generated code, press Tab. In case there are multiple suggestions, you can press Tab multiple times to navigate from one suggested code snippet to the next.

This will insert the code that you accepted in the utils.py file. A sample screenshot is shown below. The code that is provided to you might differ from the one shown below.

Notice the squiggly line around the code. This is Code citation in action. If you hover over any of the lines, it will display the code attribution and other relevant information around licensing. An example is shown below.

You can then opt to ignore the citation warning by clicking on Quick Fix …. This will display the suggest Quick Fix, a sample of which is shown below.

You can choose the Ignore Citation Warning if you are ok with accepting the code license.

Get Inline Code Suggestions while You Code

  • Open the utils.py file as the current file in the Cloud Shell IDE.
  • One of the things that we can do is to provide the correct import statements, if they are not there in the generated code.
  • Start typing the word import and you should see Duet AI suggesting code in the form of ghost text. To accept the code suggestion from Duet AI, press Tab. Otherwise, to ignore the suggestion, press Esc or continue writing your code. In the sample screen below, it suggests importing the storage class since it is being used in the code. Press Tab to accept the code suggestion. It is also suggested import os statement, but since it is not needed, we can remove it.
  • Go to the end of the utils.py file and add the line to invoke the function, if it is not provided. In the case here, it is the list_blobs function. You can just start typing the function name and code complete will help you finish the statement.
  • You will need to provide the bucket_name and you can use the following public bucket name : codelab-bucket.The sample invocation for the function is shown below. Please note that you might see minor differences in terms of function names, etc.

Congratulations!

We’ve successfully worked with Duet AI on a sample project to understand how it can assist with Code Generation in Google Cloud Project.

--

--