How can Duet AI help developers increase their speed and efficiency? A step-by-step guide on how to set up Duet AI for a developer environment
You’ll use Duet AI, an AI-powered collaborator in Google Cloud. You’ll get familiar with how you can use Duet AI Chat and inline code assistance to generate code, understand code and more.
What you’ll do…
- You’ll use Cloud Shell IDE to download an existing code template for a Web application.
- You’ll use Duet AI Chat in Cloud Shell IDE to ask general questions about Google Cloud.
- You’ll use Duet AI Inline code assistance in Cloud Shell IDE to generate, summarize and complete code.
What you’ll learn…
- How to use Duet AI for several developer tasks like code generation, code completion and code summarization.
- How to use Duet AI to learn about Google Cloud.
What you’ll need…
- Chrome web browser
- A Gmail account
- A Cloud Project with billing enabled
- Duet AI enabled for your Cloud Project
This is targeted to developers of all levels, including beginners. Although the sample application is in Python language, you don’t need to be familiar with Python programming in order to understand what’s going on. Our focus will be on getting familiar with the capabilities of Duet AI for developers.
This section covers everything you need to do to get started with this lab.
Enable Duet AI in the Google Cloud Project
We will now enable Duet AI API in our Google Cloud Project. Follow the steps given below:
- Visit https://console.cloud.google.com and ensure that you have selected the Google Cloud Project that you plan to work with for this lab. Click on the Duet AI icon that you see in the top right.
- 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.
- Once it is enabled, you can test out Duet AI by asking it a query or two. A few sample queries are shown but you can try something like What is Cloud Run?
Duet AI will respond with the answer to your question. You can click on the
icon on the top right corner to close the Duet AI chat window.
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:
- Launch Cloud Shell via the icon shown below. It may take a minute or two to start up the Cloud Shell instance.
- Click on Editor or Open Editor button (as the case might be) and wait till the Cloud Shell IDE appears. If you see the Try the new editor button, click that.
- 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!
3. Create the Sample application
We will create a sample application (a Python Flask application) that we will use to explore Duet AI’s features.
Follow 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.
4. 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. You can bring it up by clicking on the Duet AI button in the left navigation bar. Look for the Duet AI icon
in the left navigation toolbar and click on that.
This will bring up the Duet AI pane inside Cloud Shell IDE and you can chat with Duet AI to get help on Google Cloud.
Let us use the Duet AI pane to enter a prompt and view the response from Duet AI. Enter the following prompt:
What is Cloud Run?
Duet AI should respond with the details about Cloud Run. 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. For more information on writing prompts to generate good responses, see Write better prompts for Duet AI.
Try out the following sample prompts or any of your own to ask questions about Google Cloud:
What is the difference between Cloud Run and Cloud Functions?
What services are available on Google Cloud to run containerized workloads?
What are the best practices to optimize costs while working with Google Cloud Storage?
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.
You can use Duet AI to help you understand parts of the application code. Return to the application that we had created in a previous step and you can use Duet AI to explain the files and code that are available in your IDE.
- If the files are not visible, in the IDE activity bar, click Explorer ().
- In the Explorer pane, select Dockerfile.
- You can select any portion of the file contents, click the bulb (
- ), and then click Explain this. For example, select the line that begins with ENTRYPOINT, click the bulb (
- ), and then click Explain this. Duet AI generates a natural-language explanation about the specific section of the Dockerfile that you need more information on. Duet AI responds with details about the ENTRYPOINT instruction. You learn that, with this instruction, Docker will run the app.py file when the container launches.
- To view the contents of the app.py file, in the activity bar, click Explorer (), and then select app.py.
- In the hello() function definition, feel free to select the lines that you would like to get more information on. Then click the bulb (), then click Explain this. In our example, we selected the following two lines of code and then triggered the Explain this action.
- Duet AI responds with a detailed explanation of these two Cloud Run environment variables (K_SERVICE, K_REVISION) and how they are used in the application code. Do note that Duet AI with its knowledge of Google Cloud and services like Cloud Run has not only explained the Python code but also given the context of what those variables mean within Cloud Run.
Try out the following additional ways to get a code explanation:
- 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:
This section shows you how to use Duet AI to generate code from comments.
Let’s try out these features. Follow the steps given below:
- 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.
(Optional) Here are a few other suggestions to try out for code generation:
- You can also use the Duet AI chat feature to ask the same prompt and get the response in the chat window. The Chat interface then displays options to copy the code or insert into the current file.
- Try out your own prompts for sample code that you would like. A few suggestions are given below (assuming that you are in one of the python files): or choose from some of the prompts given below:
- Write Python code to publish a message to Google Cloud Pub/Sub
- Write Python code to initialize a BigQuery client
- Write Python code to connect my application to Cloud SQL
7. Get inline code 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, we will modify the utils.py
file that we created in the previous section :
- 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, pressTab
. Otherwise, to ignore the suggestion, pressEsc
or continue writing your code. In the sample screen below, it suggests importing thestorage
class since it is being used in the code. PressTab
to accept the code suggestion. It is also suggestedimport 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 thelist_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 :
duetai-tour-bucket
.The sample invocation for the function is shown below. Please note that you might see minor differences in terms of function names, etc.
(Optional) Run the sample code
Since we wrote code to list down the blobs in a Google Cloud Storage bucket, we will need to install the google-cloud-storage
Python library.
Go to the requirements.txt
file and add a new line with google-cloud-storage
dependency as shown below. A sample requirements.txt
file is shown below.
Flask==2.3.3
requests==2.31.0
debugpy # Required for debugging.
google-cloud-storage
Save the requirements.txt
file.
To see the code in action, we will need to launch a new Terminal session from Cloud Shell IDE via the menu icon in the top left. Click on the menu icon and then Terminal → New Terminal as shown below:
In the Terminal session, install the Python dependencies via the following command:
pip install -r requirements.txt
Finally, you can run the utils.py
file via the following command:
python utils.py
This should display the blobs that are present in the public Google Cloud Storage bucket. You should ideally see 2 blobs listed in the output (file1.txt
and file2.txt
).
Troubleshooting
In case you receive an “access” error while displaying the objects in the above public bucket, you can opt to create your own Google Cloud Storage bucket and use that instead of duetai-tour-bucket
. The next section shows you how to do that.
(Optional) Create your own Cloud Storage bucket
Let us take the help of Duet AI to provide us commands to create a bucket, make it publicly accessible and upload sample objects into the bucket.
Start with the following prompt: “What is the gcloud command to create a public bucket in Google Cloud Storage?”
This should provide you with a gcloud command to create a bucket. The exact command is shown below:
gcloud storage buckets create gs://<BUCKET_NAME>
Select a unique name for the <BUCKET_NAME>
and execute the command in the terminal.
Now that we have created a bucket, we would like the bucket to be publicly accessible. Let’s ask Duet AI for the command to do that using the following prompt: “How do I make a google cloud storage bucket publicly accessible?”
This should provide you with a gcloud command to make it publicly accessible. The response should be something like this:
gcloud storage buckets add-iam-policy-binding gs://<BUCKET_NAME> --member=allUsers --role=roles/storage.objectViewer
Replace the <BUCKET_NAME>
with the bucket name that you used earlier and execute the command.
Finally, we would like to upload a few sample files to the bucket. Execute the following commands in the terminal (Remember to substitute the <BUCKET_NAME>
variable with your bucket name.
touch f1.txt
touch f2.txt
gcloud storage cp f1.txt gs://<BUCKET_NAME>
gcloud storage cp f2.txt gs://<BUCKET_NAME>
You can now execute the utils.py
file by using your <BUCKET_NAME> instead of the duetai-tour-bucket
.
Reference docs…
- Duet AI Product home page
- Duet AI Documentation
- Duet AI Use cases
- https://codelabs.developers.google.com/codelabs/cloud-developer-duetai?hl=en&continue=https%3A%2F%2Fcodelabs.developers.google.com%2Fduetai-roadshow#7
About Me
As businesses move towards cloud-based solutions, I provide my expertise to support them in their journey. With over 15 years of experience in the industry, I am currently working as a Google Cloud Principal Architect. My specialization is in assisting customers to build highly scalable and efficient solutions on Google Cloud Platform. I am well-versed in infrastructure and zero-trust security, Google Cloud networking, and cloud infrastructure building using Terraform. I hold several certifications such as Google Cloud Certified, HashiCorp Certified, Microsoft Azure Certified, and Amazon AWS Certified. My certification in Google Cloud Certified — Cloud Digital Leader is particularly noteworthy.
Certificated :
- Google Cloud Certified — Cloud Digital Leader.
2. Google Cloud Certified — Associate Cloud Engineer.
3. Google Cloud Certified — Professional Cloud Architect.
4. Google Cloud Certified — Professional Data Engineer.
5. Google Cloud Certified — Professional Cloud Network Engineer.
6. Google Cloud Certified — Professional Cloud Developer Engineer.
7. Google Cloud Certified — Professional Cloud DevOps Engineer.
8. Google Cloud Certified — Professional Security Engineer.
9. Google Cloud Certified — Professional Database Engineer.
10. Google Cloud Certified — Professional Workspace Administrator.
11. Google Cloud Certified — Professional Machine Learning.
12. HashiCorp Certified — Terraform Associate
13. Microsoft Azure AZ-900 Certified
14. Amazon AWS-Practitioner Certified
I assist professionals and students in building their careers in the cloud. My responsibility is to provide easily understandable content related to Google Cloud and Google Workspace. If you find the content helpful, please like, share and subscribe for more amazing updates. If you require any guidance or assistance, feel free to connect with me.
YouTube:https://www.youtube.com/@growwithgooglecloud
Topmate :https://topmate.io/gcloud_biswanath_giri
Medium:https://bgiri-gcloud.medium.com/
Telegram: https://t.me/growwithgcp
Twitter: https://twitter.com/bgiri_gcloud
Instagram:https://www.instagram.com/google_cloud_trainer/
LinkedIn: https://www.linkedin.com/in/biswanathgirigcloudcertified/
Facebook:https://www.facebook.com/biswanath.giri
Linktree:https://linktr.ee/gcloud_biswanath_giri
and DM me,:) I am happy to help!!