1. Before you begin
In this codelab, you learn how to build AI-powered web apps that provide compelling user experiences with Firebase Extensions.
Prerequisites
- Knowledge of Node.js and JavaScript
What you'll learn
- How to use AI-related Extensions to process language and video input.
- How to use Cloud Functions for Firebase to form a pipeline between Extensions.
- How to use JavaScript to access output produced by Extensions.
What you'll need
- A browser of your choice, such as Google Chrome
- A development environment with a code editor and terminal
- A Google Account for the creation and management of your Firebase project
2. Review web apps and their Firebase services
This section describes the web apps that you'll build in this codelab and which Firebase you'll use to build them.
Reviewly app
A T-shirt company is overwhelmed by long reviews about one of their T-shirts and unsure of its overall rating. The completed Reviewly web app summarizes each review, provides a star rating for each review, and uses each review to infer an overall rating for the product. Users can also expand each summarized review to see the original review.
Service | Reason for use |
Store the text of each review, which is then processed by an extension. | |
Deploy Security Rules to help secure access to your Firebase services. | |
Add mock reviews to the web app. | |
Install, configure, and trigger the Language Tasks with PaLM API extension to summarize each review added to Firestore |
Chatbot app
A school's teaching staff is overwhelmed by repetitive questions about general topics, so they want to automate responses. The completed Chatbot app provides students with a conversational chatbot that's powered by a large language model (LLM) and can answer questions about general topics. The chatbot has historical context, so its responses can factor in previous questions that students asked in the same conversation.
Service | Reason for use |
Use sign-in-with-Google to manage users. | |
Store the text of each conversation; messages from users are processed by an extension. | |
Deploy Security Rules to help secure access to your Firebase services. | |
Install, configure, and trigger the Chatbot with PaLM API extension to respond when a new message is added to Firestore | |
Use the Local Emulator Suite to locally run the app. | |
Use web frameworks with Hosting to serve the app. |
Video Hint app
A government department wants its videos to provide audio descriptions to improve accessibility, but they have hundreds of videos to annotate and need a streamlined approach. The completed Video Hint app is a prototype that the department will review to assess its effectiveness.
Service | Reason for use |
Use sign-in-with-Google to manage users. | |
Store the text of each video summary. | |
Store videos and JSON files with the video descriptions. | |
Deploy Security Rules to help secure access to your Firebase services. | |
Install, configure, and trigger various extensions (see list below). | |
Build a pipeline between extensions with Cloud Functions. | |
Use the Local Emulator Suite to locally run the app. | |
Use web frameworks with Hosting to serve the app. |
These are the extensions used in the Video Hint app:
- Label Videos with Cloud Video AI extension — extract labels from each video uploaded to Storage.
- Language Tasks with PaLM API extension — summarize the labels into a text description.
- Convert Text to Speech extension — create an audio version of the video's description.
3. Set up your development environment
Verify your Node.js version
- In your terminal, verify that you have Node.js v20.0.0 or higher installed:
node -v
- If you don't have Node.js v20.0.0 or higher, download and install it.
Download the repository
- If you have git installed, clone the codelab's GitHub repository:
git clone https://github.com/FirebaseExtended/ai-extensions-codelab.git
- If you don't have git installed, download the GitHub repository as a zip file.
Review the folder structure
On your local machine, find the cloned repository and review the folder structure. The following table contains the folders and their descriptions:
Folder | Description |
| The starter code for the Reviewly web app |
| The solution code for the Reviewly web app |
| The starter code for the Chatbot web app |
| The solution code for the Chatbot web app |
| The starter code for the Video Hint web app |
| The solution code for the Video Hint web app |
Each folder includes a readme.md
file that offers a quick start to run the respective web app, using streamlined instructions. However, if you're a first-time learner, you should complete this codelab because it contains the most comprehensive set of instructions.
If you're unsure of whether you correctly applied code as instructed throughout this codelab, you can find the solution code for the respective apps in the reviewly-end
, chatbot-end
, and video-hint-end
folders.
Install the Firebase CLI
Run the following command to verify that you have the Firebase CLI installed and that it's v12.5.4 or higher:
firebase --version
- If you have the Firebase CLI installed, but it's not v12.5.4 or higher, update it:
npm update -g firebase-tools
- If you don't have the Firebase CLI installed, install it:
npm install -g firebase-tools
If you're unable to install the Firebase CLI because of permission errors, see the npm documentation or use another installation option.
Log in to Firebase
- In your terminal, navigate to the
ai-extensions-codelab
folder and log in to Firebase:cd ai-extensions-codelab firebase login
- If your terminal says that you're already logged in to Firebase, proceed to the Set up your Firebase project section of this codelab.
- Depending on whether you want Firebase to collect data, enter
Y
orN
. - In your browser, select your Google account, and then click Allow.
4. Set up your Firebase project
In this section, you'll set up a Firebase project and associate a Firebase web app with it. You'll also enable the Firebase services used by the sample web apps.
Create a Firebase project
- In the Firebase console, click Create project.
- In the Enter your project name text box, enter
AI Extensions Codelab
(or a project name of your choice), and then click Continue. - For this codelab, you don't need Google Analytics, so toggle off the Enable Google Analytics for this project option.
- Click Create project.
- Wait for your project to provision, and then click Continue.
- In your Firebase project, go to Project Settings. Note your project ID because you need it later. This unique identifier is how your project is identified (for example, in the Firebase CLI).
Download a Firebase service account
Some of the web apps you'll build in this codelab use server side rendering with Next.js.
The Firebase Admin SDK for Node.js is used to ensure Security Rules are functional from server side code. To use APIs in Firebase Admin, you need to download a Firebase service account from the Firebase console.
- In the Firebase console, navigate to the Service Accounts page in your Project settings.
- Click Generate new private key > Generate Key.
- After the file has downloaded to your filesystem, get the full path to that file.
For example, if you downloaded the file to your Downloads folder, the full path might look like this:/Users/me/Downloads/my-project-id-firebase-adminsdk-123.json
- In your terminal, set the
GOOGLE_APPLICATION_CREDENTIALS
environment variable to the path of your downloaded private key. In a Unix environment, the command might look like this:export GOOGLE_APPLICATION_CREDENTIALS="/Users/me/Downloads/my-project-id-firebase-adminsdk-123.json"
- Keep this terminal open and use it for the remainder of this codelab, as your environment variable might be lost if you start a new terminal session.
If you open a new terminal session, you must rerun the previous command.
Upgrade your Firebase pricing plan
To use Cloud Functions and Firebase Extensions, your Firebase project needs to be on the Blaze pricing plan, which means it's associated with a Cloud Billing account.
- A Cloud Billing account requires a payment method, like a credit card.
- If you're new to Firebase and Google Cloud, check if you're eligible for a $300 credit and a Free Trial Cloud Billing account.
However, note that completion of this codelab shouldn't incur any actual charges.
To upgrade your project to the Blaze plan, follow these steps:
- In the Firebase console, select to upgrade your plan.
- In the dialog, select the Blaze plan, and then follow the on-screen instructions to associate your project with a Cloud Billing account.
If you needed to create a Cloud Billing account, you might need to navigate back to the upgrade flow in the Firebase console to complete the upgrade.
Set up Firebase services in the Firebase console
In this section, you'll provision and set up several Firebase services used by the web apps in this codelab. Note that not all of these services are used in each web app, but setting up all these services right now is a convenience for working through this codelab.
Set up Authentication
You'll use Authentication with both the Chatbot app and the Video Hint app. Remember, though, if you were building a real app, each app should have its own Firebase project.
- In the Firebase console, navigate to Authentication.
- Click Get started.
- In the Additional providers column, click Google > Enable.
- In the Public-facing name for project text box, enter a memorable name, such as
My AI Extensions Codelab
. - From the Support email for project drop-down, select your email address.
- Click Save.
Set up Cloud Firestore
You'll use Firestore with all three apps. Remember, though, if you were building a real app, each app should have its own Firebase project.
- In the Firebase console, navigate to Firestore.
- Click Create database > Start in test mode > Next.
Later in this codelab, you'll add Security Rules to secure your data. Do not distribute or expose an app publicly without adding Security Rules for your database. - Use the default location or select a location of your choice.
For a real app, you want to choose a location that's close to your users. Note that this location cannot be changed later, and it will also automatically be the location of your default Cloud Storage bucket (next step). - Click Done.
Set up Cloud Storage for Firebase
You'll use Cloud Storage with the Video Hint app and to try out the Convert Text to Speech extension (next step of the codelab).
- In the Firebase console, navigate to Storage.
- Click Get started > Start in test mode > Next.
Later in this codelab, you'll add Security Rules to secure your data. Do not distribute or expose an app publicly without adding Security Rules for your Storage bucket. - The location of your bucket should already be selected (due to setting up Firestore in the previous step).
- Click Done.
In the next sections of this codelab, you'll install extensions and modify the codebases of each sample app in this codelab to get three different web apps working.
5. Set up the "Language Tasks with PaLM API" extension for the Reviewly app
Install the Language Tasks with PaLM API extension
- Navigate to the Language Tasks with PaLM API extension.
- Click Install in the Firebase console.
- Select your Firebase project.
- In the Review APIs enabled and resources created section, click Enable next to any services that are suggested to you:
- Click Next > Next.
- In the Collection path text box, enter
bot
. - In the Prompt text box, enter
{{ input }}
. - In the Variable fields text box, enter
input
. - In the Response field text box, enter
text
. - From the Cloud Functions location drop-down, select Iowa (us-central1) or the location that you previously selected for Firestore and Cloud Storage.
- From the Language model drop-down, select text-bison-001.
- Leave all other values as their default.
- Click Install extension and wait for the extension to install.
Try out the Language Tasks with PaLM API extension
While the goal of this codelab is to interact with the Language Tasks with PaLM API extension through a web app, it's helpful to understand how the extension works by triggering the extension using the Firebase console. The extension triggers when a Cloud Firestore document is added to the bot
collection.
To see how the extension works by using the Firebase console, follow these steps:
- In the Firebase console, navigate to Firestore.
- In the
bot
collection, click Add document. - In the Document ID text box, click Auto-ID.
- In the Field text box, enter
input
. - In the Value text box, enter
Tell me about the moon
. - Click Save and wait a few seconds. Your document within the
bot
collection now includes a response to your query.
6. Set up the Reviewly app to use Firebase
To run the Reviewly app, you need to set up your app's code and the Firebase CLI to interact with your Firebase project.
Add Firebase services and configuration to your app's code
To use Firebase, your app's codebase needs the Firebase SDKs for the services you want to use and the Firebase configuration that tells those SDKs which Firebase project to use.
This codelab's sample app already includes all the necessary import and initialization code for the SDKs (see reviewly-start/js/reviews.js
), so you don't need to add these. However, the sample app only has placeholder values for the Firebase configuration (see reviewly-start/js/firebase-config.js
), so you need to register your app with your Firebase project to get the unique Firebase configuration values for your app.
- In the Firebase console, in your Firebase project, navigate to the Project overview, and then click Web.
- In the App nickname text box, enter a memorable app nickname, such as
My Reviewly app
. - Do not select the Also set up Firebase Hosting for this app checkbox. You'll do these steps later in the codelab.
- Click Register app.
- The console displays a code snippet for adding and initializing the Firebase SDK with an app-specific Firebase configuration object. Copy all the properties in the Firebase configuration object.
- In your code editor, open the
reviewly-start/js/firebase-config.js
file. - Replace the placeholder values with the values you just copied. It's ok if you have properties and values for Firebase services that you don't use in the Reviewly app.
- Save the file.
- Back in the Firebase console, click Continue to console.
Set up your terminal to run Firebase CLI commands against your Firebase project
- In your terminal, navigate to the
ai-extensions-codelab
folder that you downloaded earlier. - Navigate to the
reviewly-start
web app folder:cd reviewly-start
- Make the Firebase CLI run commands against a specific Firebase project:
firebase use YOUR_PROJECT_ID
Run and view the Reviewly web app
To run and view the web app, follow these steps:
- In your terminal, install dependencies and then run the web app:
npm install # Include the parentheses in the following command. (cd functions && npm install) npm run dev
- In your browser, navigate to the URL shown in your terminal. For example: http://localhost:8080.
The page should load, but you'll notice that various features are missing. We'll add these in the next steps of this codelab.
7. Add functionality to the Reviewly app
In the last step of this codelab, you ran the Reviewly app locally, but it didn't have much functionality and didn't yet use the installed extension. In this step of the codelab, you'll add this functionality and use the web app to trigger the extension.
Deploy Security Rules
This codelab's sample app contains sets of Security Rules for Firestore and for Cloud Storage for Firebase. After you deploy these Security Rules to your Firebase project, the data in your database and your bucket are better protected from misuse.
You can view these rules in the firestore.rules
and storage.rules
files.
- To deploy these Security Rules, run this command in your terminal:
firebase deploy --only firestore:rules,storage
- If you're asked:
"Cloud Storage for Firebase needs an IAM Role to use cross-service rules. Grant the new role?"
, select Yes.
Update the app's code to trigger the extension
In the Reviewly app, a new review added to Firestore triggers the extension to summarize the review.
- In your code editor, open the
functions/add-mock-reviews.js
file. - Replace the
reviewWithPrompt
variable with the following code, which prompts the language model for a shorter review.const reviewWithPrompt = `Here's a user supplied review. Give me a shorter summary of the review, and a rating out of 5, and a flag which indicates if there was a product defect. Product name: "Blue t-shirt with cat picture". Review: """${review}""" Craft your response as JSON with properties has_defect, summary and rating. Don't include any extra text.`;
- After the
reviewWithPrompt
variable, replace thereviewDocument
variable with the following code, which creates a review document so that it can be added to Firestore.const reviewDocument = { input: reviewWithPrompt, originalReview: review, timestamp: Timestamp.now(), }; getFirestore().collection(DB_COLLECTION_NAME).add(reviewDocument);
- Save the file.
- In the
js/reviews.js
file, after theInsert code below, to import your Firebase Callable Cloud Function
comment, import your Firebase HTTP callable function with thehttpsCallable
helper:const addMockReviews = httpsCallable(functions, "addMockReviews");
- After the
Insert code below, to invoke your Firebase Callable Cloud Function
comment, invoke your Firebase HTTP callable function:await addMockReviews();
- Save the file.
Deploy a function to add new reviews
The Reviewly web app uses a Cloud Function to add the reviews. But currently, the Cloud Function is not deployed.
To deploy your function with the Firebase CLI, follow these steps:
- In your terminal while still in the
reviewly-start
folder, pressControl+C
to stop the server. - Deploy your function:
firebase deploy --only functions
- If you see a
Permission denied while using the Eventarc Service Agent
or similar error, wait a few minutes, and then retry the command.
You just deployed your first custom function with Cloud Functions. The Firebase console offers a dashboard where you can see all the functions that you deploy to your Firebase project.
Run and view the Reviewly web app again (now with functionality)
To run and view the now functional web app, follow these steps:
- In your terminal, run the server again:
npm run dev
- In your browser, navigate to the URL shown in your terminal. For example: http://localhost:8080.
- In the app, click Add some mock reviews and wait a few seconds for a few long reviews to appear.
In the background, the Language Tasks with PaLM API extension reacts to the new document that represents the new review. The prompt you added earlier requests a shorter summary from the language model. - To view an entire review and the prompt used for the review, click one of the reviews, and then select Show PaLM prompt.
8. Set up the "Chatbot with PaLM API" extension for the Chatbot app
Install the Chatbot with PaLM API extension
- Navigate to the Chatbot with PaLM API extension.
- Click Install in Firebase console.
- Select your Firebase project.
- Click Next > Next > Next, until you reach the Configure extension section.
- In the Collection path text box, enter
users/{uid}/discussion/{discussionId}/messages
. - From the Cloud Functions location drop-down, select Iowa (us-central1) or the location that you previously selected for Firestore and Cloud Storage.
- From the Language model drop-down, select chat-bison.
- Leave all other values as their default.
- Click Install extension and wait for the extension to install.
Try out the Chatbot with PaLM API extension
While the goal of this codelab is to interact with the Chatbot with PaLM API extension through a web app, it's helpful to understand how the extension works by triggering the extension using the Firebase console. The extension triggers when a Cloud Firestore document is created in the users/{uid}/discussion/{discussionId}/messages
collection.
- In the Firebase console, navigate to Firestore.
- Click Start collection.
- In the Collection ID text box, enter
users
, and then click Next. - In the Document ID text box, click Auto-ID, and then click Save.
- In the Collection ID text box, enter
- In the
users
collection, click Start collection.- In the Collection ID text box, enter
discussion
, and then click Next. - In the Document ID text box, click Auto-ID, and then click Save.
- In the Collection ID text box, enter
- In the
discussion
collection, click Start collection.- In the Collection ID text box, enter
messages
, and then click Next. - In the Document ID text box, click Auto-ID.
- In the Field text box, enter
prompt
. - In the Value text box, enter
Tell me 5 random fruits
. - Click Save and wait a few seconds. The
messages
collection now includes a document that contains a response to your query.
- In the Collection ID text box, enter
- In the
messages
collection, click Add document.- In the Document ID text box, click Auto-ID.
- In the Field text box, enter
prompt
. - In the Value text box, enter
And now, vegetables
. - Click Save and wait a few seconds. The
messages
collection now includes a document that contains a response to your query.
9. Set up the Chatbot app to use Firebase
To run the Chatbot app, you need to set up your app's code and the Firebase CLI to interact with your Firebase project.
Add Firebase services and configuration to your app's code
To use Firebase, your app's codebase needs the Firebase SDKs for the services you want to use and the Firebase configuration that tells those SDKs which Firebase project to use.
This codelab's sample app already includes all the necessary import and initialization code for the SDKs (see chatbot-start/lib/firebase/firebase.js
), so you don't need to add these. However, the sample app only has placeholder values for the Firebase configuration (see chatbot-start/lib/firebase/firebase-config.js
), so you need to register your app with your Firebase project to get the unique Firebase configuration values for your app.
- In the Firebase console, in your Firebase project, navigate to the Project overview, and then click Web (or click Add app if you've already registered an app with the project).
- In the App nickname text box, enter a memorable app nickname, such as
My Chatbot app
. - Do not select the Also set up Firebase Hosting for this app checkbox. You'll do these steps later in the codelab.
- Click Register app.
- The console displays a code snippet for adding and initializing the Firebase SDK with an app-specific Firebase configuration object. Copy all the properties in the Firebase configuration object.
- In your code editor, open the
chatbot-start/lib/firebase/firebase-config.js
file. - Replace the placeholder values with the values you just copied. It's ok if you have properties and values for Firebase services that you don't use in the Chatbot app.
- Save the file.
- Back in the Firebase console, click Continue to console.
Set up your terminal to run Firebase CLI commands against your Firebase project
- In your terminal, press
Control+C
to stop the server from running the previous web app. - In your terminal, navigate to the
chatbot-start
web app folder:cd ../chatbot-start
- Make the Firebase CLI run commands against a specific Firebase project:
firebase use YOUR_PROJECT_ID
Set up your app's codebase to use framework-aware Firebase Hosting
This codelab uses web frameworks with Hosting (preview) with the Chatbot web app.
- In your terminal, enable web frameworks with Firebase Hosting:
firebase experiments:enable webframeworks
- Initialize Firebase Hosting:
firebase init hosting
- When prompted with
Detected an existing
Next.js
codebase in your current directory, should we use this?
, press Y. - When prompted with
In which region would you like to host server-side content, if applicable?
, select either the default region or the location that you previously selected for Firestore and Cloud Storage, and then pressEnter
(orreturn
on macOS). - When prompted with
Set up automatic builds and deploys with GitHub?
, pressN
.
Run and view the Chatbot web app
- In your terminal, install dependencies and then run the web app:
npm install firebase emulators:start --only hosting
- In your browser, navigate to the locally hosted Hosting URL. In most cases, it's http://localhost:5000/ or something similar.
The page should load, but you'll notice that various features are missing. We'll add these in the next steps of this codelab.
Troubleshoot running the web app
If you see the error in the web page that starts like this: Error: Firebase session cookie has incorrect...
, you need to delete all your cookies in your localhost environment. To do this, follow the instructions on delete cookies | DevTools Documentation..
10. Add functionality to the Chatbot app
In the last step of this codelab, you ran the Chatbot app locally, but it didn't have much functionality and didn't yet use the installed extension. In this step of the codelab, you'll add this functionality and use the web app to trigger the extension.
Deploy Security Rules
This codelab's sample app contains sets of Security Rules for Firestore and for Cloud Storage for Firebase. After you deploy these Security Rules to your Firebase project, the data in your database and your bucket are better protected from misuse.
You can view these rules in the firestore.rules
and storage.rules
files.
- To deploy these Security Rules, run this command in your terminal:
firebase deploy --only firestore:rules,storage
- If you're asked:
"Cloud Storage for Firebase needs an IAM Role to use cross-service rules. Grant the new role?"
, select Yes.
Update the code to add messages to Cloud Firestore
In the Chatbot app, a new message from a user is added to Firestore and that triggers the extension to generate a response.
- In your code editor, open the
lib/firebase/firestore.js
file. - Near the end of the file, find the
addNewMessage
function, which handles the addition of new messages.
The function already takes in the following object properties:Parameter
Description
userId
The ID of the logged-in user
discussionId
The discussion ID to which the message was added
message
The message text content
db
A Firestore database instance
- In the body of the
addNewMessage
function after the// Insert your code below ⬇️
comment, add the following code:await addDoc( collection( db, "users", userId, "discussion", discussionId, "messages" ), { prompt: message, createTime: serverTimestamp(), } ); await updateDoc(doc(db, "users", userId, "discussion", discussionId), { latestMessage: message, updatedTime: serverTimestamp(), });
Update the code to construct a query to get messages
- Still in the
lib/firebase/firestore.js
file, locate thegetMessagesQuery
function, which needs to return a Cloud Firestore query that locates messages stored in theusers/{uid}/discussion/{discussionId}/messages
collection path. - Replace the entire
getMessagesQuery
function with the following code:function getMessagesQuery(db, userId, discussionId) { if (!userId || !discussionId) { return null; } const messagesRef = collection( db, "users", userId, "discussion", discussionId, "messages" ); return query(messagesRef, orderBy("createTime", "asc")); }
Update the code to handle Cloud Firestore message documents
- Still in the
lib/firebase/firestore.js
file, locate thehandleMessageDoc
function, which receives a Cloud Firestore document that represents a single message.
This function needs to format and structure the data in a way that makes sense for the UI of the Chatbot app. - Replace the entire
handleMessageDoc
function with the following code:function handleMessageDoc(doc) { const data = doc.data(); const item = { prompt: data.prompt, response: data.response, id: doc.id, createTime: formatDate(data.createTime.toDate()), }; if (data?.status?.completeTime) { item.completeTime = formatDate(data.status.completeTime.toDate()); } return item; }
- Save the file.
Run and view the Chatbot web app again (now with functionality)
To run and view the now functional web app, follow these steps:
- In your browser, return to the tab with the Chatbot web app and reload the page.
- Click Sign in with Google.
- If necessary, select your Google account.
- After you have signed in, reload the page.
- In the Enter your message text box, enter a message, such as
Tell me about space
. - Click Send and wait a few seconds for the Chatbot web app to respond.
Another benefit of the Chatbot with PaLM API extension is its conversation history.
To see an example of its ability to converse with historical context, follow these steps:
- In the Enter your message text box, ask a question, such as
What are five random fruits?
. - In the Enter your message text box, ask a follow-up question that's related to the previous question, such as
And what about vegetables?
.
The Chatbot web app responds with historical knowledge. Even though your last question didn't specify five random vegetables, the Chatbot with PaLM API extension understands follow-up questions.
11. Set up the "Convert Text to Speech" extension for the Video Hint app
Install the Convert Text to Speech extension
- Navigate to the Convert Text to Speech extension.
- Click Install in the Firebase console.
- Select your Firebase project.
- Click Next.
- In the Review APIs enabled and resources created section, click Enable next to any services that are suggested to you:
- Click Next, and then select Grant next to any permissions that are suggested to you.
- Click Next.
- In the Collection path text box, enter
bot
. - In the Storage path text box, enter
tts
. - Leave all other values as their default options.
- Click Install extension and wait for the extension to install.
Try out the Convert Text to Speech extension
While the goal of this codelab is to interact with the Convert Text to Speech extension through a web app, it's helpful to understand how the extension works by triggering the extension using the Firebase console. The extension triggers when a Cloud Firestore document is created in the bot
collection.
To see how the extension works by using the Firebase console, follow these steps:
- In the Firebase console, navigate to Firestore
- Click Start collection.
- In the Collection ID text box, enter
bot
. - Click Next.
- In the Document ID text box, click Auto-ID.
- In the Field text box, enter
text
. - In the Value text box, enter
The quick brown fox jumps over the lazy dog
. - Click Save.
To see and hear the MP3 file that you created, follow these steps:
- In the Firebase console, navigate to Storage.
- In the pane where you can upload files, note your bucket name after the
gs://
value. This is the name of your default bucket for this project. You need it at various tasks throughout this codelab.
- In the Google Cloud Console, navigate to Cloud Storage.
- Select your project.
If you don't see your project within the recent list of projects, click Select Project to locate your project in the project picker.
- Select your default Storage bucket.
- Navigate to the
tts/
folder. - Click the MP3 file.
- At the end of the MP3 file, click and notice that your text is converted to speech.
12. Set up the "Label Videos with Cloud Video AI" extension for the Video Hint app
Install the Label Videos with Cloud Video AI extension
- Navigate to the Label Videos with Cloud Video AI extension.
- Click Install in Firebase console.
- Select your Firebase project.
- Click Next > Next > Next, until you reach the Configure extension section.
- From the Cloud Functions location drop-down, select a supported location (either the location that you previously selected for Firestore and Cloud Storage or the nearest to it). For supported locations, see the
location_id
section inAnnotateVideoRequest
. - From the Model drop-down, select Latest.
- From the Stationary camera drop-down, select No.
- Leave all other values as their default.
- Click Install extension and wait for the extension to install.
Try out the Label Videos with Cloud Video AI extension
While the goal of this codelab is to interact with the Label Videos with Cloud Video AI extension through a web app, it's helpful to understand how the extension works by triggering the extension using the Firebase console. The extension triggers when a video file is uploaded to your Storage bucket.
To see how the extension works by using the Firebase console, follow these steps:
- Navigate to Storage within your Firebase project > Create folder.
- In the Folder name text box, enter
video_annotation_input
.
- Click Add folder.
- In the
video_annotation_input
folder, click Upload file. - In the
ai-extensions-codelab/video-hint-start/public/videos
folder that you cloned or downloaded earlier, select the first video file. - Back in your browser, in the Google Cloud Console, navigate to Cloud Storage.
- Select your default Storage bucket, which you noted earlier.
- Click the
video_annotation_output
folder.
If you don't see thevideo_annotation_output
folder, wait a few seconds and refresh the page because the Video Intelligence API might still be processing the video.
- Notice a JSON file exists that follows a similar name to the file you uploaded earlier.
- Click Download FILENAME.
- Open the downloaded JSON file in your code editor. It contains the raw output from the Video Intelligence API, which includes detected labels of the video that you uploaded.
13. Set up the Video Hint app to use Firebase
To run the Video Hint app, you need to set up your app's code and the Firebase CLI to interact with your Firebase project.
Add Firebase services and configuration to your app's code
To use Firebase, your app's codebase needs the Firebase SDKs for the services you want to use and the Firebase configuration that tells those SDKs which Firebase project to use.
This codelab's sample app already includes all the necessary import and initialization code for the SDKs (see video-hint-start/lib/firebase/firebase.js
), so you don't need to add these. However, the sample app only has placeholder values for the Firebase configuration (see video-hint-start/lib/firebase/firebase-config.js
), so you need to register your app with your Firebase project to get the unique Firebase configuration values for your app.
- In the Firebase console, in your Firebase project, navigate to the Project overview, and then click Web (or click Add app if you've already registered an app with the project).
- In the App nickname text box, enter a memorable app nickname, such as
My Video Hint app
. - Do not select the Also set up Firebase Hosting for this app checkbox. You'll do these steps later in the codelab.
- Click Register app.
- The console displays a code snippet for adding and initializing the Firebase SDK with an app-specific Firebase configuration object. Copy all the properties in the Firebase configuration object.
- In your code editor, open the
video-hint-start/lib/firebase/firebase-config.js
file. - Replace the placeholder values with the values you just copied. It's ok if you have properties and values for Firebase services that you don't use in the Video Hint app.
- Save the file.
- Back in the Firebase console, click Continue to console.
Set up your terminal to run Firebase CLI commands against your Firebase project
- In your terminal, press
Control+C
to stop the server from running the previous web app. - In your terminal, navigate to the
video-hint-start
web app folder:cd ../video-hint-start
- Make the Firebase CLI run commands against a specific Firebase project:
firebase use YOUR_PROJECT_ID
Set up your app's codebase to use framework-aware Firebase Hosting
This codelab uses web frameworks with Hosting (preview) with the Video Hint web app.
- In your terminal, enable web frameworks with Firebase Hosting:
firebase experiments:enable webframeworks
- Initialize Firebase Hosting:
firebase init hosting
- When prompted with
Detected an existing
Next.js
codebase in your current directory, should we use this?
, press Y. - When prompted with
In which region would you like to host server-side content, if applicable?
, select either the default region or the location that you previously selected for Firestore and Cloud Storage, and then pressEnter
(orreturn
on macOS). - When prompted with
Set up automatic builds and deploys with GitHub?
, pressN
.
Run and view the Video Hint web app
- In your terminal, install dependencies in the
video-hint-start
andfunctions
folders, and then run the app:npm install # Include the parentheses in the following command. (cd functions && npm install) firebase emulators:start --only hosting
- In your browser, navigate to the locally hosted Hosting URL. In most cases, it's http://localhost:5000/ or something similar.
The page should load, but you'll notice that various features are missing. We'll add these in the next steps of this codelab.
Troubleshoot running the web app
If you see an error message like Error: The query requires an index. You can create it here: https://console.firebase.google.com
in the Console pane of DevTools, follow these steps:
- Navigate to the provided URL.
- Click Save and wait for the status to change from Building to Enabled.
14. Add functionality to the Video Hint app
In the last step of this codelab, you ran the Video Hint app locally, but it didn't have much functionality and didn't yet use the installed extension. In this step of the codelab, you'll add this functionality and use the web app to trigger the extension.
Deploy Security Rules
This codelab's sample app contains sets of Security Rules for Firestore and for Cloud Storage for Firebase. After you deploy these Security Rules to your Firebase project, the data in your database and your bucket are better protected from misuse.
You can view these rules in the firestore.rules
and storage.rules
files.
- To deploy these Security Rules, run this command in your terminal:
firebase deploy --only firestore:rules,storage
- If you're asked:
"Cloud Storage for Firebase needs an IAM Role to use cross-service rules. Grant the new role?"
, select Yes.
Update the code to combine the functions
- In your code editor, expand the
functions
folder.
This folder contains several functions, which combine to form an extension pipeline. The following table lists and describes each function:Function
Description
functions/01-handle-video-upload.js
The first step in the extension pipeline. It processes the user's uploaded video file.
functions/02-handle-video-labels.js
The second step in the extension pipeline. It processes the video-labels file, which was generated by the
storage-label-videos
extension.functions/03-handle-audio-file.js
The third step in the extension pipeline. It handles the transcribed audio file.
- In the
functions/index.js
file, add the following code:import { initializeApp } from "firebase-admin/app"; export * from "./01-handle-video-upload.js"; export * from "./02-handle-video-labels.js"; export * from "./03-handle-audio-file.js"; initializeApp();
This code uses JavaScript modules to import and export the functions from the index.js
file so that there's one centralized location for all functions.
Update the code to handle video uploading
- In your code editor, open the
lib/firebase/storage.js
file. - Locate the
uploadVideo
function.
This function receivesuserId
,filePath
, andfile
parameters. This data is enough to upload a file to Cloud Storage. - In the body of the
uploadVideo
function, add the following code:const storageRef = ref(storage, `video_annotation_input/${filePath}`); const uploadTask = uploadBytesResumable(storageRef, file, { customMetadata: { uid: userId, }, }); return uploadTask;
Deploy your functions
To deploy your functions with the Firebase CLI, follow these steps:
- In your terminal while still in the
video-hint-start
folder, pressControl+C
to stop the current process. - Deploy your functions:
If you are asked about deleting any previous Cloud Functions, selectfirebase deploy --only functions
No
. - If you see a message similar to
Permission denied while using the Eventarc Service Agent
, wait a few minutes and then retry the command. - After the command is finished, run the app locally again:
firebase emulators:start --only hosting
Run and view the Video Hint web app again (now with functionality)
To run and view the now functional web app, follow these steps:
- In your browser, find the tab where you navigated to http://localhost:5000.
- If necessary, click Sign in with Google and select your Google account.
- Click Upload Example video #1 and wait a few minutes to see the results of the video summary.
- If you don't see any results after you upload the video, see Troubleshoot errors with Cloud Functions in the appendix of this codelab.
15. Conclusion
Congratulations! You achieved a lot in this codelab!
Installed and configured Firebase Extensions
You used the Firebase console to configure and install various AI extensions. Using extensions is convenient because you don't need to write lots of boilerplate code that deals with authenticating with Google Cloud services, reading and writing from Firestore and interacting with Google Cloud services - and the various nuances that are involved with those tasks.
Worked with extensions using the Firebase console
Instead of jumping straight into code, you took time to understand how the AI extensions work, based on an input that you provided via the console to Firestore or Cloud Storage. This type of interaction can be especially useful if you need to debug extension output.
Built three AI-powered web apps that use Firebase Extensions
Reviewly
In the Reviewly web app, you used the Language Tasks with PaLM API extension to summarize long reviews that users left for a T-shirt product. You also requested that the language model provide a JSON response to your query, where the JSON provided a star rating and a summarized review of the original long-form review.
Optional exercise: The T-shirt company is happy with the summarized reviews, but they've asked for an additional summary of the nature of the defect. Can you adjust the prompt to return a summary of the defect, and then include that summary within the user interface of the web app?
Chatbot
In the Chatbot web app, you used the Chatbot with PaLM API extension to provide the user with an interactive chat interface, that includes historical context in conversations - where each message is stored in a Firestore document that is scoped to a particular user.
Optional exercise: The students have been pleased with the chatbot, but the staff would like a few enhancements. Students should be given thought-provoking questions after their answer has been provided. For example:
Student asks: What is the ozone? Response: The ozone is a molecule composed of...How do you think human activities can impact the ozone layer?
Hint: You could use a configurable context option to achieve this.
Video Hint
In the Video Hint web app, you used the Convert Text to Speech, the Language Tasks with PaLM API, and the Label Videos with Cloud Video extensions to form an extension pipeline that results in a text and audio description of a video.
Optional exercise: The government department found the prototype interesting, and they now would also like a user to be able to click on a text-based label to jump to the point in the video where the label is detected.
16. Appendix: Troubleshoot errors with Cloud Functions
If your web app isn't functioning as expected, and you think it might be due to functions, follow the steps in this troubleshooting page.
Allow public unauthenticated access
If you get any permission-related errors in the Console panel of Chrome DevTools, follow these steps:
- Read the Authentication overview | Cloud Run page
- Click the link to view and complete the required tasks to Allow public unauthenticated access to the function.
- Navigate back to the Reviewly app. For example: http://localhost:8080.
- Click Add some mock reviews and wait a few seconds.
- If reviews appear: You don't need to continue with these troubleshooting steps, and can jump straight to Set up the Chatbot web app section in this codelab..
- If reviews do not appear: Continue with this troubleshooting section.
Handle insufficient permission errors
- In the Firebase console, navigate to Functions.
- Hover over the
addMockReviews
function, and then click > View logs.
- Scroll through the logs until you find an error similar to one of the following:
Exception from a finished function: Error: 7 PERMISSION_DENIED: Missing or insufficient permissions.
0001-compute@developer.gserviceaccount.com does not have storage.objects.get access to the Google Cloud Storage object. Permission 'storage.objects.get' denied on resource (or it may not exist).
- In the Google Cloud Console, navigate to the IAM Permissions page, and then select your project.
- In the table, find the Name column.
In the IAM & Admin page, there is a table of users and roles. The name column in the table describes what the user, or principal, is for. You may have a principal with the name of Default compute service account.
If you see the Default compute service account, follow these steps:
- Click Edit principal.
- Continue with the Add roles to the Default compute service account section in this codelab.
If you don't see the Default compute service account, follow these steps:
- Click Grant Access.
- In the New principals text box, enter
compute
. - In the menu of autosuggestions that appears, select Default compute service account.
Add roles to the default compute service account
In the Assign roles section of the default compute service account:
- Expand the Select a role menu.
- In the Filter text box, enter
Cloud Datastore User
. - In the menu of autosuggestions that appears, select Cloud Datastore user.
- Click Add another role.
- Expand the Select a role menu.
- In the Filter text box, enter
Cloud Storage for Firebase Admin
. - In the menu of autosuggestions that appears, select Cloud Storage for Firebase Admin.
- Click Add another role.
- Expand the Select a role menu.
- In the Filter text box, enter
Cloud Storage for Firebase Service Agent
. - In the menu of autosuggestions that appears, select Cloud Storage for Firebase Service Agent.
- Click Save.