Skip to content

Commit 9a05126

Browse files
committed
bring test PR in line with 12721
1 parent dfe1d10 commit 9a05126

File tree

3 files changed

+44
-23
lines changed

3 files changed

+44
-23
lines changed

alloydb/notebooks/embeddings_batch_processing.ipynb

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,14 @@
7373
},
7474
"outputs": [],
7575
"source": [
76-
"%pip install google-cloud-alloydb-connector[asyncpg]==1.4.0 sqlalchemy==2.0.36 pandas==2.2.3 vertexai==1.70.0 asyncio==3.4.3 greenlet==3.1.1 --quiet"
76+
"%pip install \\\n",
77+
" google-cloud-alloydb-connector[asyncpg]==1.4.0 \\\n",
78+
" sqlalchemy==2.0.36 \\\n",
79+
" pandas==2.2.3 \\\n",
80+
" vertexai==1.70.0 \\\n",
81+
" asyncio==3.4.3 \\\n",
82+
" greenlet==3.1.1 \\\n",
83+
" --quiet"
7784
]
7885
},
7986
{
@@ -191,7 +198,6 @@
191198
"instance_name = \"my-primary\" # @param {type:\"string\"}\n",
192199
"database_name = \"test_db\" # @param {type:\"string\"}\n",
193200
"table_name = \"investments\"\n",
194-
"username = \"postgres\"\n",
195201
"password = input(\"Please provide a password to be used for 'postgres' database user: \")"
196202
]
197203
},
@@ -326,7 +332,7 @@
326332
" conn: asyncpg.Connection = await connector.connect(\n",
327333
" connection_string,\n",
328334
" \"asyncpg\",\n",
329-
" user=username,\n",
335+
" user=\"postgres\",\n",
330336
" password=password,\n",
331337
" db=db_name,\n",
332338
" ip_type=IPTypes.PUBLIC,\n",
@@ -367,11 +373,9 @@
367373
},
368374
"outputs": [],
369375
"source": [
370-
"# debug: remove custom database.\n",
371-
"\"\"\"\n",
372376
"from sqlalchemy import text, exc\n",
373377
"\n",
374-
"async def create_db(database_name, connector):\n",
378+
"async def create_db(database_name, connector): \n",
375379
" pool = await init_connection_pool(connector, \"postgres\")\n",
376380
" async with pool.connect() as conn:\n",
377381
" try:\n",
@@ -382,8 +386,7 @@
382386
" except exc.ProgrammingError as e:\n",
383387
" print(e)\n",
384388
"\n",
385-
"await create_db(database_name=database_name, connector=connector)\n",
386-
"\"\"\""
389+
"await create_db(database_name=database_name, connector=connector)"
387390
]
388391
},
389392
{
@@ -774,7 +777,7 @@
774777
"\n",
775778
"**embed_text**\n",
776779
"\n",
777-
"This function your text data and sends it to vertex AI, transforming the text in specific columns into embeddings.\n",
780+
"This function your text data and sends it to Vertex AI, transforming the text in specific columns into embeddings.\n",
778781
"\n",
779782
"**embed_objects_concurrently**\n",
780783
"\n",
@@ -868,9 +871,7 @@
868871
" logger.warning(f\"Error: {e}. Retrying in {delay} seconds...\")\n",
869872
" await asyncio.sleep(delay) # Wait before retrying\n",
870873
" else:\n",
871-
" logger.error(f\"Failed to get embeddings after {retries} attempts.\")\n",
872-
" raise # Raise the error if all retries fail\n",
873-
"\n",
874+
" logger.error(f\"Failed to get embeddings for data: {batch_data} after {retries} attempts.\")\n",
874875
" return []\n",
875876
"\n",
876877
"\n",

alloydb/notebooks/e2e_test.py renamed to alloydb/notebooks/embeddings_batch_processing_e2e_test.py

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818

1919
import asyncpg # type: ignore
2020
import conftest as conftest # python-docs-samples/alloydb/conftest.py
21+
from google.cloud.alloydb.connector import AsyncConnector, IPTypes
2122
import pytest
2223
import sqlalchemy
23-
from google.cloud.alloydb.connector import AsyncConnector, IPTypes
2424
from sqlalchemy.ext.asyncio import AsyncEngine, create_async_engine
2525

2626

@@ -43,7 +43,11 @@ async def _init_connection_pool(
4343
region: str,
4444
password: str,
4545
) -> AsyncEngine:
46-
connection_string = f"projects/{project_id}/locations/{region}/clusters/{cluster_name}/instances/{instance_name}"
46+
connection_string = (
47+
f"projects/{project_id}/locations/"
48+
f"{region}/clusters/{cluster_name}/"
49+
f"instances/{instance_name}"
50+
)
4751

4852
async def getconn() -> asyncpg.Connection:
4953
conn: asyncpg.Connection = await connector.connect(
@@ -71,27 +75,34 @@ async def test_embeddings_batch_processing(
7175
instance_name: str,
7276
region: str,
7377
database_name: str,
74-
username: str,
7578
password: str,
7679
table_name: str,
7780
) -> None:
81+
# TODO: Create new table
7882
# Populate the table with embeddings by running the notebook
7983
conftest.run_notebook(
8084
"embeddings_batch_processing.ipynb",
8185
variables={
8286
"project_id": project_id,
8387
"cluster_name": cluster_name,
8488
"database_name": database_name,
85-
"username": username,
8689
"region": region,
8790
"instance_name": instance_name,
8891
"table_name": table_name,
8992
},
9093
preprocess=preprocess,
9194
skip_shell_commands=True,
9295
replace={
93-
"password = input(\"Please provide a password to be used for 'postgres' database user: \")": f"password = '{password}'",
94-
"await create_db(database_name=database_name, connector=connector)": "",
96+
(
97+
"password = input(\"Please provide "
98+
"a password to be used for 'postgres' "
99+
"database user: \")"
100+
): f"password = '{password}'",
101+
(
102+
"await create_db("
103+
"database_name=database_name, "
104+
"connector=connector)"
105+
): "",
95106
},
96107
until_end=True,
97108
)
@@ -111,25 +122,35 @@ async def test_embeddings_batch_processing(
111122
# Validate that embeddings are non-empty for all rows
112123
result = await conn.execute(
113124
sqlalchemy.text(
114-
f"SELECT COUNT(*) FROM {table_name} WHERE analysis_embedding IS NULL"
125+
f"SELECT COUNT(*) FROM "
126+
f"{table_name} WHERE "
127+
f"analysis_embedding IS NULL"
115128
)
116129
)
117130
row = result.fetchone()
118131
assert row[0] == 0
119132
result = await conn.execute(
120133
sqlalchemy.text(
121-
f"SELECT COUNT(*) FROM {table_name} WHERE overview_embedding IS NULL"
134+
f"SELECT COUNT(*) FROM "
135+
f"{table_name} WHERE "
136+
f"overview_embedding IS NULL"
122137
)
123138
)
124139
row = result.fetchone()
125140
assert row[0] == 0
126141

127142
# Get the table back to the original state
128143
await conn.execute(
129-
sqlalchemy.text(f"UPDATE {table_name} set analysis_embedding = NULL")
144+
sqlalchemy.text(
145+
f"UPDATE {table_name} set "
146+
f"analysis_embedding = NULL"
147+
)
130148
)
131149
await conn.execute(
132-
sqlalchemy.text(f"UPDATE {table_name} set overview_embedding = NULL")
150+
sqlalchemy.text(
151+
f"UPDATE {table_name} set "
152+
f"overview_embedding = NULL"
153+
)
133154
)
134155
await conn.commit()
135156
await pool.dispose()

alloydb/notebooks/requirements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +0,0 @@
1-
# empty

0 commit comments

Comments
 (0)