1. Before you begin
This codelab will guide you through using the Conversational Analytics API (CA API) with Looker to build your own chat application for querying data. To get the most value, you should be familiar with BigQuery, Looker, LLMs and agents, and Python.
What you'll learn
After following this codelab, you'll have learned:
- How to use Conversational Analytics with Looker
- How to set up and use the Conversational Analytics Python quickstart app (and how the app works)
- The benefits of using Looker for semantic modeling with Conversational Analytics
What you'll need
To complete this codelab, you'll need:
- A Looker instance with the ASC Demographic Data Looker Block installed
- API and developer access to your Looker instance
- A local environment with Python v3.11+, Git, and gcloud installed.
- A cloud project with these IAM roles set up for your user account:
roles/bigquery.dataViewerBigQuery Data Viewerroles/bigquery.userBigQuery Userroles/looker.instanceUserLooker Instance User
2. Build and try out your chat app with BigQuery
Let's first set up and try out the Conversational Analytics quickstart app with BigQuery as a data source.
Authenticate and enable Cloud APIs
- Authenticate locally with your user account:
gcloud auth login
- Enable Cloud project APIs. Please replace
YOUR_PROJECT_IDwith the ID of your Google Cloud project:
gcloud services enable geminidataanalytics.googleapis.com bigquery.googleapis.com cloudaicompanion.googleapis.com --project=YOUR_PROJECT_ID
- Set application default credentials (ADC) and billing:
gcloud auth application-default login
gcloud auth application-default set-quota-project YOUR_PROJECT_ID
Set up the quickstart app
- Clone the quickstart github repo into your local environment. See the following example command below:
git clone "https://github.com/looker-open-source/ca-api-quickstarts"
- Navigate into the
ca-api-quickstartsdirectory you've cloned:
cd ca-api-quickstarts
- Create a
secrets.tomlfile in the.streamlitdirectory.YOUR_LOOKER_CLIENT_IDandYOUR_LOOKER_CLIENT_SECRETshould be your Looker client ID and Looker client secret from your Looker instance. The file contents should be as follows:
[cloud]
project_id = "YOUR_PROJECT_ID"
[looker]
client_id = "YOUR_LOOKER_CLIENT_ID"
client_secret = "YOUR_LOOKER_CLIENT_SECRET"
You can use the following example command to create the file. Make sure to replace the values with your project ID, client ID, and client secret:
cat > .streamlit/secrets.toml <<'EOF'
[cloud]
project_id = "YOUR_PROJECT_ID"
[looker]
client_id = "YOUR_LOOKER_CLIENT_ID"
client_secret = "YOUR_LOOKER_CLIENT_SECRET"
EOF
- Install the quickstart app's Python requirements:
pip install -r requirements.txt
- Run the quickstart app:
streamlit run app.py
- Open https://localhost:8501 in your browser of choice.
Try out the quickstart app
You are now on the quickstart app's agents page. You have no data agents available.
- Now let's create a data agent that speaks like a pirate in the Create Agent form:
- Set the Display name field:
Pirate BQ Agent
- Set the System Instructions field:
You are a pirate. Answer questions like a pirate
- Select the BigQuery option.
- Set the Project field:
bigquery-public-data
- Set the Dataset field:
google_trends
- Set the Table field:
top_rising_terms
- Select Create agent.
- Now let's navigate to the chat page through the left main navigation. Our newly created agent is available to chat. Let's start a chat by adding a prompt/message: "Hello, how are you?"
- Now let's learn a little bit about this dataset: "What's this dataset about?"
- Ask another question - something like this:
- "What [Enter State] locations are in the dataset?"
- "Can you give me the data for the last 2 weeks for [City, State?]"
- "Can you create a visualization?"
You've just had a multi-turn conversation with your data agent and your BigQuery data in your own chat application.
3. Model and explore your data in Looker
Now let's model and explore the same BigQuery data in Looker.
Query BigQuery census data
What happens when we connect to a larger, relational dataset? First we'll create a new agent.
- Navigate back to the agents page's Create Agent form.
- Set the Display name field:
BQ Census Agent
- Set the System Instructions field:
You are really excited about census data, you love learning about the demographics in your area because you want to come up with marketing campaigns for your company
- Select the BigQuery option.
- Set the Project field:
bigquery-public-data
- Set the Dataset field:
census_bureau_acs
- Set the Table field:
blockgroup_2018_5yr
- Select Create agent.
- Ask the newly created agent both of these questions:
- "What data do you have?"
- "Can you show me a preview of my data"?
Use Looker to model the census data
Now we are going to use a LookML model to define the BigQuery census data. We will solve the issues we just saw previously.
- Log in to your Looker instance
- Navigate to your ACS demographic data block files by navigating to the left main navigation, selecting Develop, and then selecting marketplace_acs_census-v2.
- Let's take a look at a Looker view. Open the
imported_projectdirectory, theacs_census-v2directory, and thegeographydirectory. - Finally, open the
cbsa.viewfile. - On line 17, we can see that the LookML is creating a derived table. In other words, this code is creating a new, combined table by joining two public BigQuery datasets, one with census data and another with geographic boundary data. LookML allows us to join on multiple datasets by creating a persistent derived table to query.
Additionally, the join is defined in versionable code (LookML) instead of system instructions, making agent development more scalable. With LookML, you can define specific assumptions and constraints around your data to support your users as they chat with your data through a data agent.
Explore the Looker modeled census data
Let's run a quick query in an Explore to demonstrate that we have one centralized data source to query.
- In the left main navigation menu, select Explore, then Census Data Block, then State and County.
- Select the measure Education > Associates Degree.
- Select the following dimensions:
- Geography > Block Group
- Geography > County > County Name
- Geography > State > State.
- Run the query (top right) and you will now see that those Geography IDs (called "Block Group") are now associated with human readable strings.
- Open the visualization tab and you will see this data in a map.
4. Try out the chat app with Looker
Now let's create a data agent for Looker.
- Navigate to your quickstart app's agent page's Create Agent form:
- Set the Display name field:
Cartoon character data analyst
- Set the System Instructions field:
You are [fill in your favorite cartoon character, Bugs Bunny is a favorite]. Answer questions like that cartoon character
- Select the Looker option.
- Set the Url field to your looker instance's URL: "https://..."
- Set the Model field:
data_block_acs_bigquery
- Set the Explore field:
cbsa
- Select Create agent.
- Navigate to the chat page and select the newly created agent.
- Let's start a chat by adding a prompt/message: "Hello, how are you?"
- Let's ask the agent to describe the dataset: "What data do you have available?"
- Let's ask a few more questions:
- "Which county in NY has the biggest population?"
- "What about the most graduates from high school per capita?"
- "Can you give me the population by gender and state?"
- "I would like to see a bar chart of the top 10 states with the most population?"
5. What else can the quickstart app do?
You've now:
- Set up the chat app locally
- Created a BigQuery data agent and a Looker data agent
- Held conversations with your data agents
- Learned about the benefits of Looker semantic modeling as context for your chats
Let's try out the other capabilities of the quickstart app:
- Reload the quickstart app's web page and navigate to the chat page.
- You can see your past conversations with all their respective messages from both your BigQuery and Looker agents. The app's frontend is not storing the past convos or messages, but is fetching them from the CA API.
- Now navigate to the agents page and select your Looker agent from the list.
- You can update the agent's configurations or delete the agent if you'd like. No agents are stored in the app's frontend.
6. Optional challenges and further exploration
If you'd like, you can take this opportunity to familiarize yourself with Conversational Analytics and the quickstart app. Or give yourself a challenge to modify the quickstart app. You can try out some of the following suggestions and challenges or skip ahead to the final section.
Familiarize yourself more with the CA API and quickstart app
- Learn more about in depth system instructions to control your agent's behavior.
- Set up another data agent pointing to a new BigQuery dataset.
- Install a new Looker block in your Looker instance, create an agent, and point it to a new Looker dataset.
Quickstart app modification challenges
- Are you familiar with Looker embedding? If so, try embedding a Looker dashboard inside the quickstart app. Consider using Streamlit's iframe component.
- Find a public dataset containing video URLs, and update the app's chat rendering logic to show a video in chat.
7. Conclusion and takeaways
You made it to the end! You've built a chat app, created data agents, and chatted with your data all with the Conversational Analytics API. Let's review some key capabilities and concepts!
Conversational Analytics capabilities
We've covered a lot of the Conversational Analytics capabilities like the ability to:
- Create, update, list, get, and delete data agents.
- Point data agents to either BigQuery or Looker as a data source.
- Create, list, and get conversations and their respective messages.
- Produce vega chart specifications for immersive visualization in conversations.
- Use past messages in a conversation as useful context for future messages.
Benefits of using Looker with the CA API
Looker provides centralized, testable, and governable semantic modeling of your data. Combined with Conversational Analytics, Looker provides a scalable way to provide context for your data agents.
What's next
- Check out the Conversational Analytics documentation for more guides, features, and use cases.
- Stay up to date with and contribute to the quickstart application repository.