Skip to content

Commit 04680a7

Browse files
committed
working with adk web
1 parent 6320805 commit 04680a7

File tree

8 files changed

+2627
-193
lines changed

8 files changed

+2627
-193
lines changed

adk-sql-agent/main.env

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
1-
OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED=true
2-
OTEL_SEMCONV_STABILITY_OPT_IN=gen_ai_latest_experimental
3-
# OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT=event_only
4-
51
GOOGLE_GENAI_USE_VERTEXAI=TRUE
62
GOOGLE_CLOUD_LOCATION=us-central1
3+
GOOGLE_CLOUD_PROJECT=otel-starter-project
74

8-
ENABLE_GCS_PYTHON_CLIENT_OTEL_TRACES=true
9-
10-
PORT=8000
115
OTEL_INSTRUMENTATION_GENAI_UPLOAD_FORMAT='jsonl'
12-
GOOGLE_CLOUD_PROJECT=otel-starter-project
136
OTEL_INSTRUMENTATION_GENAI_COMPLETION_HOOK=upload
147
OTEL_INSTRUMENTATION_GENAI_UPLOAD_BASE_PATH="gs://otel-starter-project-genai-refs/v3"
8+
OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED=true
9+
OTEL_SEMCONV_STABILITY_OPT_IN=gen_ai_latest_experimental

adk-sql-agent/main.py

Lines changed: 0 additions & 167 deletions
This file was deleted.

adk-sql-agent/pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ dependencies = [
3131
"opentelemetry-instrumentation-requests>=0.54b1",
3232
"opentelemetry-instrumentation-sqlite3>=0.54b1",
3333
"opentelemetry-instrumentation-vertexai>=2.0b0",
34+
"opentelemetry-util-genai",
3435
"rich>=14.0.0",
3536
]
3637

@@ -41,3 +42,5 @@ dev = ["debugpy>=1.8.17", "ipython>=9.3.0", "pillow>=11.2.1"]
4142
fsspec = { git = "https://github.com/fsspec/filesystem_spec.git" }
4243
gcsfs = { git = "https://github.com/aabmass/gcsfs.git", branch = "remove-conflicts" }
4344
opentelemetry-exporter-gcp-logging = { git = "https://github.com/GoogleCloudPlatform/opentelemetry-operations-python.git", branch = "main", subdirectory = "opentelemetry-exporter-gcp-logging" }
45+
opentelemetry-instrumentation-google-genai = { path = "../instrumentation-genai/opentelemetry-instrumentation-google-genai", editable = true }
46+
opentelemetry-util-genai = { path = "../util/opentelemetry-util-genai", editable = true }

adk-sql-agent/run.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/sh
2+
uv run --env-file main.env adk web --otel_to_cloud --host 0.0.0.0

adk-sql-agent/sql_agent/agent.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def get_dbpath(thread_id: str) -> str:
7878

7979

8080
root_agent = Agent(
81-
name="weather_time_agent",
81+
name="sql_agent",
8282
model="gemini-2.5-flash",
8383
description=INTRO_TEXT,
8484
instruction=SYSTEM_PROMPT,

adk-sql-agent/sql_agent/tools.py

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,16 @@
1414

1515

1616
import logging
17-
from typing import Any, NotRequired, TypedDict
18-
from opentelemetry import trace
1917
import sqlite3
20-
from google.adk.tools import ToolContext
18+
from typing import Any, NotRequired, TypedDict
2119

2220
from google.genai import Client
2321
from google.genai import types as genai_types
2422

23+
from opentelemetry import trace
24+
from opentelemetry.instrumentation.sqlite3 import SQLite3Instrumentor
25+
26+
2527
tracer = trace.get_tracer(__name__)
2628
logger = logging.getLogger(__name__)
2729

@@ -35,11 +37,13 @@ class SqlRunResult(TypedDict):
3537

3638

3739
def create_run_sql_tool(dbpath: str):
40+
instrumentor = SQLite3Instrumentor()
41+
3842
@tracer.start_as_current_span("run_sql")
3943
def run_sql(sql_query: str) -> dict[str, Any]:
4044
"""Runs a SQLite query. The SQL query can be DDL or DML. Returns the rows if it's a SELECT query."""
4145

42-
with sqlite3.connect(dbpath) as db:
46+
with instrumentor.instrument_connection(sqlite3.connect(dbpath)) as db:
4347
try:
4448
cursor = db.cursor()
4549
cursor.execute(sql_query)
@@ -81,17 +85,25 @@ def run_sql(sql_query: str) -> dict[str, Any]:
8185
# Tool that calls gemini with an image
8286
@tracer.start_as_current_span("describe_uri_tool")
8387
def describe_uri_tool(uri: str, mime_type: str) -> dict[str, Any]:
84-
"""describe_uri_tool is a tool that takes a URI and mime type for the URI (you can guess
85-
the type your best) and returns a description or error
88+
"""describe_uri_tool is a tool that takes a URI (http(s) or gs:// for GCS) and mime type
89+
for the URI (you can guess the type your best) and returns a description or error.
8690
"""
8791
client = Client()
88-
res = client.models.generate_content(
89-
model="gemini-2.0-flash-lite",
90-
contents=[
91-
genai_types.Part.from_text(
92-
text="Describe the following attached file:"
93-
),
94-
genai_types.Part.from_uri(file_uri=uri, mime_type=mime_type),
95-
],
96-
)
97-
return res.model_dump()
92+
93+
try:
94+
res = client.models.generate_content(
95+
model="gemini-2.0-flash-lite",
96+
contents=[
97+
genai_types.Part.from_text(
98+
text="Describe the following attached file:"
99+
),
100+
genai_types.Part.from_uri(file_uri=uri, mime_type=mime_type),
101+
],
102+
)
103+
except Exception as e:
104+
return {"error": str(e)}
105+
106+
if not res.candidates:
107+
return {"error": "No candidates returned"}
108+
109+
return res.candidates[0].model_dump(mode="json")

0 commit comments

Comments
 (0)