Code with Duet AI assistance

This document describes how you can use Duet AI, an AI-powered collaborator in Google Cloud, to help you do the following in the Cloud Workstations base editor:

  • Provide guidance to help you solve problems with your code.

  • Generate code for your project.

  • Receive inline suggestions while you're coding.

Duet AI doesn't use your prompts or its responses as data to train its model. For more information, see How Duet AI in Google Cloud uses your data.

To help you comply with any license requirements for your code, Duet AI provides source citations when its suggestions directly quote at length from a specific source. To learn more about how and when Duet AI cites sources, see How Duet AI helps you generate code and cites sources.

This document is intended for developers of all skill levels. It assumes you have working knowledge of Cloud Workstations and are familiar with Google Cloud. If you prefer, you can also explore Duet AI in Cloud Code for VS Code, Cloud Code for IntelliJ, and Cloud Shell Editor.

Before you begin

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Make sure that billing is enabled for your Google Cloud project.

  4. Enable the Workstations API.

    Enable the API

  5. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  6. Make sure that billing is enabled for your Google Cloud project.

  7. Enable the Workstations API.

    Enable the API

  8. Ensure that Duet AI is set up for your Google Cloud user account and project.
  9. Make sure that you have the Cloud Workstations Admin IAM role on the project so that you can create workstation configurations.

    Go to IAM

  10. Set up your workstation if you haven't already, and make sure to select Base Editor (Code-OSS) in the configuration.

Connect to Google Cloud

If you're already connected to Google Cloud in your workstation, you can skip this section. Otherwise, follow these steps:

  1. In the Google Cloud console, launch your workstation.

    Launch workstation

  2. In the status bar, click Cloud Code - Sign In.

    Cloud Code - Sign In button in the status bar.

  3. When you're prompted to sign in, click Proceed to sign in.

  4. To launch the Google Cloud sign-in flow, press Control (for Windows and Linux) or Command (for MacOS) and click the URL in the console.

  5. To sign into your account, follow the prompts.

    When you're prompted to let the Google Cloud SDK access your Google Account and agree to the terms, click Allow.

    A new tab opens in your browser, displaying your verification code to sign into the Google Cloud CLI.

  6. Copy the verification code and paste it into your terminal where it says Enter authorization code, and then press Enter (for Windows and Linux) or Return (for MacOS).

    You're now connected to Google Cloud.

Enable Duet AI in Cloud Code

In this section, you enable Duet AI in Cloud Code. Before you proceed with these steps, make sure to follow the instructions in Set up Duet AI for a project, if you haven't already.

To enable Duet AI in Cloud Code for your workstation, follow these steps:

  1. In your workstation, select menu Menu, and then navigate to File > Preferences > Settings.

  2. On the User tab of the Settings dialog, navigate to Extensions > Google Cloud Code.

  3. Scroll until you find Duet AI: Enable, and then select the Enable Duet AI for developers checkbox.

  4. Reload your workstation.

    This enables Duet AI in Cloud Code, and the Duet AI status bar appears in your workstation.

    The Duet AI status bar is available.

  5. Select a Google Cloud project that has the Cloud AI Companion API enabled:

    1. In the Duet AI status bar, click Duet AI.

    2. In the Duet AI Quick Pick menu, select a Google Cloud project that has the Cloud AI Companion API enabled.

    Duet AI is ready to use.

    The Duet AI icon in status bar is set to normal.

    If you select a Google Cloud project without the Cloud AI Companion API enabled, you receive an error notification and are instructed to contact your administrator. For more information, see Set up Duet AI for a project.

To test the capabilities of Duet AI, open an application, or create a sample application in the next section.

Optional: Create a sample application

If you prefer to use an existing application to test the capabilities of Duet AI, you can skip this section. Otherwise, to create a sample application, follow these steps:

  1. In your workstation, open the command palette: either press Control+Shift+P (for Windows and Linux) or Command+Shift+P (for MacOS), and then run Cloud Code: New Application.

  2. Select Kubernetes application.

  3. Select the Python (Flask): Guestbook application template.

  4. Enter a name for your sample application, and then click OK to save it.

    A notification confirms that your application was created, and a new window opens with your application loaded.

Chat with Duet AI

In this section, you learn how to open the Duet AI pane and chat with Duet AI to get an explanation of your existing code.

To begin chatting with Duet AI, follow these steps:

  1. Create a new or use an existing code file. If you're using the Python (Flask) sample, you can perform this task in your front.py file: navigate to Explorer > src > frontend and open the front.py file.

  2. In the activity bar of your workstation, click chat_spark Duet AI.

  3. In the Duet AI pane, enter the prompt Explain this code to me and click send Send.

    Duet AI uses the code in your code file as a reference to your prompt and responds with an explanation of your code.

    To refer to a specific block of code instead of all the code in the file, you can select the block in your code file and then prompt Duet AI.

Reset chat history

Duet AI uses the chat history for additional context when responding to your prompts.

If your chat history is no longer relevant to what you're trying to achieve, you can reset the chat history: in the Duet AI pane, click delete Reset Chat.

Generate code with prompts

The following sections show you how to use Duet AI to generate code with the example prompt # Function to create a Cloud Storage bucket inside a Python file. You can also select a part of your code and then prompt Duet AI for help through the chat feature, and receive and accept or reject code suggestions while you code.

Prompt Duet AI in a code file

  1. Create a new or use an existing code file. If you're using the Python (Flask) sample, you can do this in your front.py file: navigate to Explorer > src > frontend and open the front.py file.

  2. In your code file, on a new line, enter # Function to create a Cloud Storage bucket, and then press Enter (for Windows and Linux) or Return (for MacOS).

  3. To generate code, 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.

  4. Optional: To accept the generated code, press Tab.

Prompt Duet AI with selected code using chat

Duet AI can perform tasks or answer your questions based on the code that you select. To get generated code that's based on a prompt with selected code, follow these steps:

  1. In your workstation, open a file in your project that contains code, or use the same code file that you used in previous steps.

  2. In the activity bar, click chat_spark Duet AI to open the Duet AI pane.

  3. In your code file, select a block of code.

  4. In the Duet AI pane text field, enter a prompt for the selected code.

    For example, select a function in your code and enter the prompt Write a unit test for this function:

    Duet AI writes a unit test for a selected function.

    Duet AI uses your selected code as reference and responds to your prompt.

Get inline suggestions while you code

While you write code, Duet AI makes inline code suggestions that you can either accept or ignore. To try this feature, follow these steps:

  1. Create a new or use an existing code file. If you're using the Python (Flask) sample, you can do this in your front.py file: navigate to Explorer > src > frontend and open the front.py file.

  2. In your code file, on a new line, start writing a function. For example, if you're in a Python file, write def.

    Duet AI suggests code in the form of ghost text.

  3. To accept the code suggestion from Duet AI, press Tab. Otherwise, to ignore the suggestion, press Esc or continue writing your code.

Optional: Disable inline suggestions

If you prefer to disable inline suggestions in Duet AI, follow these steps:

  1. In your workstation, select menu Menu, and then navigate to File > Preferences > Settings.

  2. On the User tab of the Settings dialog, navigate to Extensions > Cloud Code.

  3. Scroll until you find the Cloudcode: Duet AI: Inline Suggestions: Enable Auto list, and then select Off.

    This turns off the inline suggestions. You can still press Control+Enter (for Windows and Linux) or Control+Return (for MacOS) to manually trigger inline suggestions.

Use smart actions

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.

To use smart actions in your code, follow these steps:

  1. In your code file, select a block of code.

  2. Next to the selected code block, click lightbulb More Actions.

  3. Select an action such as Generate unit tests.

    Duet AI generates a response that's based on the action you selected.

Test other example prompts

After reading through the Generate code with prompts section of this document, try some of the following example prompts.

Get an explanation of code

  1. In your code file, select a function that you want explained.
  2. In the Duet AI pane, enter the prompt Explain this code to me.

    Duet AI uses your selected code as a reference and responds with an explanation of the selected function.

Generate test plans

  1. In your code file, select the code that you want to add unit tests for.
  2. In the Duet AI pane, enter the prompt Write unit tests for my code.

Get help with debugging code

  1. In your code file, select the code that you want to debug.
  2. In the Duet AI pane, enter the prompt Help me debug my code.

Make your code more readable

  1. In your code file, select the code that you want to make more readable.
  2. In the Duet AI pane, enter the prompt Make my code more readable.

    If you prefer to focus on a specific part of your code, then select the preferred part of code before prompting Duet AI.

Known issues

The following sections outline the known issues of Duet AI in Cloud Code.

License recitation warnings don't persist across sessions

If license recitation warnings don't persist across sessions, refer to the persistent logs:

  1. Click menu Menu > View > Output.

  2. Select Duet AI - Citations.

Connectivity issues in the Duet AI output window

If you see a connection error or other connectivity problems in the Duet AI output window, try the following:

  • Configure your firewall to allow access to oauth2.googleapis.com and cloudaicompanion.googleapis.com.

  • Configure your firewall to allow communication over HTTP/2, which gRPC uses.

You can use the grpc-health-probe tool to test connectivity. A successful check results in the following output:

$ grpc-health-probe -addr cloudaicompanion.googleapis.com:443 -tls error: this server does not implement the grpc health protocol (grpc.health.v1.Health): GRPC target method can't be resolved

An unsuccessful check results in the following output:

timeout: failed to connect service "cloudaicompanion.googleapis.com:443" within 1s

To obtain more details, run the following before grpc-health-probe:

export GRPC_GO_LOG_SEVERITY_LEVEL=info

Duet AI and JetBrains Gateway

Duet AI does not run through JetBrains Gateway. As a result, Duet AI does not run on Cloud Workstations IDEs that require JetBrains Gateway to connect.

Leave feedback

To leave feedback of your experience, follow these steps:

  1. In the status bar, click Duet AI, and then in the Quick Pick menu, select Send feedback.

  2. In the form, fill out the Title and Comments fields.

  3. If you want to share your Skaffold or AI Companion logs, make sure that you select the Send Skaffold logs or Send AI Companion logs option.

  4. Click Submit Feedback.

What's next