|
13 | 13 | # limitations under the License. |
14 | 14 | from __future__ import annotations |
15 | 15 |
|
16 | | -import json |
17 | 16 | from typing import Any |
18 | 17 | from typing import Dict |
19 | 18 | from typing import List |
20 | 19 |
|
21 | 20 | from google.adk.tools import _gda_stream_util |
22 | 21 | from google.auth.credentials import Credentials |
23 | | -from google.cloud import bigquery |
24 | | -import requests |
25 | 22 |
|
26 | | -from . import client |
27 | 23 | from .config import BigQueryToolConfig |
28 | 24 |
|
29 | 25 | _GDA_CLIENT_ID = "GOOGLE_ADK" |
@@ -117,48 +113,39 @@ def ask_data_insights( |
117 | 113 | """ |
118 | 114 | try: |
119 | 115 | location = "global" |
120 | | - if not credentials.token: |
121 | | - error_message = ( |
122 | | - "Error: The provided credentials object does not have a valid access" |
123 | | - " token.\n\nThis is often because the credentials need to be" |
124 | | - " refreshed or require specific API scopes. Please ensure the" |
125 | | - " credentials are prepared correctly before calling this" |
126 | | - " function.\n\nThere may be other underlying causes as well." |
127 | | - ) |
128 | | - return { |
129 | | - "status": "ERROR", |
130 | | - "error_details": "ask_data_insights requires a valid access token.", |
| 116 | + session, endpoint = _gda_stream_util.get_gda_session(credentials) |
| 117 | + with session: |
| 118 | + headers = { |
| 119 | + "Content-Type": "application/json", |
| 120 | + "X-Goog-API-Client": _GDA_CLIENT_ID, |
131 | 121 | } |
132 | | - headers = { |
133 | | - "Authorization": f"Bearer {credentials.token}", |
134 | | - "Content-Type": "application/json", |
135 | | - "X-Goog-API-Client": _GDA_CLIENT_ID, |
136 | | - } |
137 | | - ca_url = f"https://geminidataanalytics.googleapis.com/v1beta/projects/{project_id}/locations/{location}:chat" |
| 122 | + ca_url = ( |
| 123 | + f"{endpoint}/v1beta/projects/{project_id}/locations/{location}:chat" |
| 124 | + ) |
138 | 125 |
|
139 | | - instructions = """**INSTRUCTIONS - FOLLOW THESE RULES:** |
| 126 | + instructions = """**INSTRUCTIONS - FOLLOW THESE RULES:** |
140 | 127 | 1. **CONTENT:** Your answer should present the supporting data and then provide a conclusion based on that data, including relevant details and observations where possible. |
141 | 128 | 2. **ANALYSIS DEPTH:** Your analysis must go beyond surface-level observations. Crucially, you must prioritize metrics that measure impact or outcomes over metrics that simply measure volume or raw counts. For open-ended questions, explore the topic from multiple perspectives to provide a holistic view. |
142 | 129 | 3. **OUTPUT FORMAT:** Your entire response MUST be in plain text format ONLY. |
143 | 130 | 4. **NO CHARTS:** You are STRICTLY FORBIDDEN from generating any charts, graphs, images, or any other form of visualization. |
144 | 131 | """ |
145 | 132 |
|
146 | | - ca_payload = { |
147 | | - "project": f"projects/{project_id}", |
148 | | - "messages": [{"userMessage": {"text": user_query_with_context}}], |
149 | | - "inlineContext": { |
150 | | - "datasourceReferences": { |
151 | | - "bq": {"tableReferences": table_references} |
152 | | - }, |
153 | | - "systemInstruction": instructions, |
154 | | - "options": {"chart": {"image": {"noImage": {}}}}, |
155 | | - }, |
156 | | - "clientIdEnum": _GDA_CLIENT_ID, |
157 | | - } |
| 133 | + ca_payload = { |
| 134 | + "project": f"projects/{project_id}", |
| 135 | + "messages": [{"userMessage": {"text": user_query_with_context}}], |
| 136 | + "inlineContext": { |
| 137 | + "datasourceReferences": { |
| 138 | + "bq": {"tableReferences": table_references} |
| 139 | + }, |
| 140 | + "systemInstruction": instructions, |
| 141 | + "options": {"chart": {"image": {"noImage": {}}}}, |
| 142 | + }, |
| 143 | + "clientIdEnum": _GDA_CLIENT_ID, |
| 144 | + } |
158 | 145 |
|
159 | | - resp = _gda_stream_util.get_stream( |
160 | | - ca_url, ca_payload, headers, settings.max_query_result_rows |
161 | | - ) |
| 146 | + resp = _gda_stream_util.get_stream( |
| 147 | + session, ca_url, ca_payload, headers, settings.max_query_result_rows |
| 148 | + ) |
162 | 149 | except Exception as ex: # pylint: disable=broad-except |
163 | 150 | return { |
164 | 151 | "status": "ERROR", |
|
0 commit comments