This document describes how to test QueryData and update the context set file. You can test QueryData's ability to generate SQL queries from natural language questions. If a generated query is not accurate, you can update the context set file.
To learn about context sets, see Context sets overview.Before you begin
Make sure that a context set is already created and the context set file is uploaded to the QueryData agent. For more information, see Manage context sets in Bigtable Studio.
To download, update, and upload context set files, make sure that the IAM user has the Dataplex Catalog Editor (roles/dataplex.catalogEditor) or Dataplex Catalog Admin (roles/dataplex.catalogAdmin) role on the project. For more information, see Context sets permissions.
Test QueryData
To test a QueryData, perform the following steps:
In the Google Cloud console, go to the Bigtable page.
Select an instance from the list.
In the navigation menu, click Bigtable Studio.
In the Explorer pane, click View actions next to the context set you're using.
Click Test context set.
In the query editor, click Generate SQL using QueryData with: CONTEXT_SET_NAME to open the Help me code panel.
Enter a natural language question in the editor to generate a SQL query, and click Generate.
Review the SQL query for accuracy. Click Insert to add the query to the editor.
Download and update a context set
If you are not satisfied with the generated SQL query for a natural language question, download the existing context set file. You can then review and update the query template, and reupload the updated context file to the agent.
To download and update a context set, perform the following steps:
- In the Explorer pane, click View actions next to the context set you're using.
- Click Download context file.
- Follow steps in Build context sets using the context engineering agent to update context with additional query pairs.
- In the Explorer pane, click View actions next to the context set you're using.
- Click Edit context set.
- Click Browse in the Upload context file section, and select the updated context set file.
- Click Save to update the context set.
After you are satisfied with the accuracy of your responses, you can use the QueryData endpoint to connect your application to the context set.
Find the context set ID
To connect a data application to the QueryData agent, you need the context set's ID.
In the Google Cloud console, go to the Bigtable page.
Select an instance from the list.
In the navigation menu, click Bigtable Studio.
In the Explorer pane, click View actions next to the context set you're using.
Click Edit context set.
Note the context ID in Context set ID. The context set ID format is similar to
projects/PROJECT_ID/locations/LOCATION/contextSetGroups/GROUP_ID/contextSets/CONTEXT_SET_ID@v1.
Connect QueryData to application
Set the context set ID in the QueryData method call to provide authored context for database data sources such as AlloyDB, Spanner, Cloud SQL, Cloud SQL for PostgreSQL, Firestore, and Bigtable. For more information, see
Define data agent context for database data sources.
After testing the context set, you can reference the database data source in your QueryData call.
Example QueryData request with authored context
The following example shows a QueryData request using bigtable_reference database data source. The agent_context_reference.context_set_id field is used to link to the context set that
contains your authored context.
POST https://geminidataanalytics.googleapis.com/v1beta/projects/context-set-project/locations/us-central1:queryData
{ "parent": "projects/context-set-project/locations/us-central1", "prompt": "List the names and ratings of all hotels located in London.", "context": { "datasource_references": { "bigtable_reference": { "database_reference": { "project_id": "context-set-project", "instance_id": "evalbench" }, "agent_context_reference": { "context_set_id": "projects/context-set-project/locations/us-central1/contextSets/hotels_bigtable_all_templates" } } } }, "generation_options": { "generate_query_result": true, "generate_natural_language_answer": true, "generate_disambiguation_question": true, "generate_explanation": true } }
The request body contains the following fields:
prompt: The natural language question from the end user.context: Contains information about the data sources.datasource_references: Specifies the data source type.bigtable_reference: Required when querying the database. This field changes based on the database you are querying.database_reference: Specifies information related to your Bigtable instance.project_id: The project ID that the Bigtable instance belongs to.instance_id: The instance ID of the Bigtable instance.table_ids: Optional. The table IDs to scope the agent to. If unset, all tables in the instance are included.
agent_context_reference: Links to authored context.context_set_id: The complete context set ID of the context set that contains your authored context. For example,projects/PROJECT_ID/locations/LOCATION/contextSetGroups/GROUP_ID/contextSets/CONTEXT_SET_ID@v1.
generation_options: Configures the type of output to generate.generate_query_result: Set to true to generate and return the query results.generate_natural_language_answer: Optional. If set to true, generates a natural language answer.generate_explanation: Optional. If set to true, generates an explanation of the SQL query.generate_disambiguation_question: Optional. If set to true, generates disambiguation questions if the query is ambiguous.
Example QueryData response
Here is an example of a successful response from a QueryData call:
{
"generatedQuery": "SELECT COUNT(*) FROM `hotels` WHERE CAST(cf['location'] AS STRING) = 'London'",
"intentExplanation": "count of hotels in London",
"queryResult": {
"columns": [
{
"name": "$col1",
"type": "int64_type:{}"
}
],
"rows": [
{
"values": [
{
"value": "2"
}
]
}
],
"totalRowCount": "1"
},
"naturalLanguageAnswer": "Based on the data, there are 2 hotels located in London."
}
What's next
- Learn more about context sets.
- Learn how to build context sets using the context engineering agent.
- Learn how to Manage context sets in Bigtable Studio.