Answer questions based on Chat conversations with a Gemini AI Chat app
Stay organized with collections
Save and categorize content based on your preferences.
Spark icon
AI-generated Key Takeaways
This tutorial guides you through building a Google Chat app that uses generative AI (Vertex AI with Gemini) to provide answers within Chat spaces.
The app continuously learns and bases its answers on the entire chat history, responding to questions in real-time, even if not directly addressed.
If the AI cannot find an answer, it can alert a designated space manager for assistance.
You'll need a Google Workspace account, access to Google Cloud services (including billing), and potentially a Node.js development environment to get started.
The app utilizes various Google Cloud services, such as Vertex AI, Chat API, Firestore, and Pub/Sub, to achieve its functionality.
This tutorial shows how to make a Google Chat app that answers
questions based on conversations in Chat spaces with generative
AI powered by Vertex AI with Gemini. The Chat app uses
the Google Workspace Events API plus Pub/Sub to recognize and answer questions
posted in Chat spaces in real time, even when it
isn't mentioned.
The Chat app uses all the messages sent in the space as
a data source and knowledge base: when someone asks a question, the
Chat app checks for previously shared answers and then
shares one. If no answer is found, it says it can't answer. On each answer,
users can click an accessory action button to @mention a space manager and ask
for an answer. By using Gemini AI, the Google Chat app adapts and
grows its knowledge base as it continuously trains on conversations in spaces
it's added to.
Here's how the Chat app works in an employee onboarding
and support space:
Mentioning the AI knowledge assistant app adds it to a space.
Figure 1. Charlie adds the AI knowledge assistant Chat app to a Chat space.
Dana asks a question.
Figure 2. Dana asks if the company offers public speaking training.
The AI knowledge assistant Chat app answers the question.
Figure 3. The AI knowledge assistant Chat app prompts Vertex AI with Gemini to answer Dana's question based on the Chat space's conversation history and then shares the answer.
Build a Chat app that uses generative AI to
answer questions based on knowledge shared in Chat space
conversations.
With generative AI:
Detect and answer employee questions.
Continuously learn from ongoing conversations in a Chat
space.
Listen for and respond to messages in a Chat space in real
time even when the Chat app isn't directly
messaged.
Persist messages by writing to, and reading from, a Firestore database.
Facilitate collaboration in a Chat space by mentioning space
managers when no answer to a question is found.
Architecture
The following diagram shows the architecture of the Google Workspace and
Google Cloud resources used by the AI knowledge assistant
Chat app.
Architecture diagram for the AI knowledge assistant Chat app
The AI knowledge assistant Chat app works like this:
A user adds the AI knowledge assistant Chat app
to a Chat space:
The Chat app prompts the
user who added it to the Chat space to configure
authentication and authorization.
The Chat app fetches the space's messages by
calling the spaces.messages.list method in the Chat API and
then stores the fetched messages in a Firestore database.
The Chat app calls the
subscriptions.create method in the Google Workspace Events API to start
listening for events like messages in the space. The
subscription's notification endpoint is a Pub/Sub topic that uses
Eventarc to forward the event to the Chat app.
The Chat app posts an introduction message to
the space.
A user in the Chat space posts a message:
The Chat app receives the message in real time
from the Pub/Sub topic.
The Chat app adds the message to the Firestore
database.
If a user later edits or deletes the message, the
Chat app receives the updated or deleted event
in real time and then updates or deletes the message in the Firestore
database.
The Chat app sends the message to Vertex AI
with Gemini:
The prompt instructs Vertex AI with Gemini to check if the message
includes a question. If it does, Gemini answers the question based
on the Chat space's message history kept in Firestore
and the Google Chat app then sends the message to the
Chat space. If it doesn't, don't respond.
If Vertex AI with Gemini answers the question, the
Chat app posts the
answer by calling the spaces.messages.create method in
Chat API using app authentication.
If Vertex AI with Gemini can't answer the question, the
Chat app posts a
message saying that it can't find an answer to that question in
the Chat space's history.
Messages always include an accessory action button that users can
click, which causes the Chat app to
@mention a space manager asking them to answer.
The Chat app receives a lifecycle notification from
the Google Workspace Events API that the Chat space subscription
is about to expire:
The Chat app sends a request to renew the
subscription by calling the subscriptions.patch method in the
Google Workspace Events API.
The Chat app is removed from a Chat
space:
The Chat app deletes the subscription by
calling the subscriptions.delete method in the
Google Workspace Events API.
The Chat app deletes the Chat
space's data from Firestore.
Review the products used by the AI knowledge assistant Chat app
The AI knowledge assistant Chat app uses the
following Google Workspace and Google Cloud products:
Vertex AI API with Gemini:
A generative AI platform powered by Gemini. The AI knowledge assistant
Chat app uses Vertex AI API with Gemini to
recognize, understand, and answer employee questions.
Chat API:
An API for developing Google Chat apps that receive and respond to
Chat interaction events, like messages. The AI
knowledge assistant Chat app uses Chat API to:
Receive and respond to interaction events sent by Chat.
List messages sent in a space.
Post responses to user questions in a space.
Configure attributes that determine how it appears in
Chat, like name and avatar image.
Google Workspace Events API:
This API lets you subscribe to events and manage
change notifications across Google Workspace applications. The
AI knowledge assistant Chat app uses the
Google Workspace Events API to listen for messages posted in a
Chat space so it can detect and answer questions even when it
isn't mentioned.
Firestore:
A serverless document database. The AI knowledge assistant
Chat app uses Firestore to store data about messages
sent in a Chat space.
Pub/Sub:
Pub/Sub is an asynchronous and scalable messaging service that decouples
services producing messages from services processing those messages. The
AI knowledge assistant Chat app uses Pub/Sub to
receive subscription events from Chat spaces.
Eventarc:
Eventarc lets you build event-driven architectures without having to
implement, customize, or maintain the underying infrastructure. The AI
knowledge assistant Chat app uses Eventarc to
route events from Pub/Sub to a Chat space and
the Cloud Function that receives and processes the subscription events.
Cloud Functions:
A lightweight serverless compute service that lets you create
single-purpose, standalone functions that can respond to Chat
interaction and subscription events without the need to manage a server or runtime
environment. The AI knowledge assistant Chat app
uses two Cloud Functions named:
app: Host the HTTP endpoint that Chat sends
interaction events to and as a compute platform to run logic that
processes and responds to these events.
eventsApp: Receives and process Chat space
events like messages from a Pub/Sub subscription.
Cloud Functions uses the following Google Cloud products to build and
host compute resources:
Cloud Build:
A fully managed continuous integration, delivery and deployment platform
that runs automated builds.
Cloud Run:
A fully managed environment for running containerized apps.
Prepare the environment
This section shows how to create and configure a Google Cloud project for the
Chat app.
In the Google Cloud console, enable the Google Chat API, the Vertex AI API,
the Cloud Functions API, the Firestore API, the Cloud Build API,
the Pub/Sub API, the Google Workspace Events API, the Eventarc API
and the Cloud Run Admin API.
Confirm that you're enabling the APIs in the correct
Cloud project, then click Next.
Confirm that you're enabling the correct APIs, then click Enable.
gcloud CLI
If necessary, set the current Cloud project to the one you
created:
gcloudconfigsetprojectPROJECT_ID
Replace PROJECT_ID with the Project ID of the
Cloud project you created.
Enable the Google Chat API, the Vertex AI API, the Cloud Functions API,
the Firestore API, the Cloud Build API, the Pub/Sub API,
the Google Workspace Events API, the Eventarc API and the Cloud Run Admin
API:
Authentication and authorization lets the
Chat app access resources in Google Workspace and
Google Cloud.
In this tutorial, you publish the Google Chat app internally so it's
OK to use placeholder information. Before publishing the
Google Chat app externally, replace placeholder information with
real information for the consent screen.
Configure the OAuth consent screen, specify scopes, and register your app
In the Google Cloud console, go to Menu menu>Google Auth platform>Branding.
If you have already configured the
Google Auth platform, you can configure
the following OAuth Consent Screen settings in
Branding,
Audience,
and Data Access.
If you see a message that says
Google Auth platform not configured yet,
click Get Started:
Under App Information, in App name, type
AI knowledge assistant.
In User support email, select your email address or an appropriate
Google group.
Click Next.
Under Audience, select Internal. If you can't select
Internal, select External.
Click Next.
Under Contact Information, enter an Email address where you can
be notified about any changes to your project.
REGION: the Cloud Function's region, like us-central1.
Later, when you create the two Cloud Functions, you must set their
region to this value.
PROJECT_ID: the Project ID of the Cloud project
you created.
Click Create.
From the OAuth client created window, click Download JSON.
Save the downloaded file as credentials.json. Later, when you create
the two Cloud Functions, you include the credentials.json file in each
deployment.
Click OK.
Create the Pub/Sub topic
The Pub/Sub topic works with
Google Workspace Events API to subscribe to events in a Chat
space like messages and notify the Chat app in real
time.
Here's how to create the Pub/Sub topic:
Google Cloud console
In the Google Cloud console, go to
Menumenu>Pub/Sub.
The Firestore database persists and retrieves
data from Chat spaces, like messages. You don't define the data
model, which is set implicitly in the sample code by the model/message.js and
services/firestore-service.js files.
The AI knowledge assistant Chat app database uses a
NoSQL data model based on
classdocuments
organized into
collections_bookmarkcollections. To learn more, see
Firestore data model.
The following diagram is an overview of the AI knowledge assistant
Chat app's data model:
Data model of the Firestore database.
The root contains two collections:
spaces, where each document represents a Chat space that
the Chat app is added to. Each message is represented
by a document in the messages subcollection.
users, where each document represents a user who added the
Chat app to a Chat space.
View collection, document, and field definitions
spaces
A Chat space that includes the AI knowledge assistant Chat app.
Fields
Document ID
String Unique ID of a specific space. A part of the space's resource name in Chat API.
messages
Subcollection of Documents (messages) Messages sent in the Chat space. Corresponds with the Document ID of a message in Firebase.
spaceName
String The unique name of the space in the Chat API. Corresponds with the space's resource name in Chat API.
messages
Messages sent in the Chat space.
Fields
Document ID
String Unique ID of a specific message.
name
String The unique name of a message in the Chat API. Corresponds with the message's resource name in Chat API.
text
String The message's text body.
time
String (Timestamp format) The time at which the message was created.
users
Users who added the AI knowledge assistant Chat app to a Chat space.
Fields
Document ID
String Unique ID of a specific user.
accessToken
String The access token granted during OAuth 2.0 user authorization used to call Google Workspace APIs.
refreshToken
String The refresh token granted during OAuth 2.0 user authorization.
Here's how to create the Firestore database:
Google Cloud console
In the Google Cloud console, go to
Menumenu>Firestore.
From Select your Firestore mode, click Native mode.
Click Continue.
Configure the database:
In Name your database, leave the Database ID as (default).
Under Location type, select Region.
In Region, specify a region for your database, such as
us-central1. For best performance, select the same or nearby
location as the Chat app's Cloud Functions.
Replace LOCATION with the name of a Firestore
region, such
as us-central1. For best performance, select the same or nearby
location as the Chat app's Cloud Functions.
Create and deploy the Chat app
Now that your Google Cloud project is created and configured, you're ready to build
and deploy the Chat app. In this section, you do the
following:
Create and deploy two Cloud Functions. One to respond to Chat
interaction events and one to respond to Pub/Sub events.
Create and deploy a Chat app on the Google Chat API
configuration page.
Create and deploy the Cloud Functions
In this section, you create and deploy two Cloud Functions named:
app: Hosts and runs the Chat app's
code that responds to events received from Chat as HTTP
requests.
eventsApp: Receives and processes Chat space
events like messages from Pub/Sub.
Together, these Cloud Functions make up the AI knowledge assistant
Chat app's application logic.
Optionally, before creating the Cloud Functions, take a moment to review and
familiarize yourself with the sample code hosted on GitHub.
The extracted folder contains the entire Google Workspace samples
repository.
In the extracted folder, navigate to the
google-chat-samples-main/node/ai-knowledge-assistant directory.
In the google-chat-samples/node/ai-knowledge-assistant directory,
add the credentials.json file that you downloaded when you
created OAuth client ID credentials
for authentication and authorization.
Compress the contents of the ai-knowledge-assistant folder into a zip
file.
The zip file must contain the following
files and folders:
.gcloudignore
.gitignore
README.md
deploy.sh
env.js
events_index.js
http_index.js
index.js
credentials.json
package-lock.json
package.json
controllers/
model/
services/
test/
In the Google Cloud console, go to
Menumenu>Cloud Functions.
Make sure that the Google Cloud project for your
Chat app is selected.
Click add_boxCreate Function.
On the Create function page, set up your function:
In Environment, select Cloud Run Function.
In Function name, type app.
In Region, select a region, like us-central1. This region
must match the region you set in the authorized redirect URI when
you
created OAuth client ID credentials
for authentication and authorization.
In Trigger type, select HTTPS.
Under Authentication, select
Allow unauthenticated invocations.
Click Next.
In Runtime, select Node.js 20.
In Entry point, delete the default text and enter
app.
Google Cloud uploads the zip file to and extracts the component files
in this bucket. Cloud Functions then copies the component files into
the Cloud Function.
In Zip file, upload the zip file you downloaded from GitHub,
extracted, and recompressed:
Click Browse.
Navigate to and select the zip file.
Click Open.
Click Deploy.
The Cloud Functions detail page opens, and your function appears
with two progress indicators: one for the build and one for the service.
When both progress indicators disappear and are replaced with a check
mark, your function is deployed and ready.
Edit the sample code to set constants:
On the Cloud Function detail page, click Edit.
Click Next.
In Source code, select Inline editor.
In the inline editor, open and edit the env.js file:
Set the value of project to your
Cloud project ID.
Set the value of location to the Cloud Function's
region,
like us-central1.
Switch to the directory that holds the code for this AI knowledge
assistant Chat app:
cdgoogle-chat-samples/node/ai-knowledge-assistant
In the google-chat-samples/node/ai-knowledge-assistant directory,
add the credentials.json file that you downloaded when you
created OAuth client ID credentials
for authentication and authorization.
Edit the env.js file to set environment variables:
Set the value of project to your Cloud project
ID.
Set the value of location to the Cloud Function's
region, like
us-central1.
Make sure that the Google Cloud project for your
Chat app is selected.
Click add_boxCreate Function.
On the Create function page, set up your function:
In Environment, select Cloud Run Function.
In Function name, type eventsApp.
In Region, select a region, like us-central1. This region
must match the region you set in the authorized redirect URI when
you
created OAuth client ID credentials
for authentication and authorization.
In Trigger type, select Cloud Pub/Sub.
In Cloud Pub/Sub topic, select the Pub/Sub topic name you
created, which has the format
projects/PROJECT/topics/events-api where
PROJECT is your Cloud project ID.
If you see a message beginning to Service account(s) might not have
enough permissions to deploy the function with the selected
trigger., click Grant All.
Click Next.
In Runtime, select Node.js 20.
In Entry point, delete the default text and enter
eventsApp.
In Source code, select Zip from Cloud Storage.
In Cloud Storage location, click Browse.
Select the bucket you uploaded the zip file to when you created the
app Cloud Function.
Click the zip file you uploaded.
Click Select.
Click Deploy.
The Cloud Functions detail page opens, and your function appears
with three progress indicators: one for the build, one for the service,
and one for the trigger. When all three progress indicators disappear
and are replaced with a check mark, your function is deployed and ready.
Edit the sample code to set constants:
On the Cloud Function detail page, click Edit.
Click Next.
In Source code, select Inline editor.
In the inline editor, open and edit the env.js file:
Set the value of project to your
Cloud project ID.
Set the value of location to the Cloud Function's
region,
like us-central1.
Click Deploy.
gcloud CLI
In gcloud CLI, if you are not already there, switch to the directory
that holds the code for this AI knowledge
assistant Chat app that you previously
cloned from GitHub:
cdgoogle-chat-samples/node/ai-knowledge-assistant
In the google-chat-samples/node/ai-knowledge-assistant directory,
add the credentials.json file that you downloaded when you
created OAuth client ID credentials
for authentication and authorization.
Edit the env.js file to set environment variables:
Set the value of project to your Cloud project
ID.
Set the value of location to the Cloud Function's
region, like
us-central1.
In the Name column of the list of Cloud Functions, click
app.
Click Trigger.
Copy the URL.
gcloud CLI
Describe the app Cloud Function:
gcloudfunctionsdescribeapp
Copy the url property.
Configure the Chat app in the Google Cloud console
This section shows how to configure the Chat API in the
Google Cloud console with information about your Chat app,
including the Chat app's name
and the trigger URL of the Chat app's Cloud
Function to which it sends Chat interaction events.
In the Google Cloud console, click Menumenu>More products>Google Workspace>Product Library>Google Chat API>Manage>Configuration.
Clear Build this Chat app as a Google Workspace add-on. A dialog opens asking you to confirm. In the dialog, click Disable.
In App name, type AI knowledge assistant.
In Avatar URL, type https://fonts.gstatic.com/s/i/short-term/release/googlesymbols/live_help/default/24px.svg.
In Description, type Answers questions with AI.
Click the Enable Interactive features toggle to the on position.
Under Functionality, select Join spaces and group conversations.
Under Connection settings, select HTTP endpoint URL.
In HTTP endpoint URL, paste the Trigger URL from the app Cloud Function
formatted as
https://REGION-PROJECT_ID.cloudfunctions.net/app
where REGION is the Cloud Function's region, like us-central1
and PROJECT_ID is the Project ID of the
Cloud project you created.
Under Visibility, select
Make this Chat app available to specific people and groups in your
Workspace domain and enter your email address.
Optionally, under Logs, select Log errors to Logging.
Click Save. A configuration saved message appears, meaning the
Chat app is
ready to test.
Test the Chat app
Test the AI knowledge assistant Chat app in a
Chat space with messages by asking questions that the AI
knowledge assistant Chat app can answer.
Here's a few ways to test the AI knowledge assistant
Chat app:
Add the AI knowledge assistant Chat app to an
existing Chat space and ask questions that are relevant to
that space.
Create a Chat space and post a few messages to use as
a data source. Messages can be sourced from
Gemini with a prompt like
Answer 20 common onboarding questions employees ask their teams. Or,
you can paste a few paragraphs from the
develop with Chat overview guide
and then ask questions about it.
For this tutorial, lets create a Chat space and paste a few
paragraphs from the
develop with Chat overview guide.
Copy and paste the guide's content into the Chat space
you created.
Add the AI knowledge assistant Chat app:
In the message compose bar, type @AI knowledge assistant and in the
suggestion menu that appears, select the AI knowledge assistant
Chat app, and press enter.
A message appears asking if you want to add the AI knowledge assistant
Chat app to the space. Click Add to space.
If this is the first time you add the Chat app
to a space, you must configure authentication and authorization for the
Chat app:
Click Configure.
A new browser window or tab opens asking you to choose a
Google Account. Choose the account you are testing with.
Review the permissions that the AI knowledge assistant
Chat app requests. To grant them, click
Allow.
A message that says You may close this page now. appears.
Close the browser window or tab and return to the
Chat space.
Ask a question:
In the message compose bar, type a question like
What are Google Chat apps?
The AI knowledge assistant Chat app answers.
Optionally, if the answer isn't accurate or sufficient, to help the AI's
conversation history improve, click
contact_supportGet help.
The AI knowledge assistant Chat app mentions
a space manager and asks them to answer the question. Next time, the
AI knowledge assistant Chat app will know
the answer!
Considerations, alternate architecture choices, and next steps
This section reviews other ways the AI knowledge assistant
Chat app can be built.
Firestore, Cloud Storage, or calling List Messages in Chat API
This tutorial recommends storing Chat space data like
messages in a Firestore database because it improves performance compared
with calling the list method on the Message
resource with Chat API every time the
Chat app answers a question. Further, calling
list messages repeatedly can cause the
Chat app to hit API quota limits.
However, if a Chat space's conversation history becomes
too long then using Firestore can become costly.
Cloud Storage
is an alternative to Firestore. Each space the AI knowledge assistant
Chat app is active in gets its own object, and each object
is a text file that contains all the messages in the space. The advantage of
this approach is that the full contents of the text file can be fed to Vertex
AI with Gemini at once, but the drawback is that it takes more work to
update the conversation history because you can't append to an object in Cloud
Storage, only replace it. This approach doesn't make sense if you regularly
update the message history, but it would be a good choice if you batch-update
the message history periodically, say once per week.
Troubleshoot
When a Google Chat app or
card returns an error, the
Chat interface surfaces a message saying "Something went wrong."
or "Unable to process your request." Sometimes the Chat UI
doesn't display any error message, but the Chat app or
card produces an unexpected result; for example, a card message might not
appear.
Although an error message might not display in the Chat UI,
descriptive error messages and log data are available to help you fix errors
when error logging for Chat apps is turned on. For help viewing,
debugging, and fixing errors, see
Troubleshoot and fix Google Chat errors.
Clean up
To avoid incurring charges to your Google Cloud account for the
resources used in this tutorial, we recommend that you delete the
Cloud project.
In the Google Cloud console, go to the Manage resources page. Click
Menumenu>IAM & Admin>Manage Resources.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025年10月13日 UTC."],[],["The described application is a Google Chat app that uses Vertex AI with Gemini to provide AI-powered Q\\&A within chat spaces. It listens to real-time messages using the Google Workspace Events API and Pub/Sub, storing them in Firestore. The app learns from the conversation history, automatically renewing subscriptions, and deleting data upon removal. If unable to answer, it provides a button to escalate to a manager. The app is built using Cloud Functions, Cloud Run, and various Google Cloud APIs. Setup involves project creation, API activation, OAuth configuration, Pub/Sub topic setup, Firestore database creation, Cloud Function deployment, and Chat app configuration.\n"]]