Skip to content

Commit 9038fa4

Browse files
committed
WL#17088 Formatting ML SDK [POSTFIX]
Issue: AI package formatting and type hints are flagged in some build pipelines Solution: Fix formatting Change-Id: If7cda8d86fd8f434a92765dd91608dfa4d8c5232
1 parent 12ce368 commit 9038fa4

File tree

17 files changed

+400
-138
lines changed

17 files changed

+400
-138
lines changed

mysql-connector-python/lib/mysql/ai/genai/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@
2626
# along with this program; if not, write to the Free Software Foundation, Inc.,
2727
# 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2828

29+
"""GenAI package for MySQL Connector/Python.
30+
31+
Performs optional dependency checks and exposes public classes:
32+
- MyEmbeddings
33+
- MyLLM
34+
- MyVectorStore
35+
"""
2936
from mysql.ai.utils import check_dependencies as _check_dependencies
3037

3138
_check_dependencies(["GENAI"])

mysql-connector-python/lib/mysql/ai/genai/embedding.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,19 @@
2626
# along with this program; if not, write to the Free Software Foundation, Inc.,
2727
# 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2828

29+
"""Embeddings integration utilities for MySQL Connector/Python.
30+
31+
Provides MyEmbeddings class to generate embeddings via MySQL HeatWave
32+
using ML_EMBED_TABLE and ML_EMBED_ROW.
33+
"""
34+
2935
from typing import Dict, List, Optional
3036

3137
import pandas as pd
3238

3339
from langchain_core.embeddings import Embeddings
40+
from pydantic import PrivateAttr
41+
3442
from mysql.ai.utils import (
3543
atomic_transaction,
3644
execute_sql,
@@ -40,8 +48,6 @@
4048
sql_table_to_df,
4149
temporary_sql_tables,
4250
)
43-
from pydantic import PrivateAttr
44-
4551
from mysql.connector.abstracts import MySQLConnectionAbstract
4652

4753

@@ -134,7 +140,13 @@ def embed_documents(self, texts: List[str]) -> List[List[float]]:
134140
temporary_tables.append((self.schema_name, table_name))
135141

136142
# ML_EMBED_TABLE expects input/output columns and options as parameters
137-
embed_query = f"CALL sys.ML_EMBED_TABLE('{qualified_table_name}.text', '{qualified_table_name}.embeddings', {self.options_placeholder})"
143+
embed_query = (
144+
"CALL sys.ML_EMBED_TABLE("
145+
f"'{qualified_table_name}.text', "
146+
f"'{qualified_table_name}.embeddings', "
147+
f"{self.options_placeholder}"
148+
")"
149+
)
138150
execute_sql(cursor, embed_query, params=self.options_params)
139151

140152
# Read back all columns, including "embeddings"

mysql-connector-python/lib/mysql/ai/genai/generation.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@
2626
# along with this program; if not, write to the Free Software Foundation, Inc.,
2727
# 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2828

29+
"""GenAI LLM integration utilities for MySQL Connector/Python.
30+
31+
Provides MyLLM wrapper that issues ML_GENERATE calls via SQL.
32+
"""
33+
2934
import json
3035

3136
from typing import Any, List, Optional
@@ -34,9 +39,9 @@
3439
from langchain_core.language_models.llms import LLM
3540
except ImportError:
3641
from langchain.llms.base import LLM
37-
from mysql.ai.utils import atomic_transaction, execute_sql, format_value_sql
3842
from pydantic import PrivateAttr
3943

44+
from mysql.ai.utils import atomic_transaction, execute_sql, format_value_sql
4045
from mysql.connector.abstracts import MySQLConnectionAbstract
4146

4247

@@ -50,7 +55,8 @@ class MyLLM(LLM):
5055
agentic queries.
5156
5257
Attributes:
53-
_db_connection (MySQLConnectionAbstract): The underlying MySQL connector database connection.
58+
_db_connection (MySQLConnectionAbstract):
59+
Underlying MySQL connector database connection.
5460
"""
5561

5662
_db_connection: MySQLConnectionAbstract = PrivateAttr()
@@ -138,7 +144,8 @@ def _identifying_params(self) -> dict:
138144
Return a dictionary of params that uniquely identify this LLM instance.
139145
140146
Returns:
141-
dict: Dictionary of identifier parameters (should include model_name for tracing/caching).
147+
dict: Dictionary of identifier parameters (should include
148+
model_name for tracing/caching).
142149
"""
143150
return {
144151
"model_name": "mysql_heatwave_llm",

mysql-connector-python/lib/mysql/ai/genai/vector_store.py

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@
2626
# along with this program; if not, write to the Free Software Foundation, Inc.,
2727
# 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2828

29+
"""MySQL-backed vector store for embeddings and semantic document retrieval.
30+
31+
Provides a VectorStore implementation persisting documents, metadata, and
32+
embeddings in MySQL, plus similarity search utilities.
33+
"""
34+
2935
import json
3036

3137
from typing import Any, Iterable, List, Optional, Sequence, Union
@@ -35,6 +41,8 @@
3541
from langchain_core.documents import Document
3642
from langchain_core.embeddings import Embeddings
3743
from langchain_core.vectorstores import VectorStore
44+
from pydantic import PrivateAttr
45+
3846
from mysql.ai.genai.embedding import MyEmbeddings
3947
from mysql.ai.utils import (
4048
VAR_NAME_SPACE,
@@ -48,9 +56,7 @@
4856
source_schema,
4957
table_exists,
5058
)
51-
5259
from mysql.connector.abstracts import MySQLConnectionAbstract
53-
from pydantic import PrivateAttr
5460

5561
BASIC_EMBEDDING_QUERY = "Hello world!"
5662
EMBEDDING_SOURCE = "external_source"
@@ -78,7 +84,8 @@ class MyVectorStore(VectorStore):
7884
db_connection (MySQLConnectionAbstract): Active MySQL database connection.
7985
embedder (Embeddings): Embeddings generator for computing vector representations.
8086
schema_name (str): SQL schema for table storage.
81-
table_name (Optional[str]): Name of the active table backing the store (or None until created).
87+
table_name (Optional[str]): Name of the active table backing the store
88+
(or None until created).
8289
embedding_dimension (int): Size of embedding vectors stored.
8390
next_id (int): Internal counter for unique document ID generation.
8491
"""
@@ -116,8 +123,11 @@ def __init__(
116123
self._db_connection = db_connection
117124
self._table_name: Optional[str] = None
118125

119-
# Embedding dimension determined using an example call. Assumes embeddings have fixed length.
120-
self._embedding_dimension = len(self._embedder.embed_query(BASIC_EMBEDDING_QUERY))
126+
# Embedding dimension determined using an example call.
127+
# Assumes embeddings have fixed length.
128+
self._embedding_dimension = len(
129+
self._embedder.embed_query(BASIC_EMBEDDING_QUERY)
130+
)
121131

122132
def _get_ids(self, num_ids: int) -> list[str]:
123133
"""
@@ -174,7 +184,7 @@ def _make_vector_store(self) -> None:
174184

175185
self._table_name = table_name
176186

177-
def delete(self, ids: Optional[Sequence[str]] = None, **kwargs: Any) -> None:
187+
def delete(self, ids: Optional[Sequence[str]] = None, **_: Any) -> None:
178188
"""
179189
Delete documents by ID. Optionally deletes the vector table if empty after deletions.
180190
@@ -190,7 +200,8 @@ def delete(self, ids: Optional[Sequence[str]] = None, **kwargs: Any) -> None:
190200
If an operational error occurs during execution.
191201
192202
Notes:
193-
If the backing table is empty after deletions, the table is dropped and table_name is set to None.
203+
If the backing table is empty after deletions, the table is dropped and
204+
table_name is set to None.
194205
"""
195206
with atomic_transaction(self._db_connection) as cursor:
196207
if ids:
@@ -221,7 +232,7 @@ def add_texts(
221232
texts: Iterable[str],
222233
metadatas: Optional[list[dict]] = None,
223234
ids: Optional[List[str]] = None,
224-
**kwargs: dict,
235+
**_: dict,
225236
) -> List[str]:
226237
"""
227238
Add a batch of text strings and corresponding metadata to the vector store.
@@ -312,7 +323,7 @@ def add_documents(
312323
"""
313324
if ids and len(ids) != len(documents):
314325
msg = (
315-
f"ids must be the same length as documents. "
326+
"ids must be the same length as documents. "
316327
f"Got {len(ids)} ids and {len(documents)} documents."
317328
)
318329
raise ValueError(msg)
@@ -357,7 +368,8 @@ def similarity_search(
357368
Args:
358369
query: String query to embed and use for similarity search.
359370
k: Number of top documents to return.
360-
kwargs: options to pass to ML_SIMILARITY_SEARCH. Currently supports distance_metric, max_distance, percentage_distance, and segment_overlap
371+
kwargs: options to pass to ML_SIMILARITY_SEARCH. Currently supports
372+
distance_metric, max_distance, percentage_distance, and segment_overlap
361373
362374
Returns:
363375
List of Document objects, ordered from most to least similar.
@@ -399,8 +411,14 @@ def similarity_search(
399411
similarity_search_query = f"""
400412
CALL sys.ML_SIMILARITY_SEARCH(
401413
@{VAR_EMBEDDING},
402-
JSON_ARRAY('{self._schema_name}.{self._table_name}'),
403-
JSON_OBJECT("segment", "content", "segment_embedding", "embed", "document_name", "id"),
414+
JSON_ARRAY(
415+
'{self._schema_name}.{self._table_name}'
416+
),
417+
JSON_OBJECT(
418+
"segment", "content",
419+
"segment_embedding", "embed",
420+
"document_name", "id"
421+
),
404422
{k},
405423
%s,
406424
NULL,
@@ -425,13 +443,17 @@ def similarity_search(
425443
for context in context_maps:
426444
execute_sql(
427445
cursor,
428-
f"SELECT id, content, metadata FROM {self._schema_name}.{self._table_name} WHERE id =%s",
446+
(
447+
"SELECT id, content, metadata "
448+
f"FROM {self._schema_name}.{self._table_name} "
449+
"WHERE id = %s"
450+
),
429451
params=(context["document_name"],),
430452
)
431-
id, content, metadata = cursor.fetchone()
453+
doc_id, content, metadata = cursor.fetchone()
432454

433455
doc_args = {
434-
"id": id,
456+
"id": doc_id,
435457
"page_content": content,
436458
}
437459
if metadata is not None:
@@ -450,8 +472,10 @@ def __enter__(self) -> "VectorStore":
450472
The current MyVectorStore object, allowing use within a `with` statement block.
451473
452474
Usage Notes:
453-
- Intended for use in a `with` statement to ensure automatic cleanup of resources.
454-
- No special initialization occurs during context entry, but enables proper context-managed lifecycle.
475+
- Intended for use in a `with` statement to ensure automatic
476+
cleanup of resources.
477+
- No special initialization occurs during context entry, but enables
478+
proper context-managed lifecycle.
455479
456480
Example:
457481
with MyVectorStore(db_connection, embedder) as vectorstore:
@@ -468,7 +492,8 @@ def __exit__(
468492
exc_tb: Union[object, None],
469493
) -> None:
470494
"""
471-
Exit the runtime context for the vector store, ensuring all storage resources are cleaned up.
495+
Exit the runtime context for the vector store, ensuring all storage
496+
resources are cleaned up.
472497
473498
Args:
474499
exc_type: The exception type, if any exception occurred in the context block.

mysql-connector-python/lib/mysql/ai/ml/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@
2626
# along with this program; if not, write to the Free Software Foundation, Inc.,
2727
# 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2828

29+
"""ML package for MySQL Connector/Python.
30+
31+
Performs optional dependency checks and exposes ML utilities:
32+
- ML_TASK, MyModel
33+
- MyClassifier, MyRegressor, MyGenericTransformer
34+
- MyAnomalyDetector
35+
"""
2936
from mysql.ai.utils import check_dependencies as _check_dependencies
3037

3138
_check_dependencies(["ML"])

mysql-connector-python/lib/mysql/ai/ml/base.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,19 @@
2626
# along with this program; if not, write to the Free Software Foundation, Inc.,
2727
# 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2828

29+
"""Base classes for MySQL HeatWave ML estimators for Connector/Python.
30+
31+
Implements a scikit-learn-compatible base estimator wrapping server-side ML.
32+
"""
2933
from typing import Optional, Union
3034

3135
import pandas as pd
3236

33-
from mysql.ai.ml.model import ML_TASK, MyModel
34-
from mysql.ai.utils import copy_dict
3537
from sklearn.base import BaseEstimator
3638

3739
from mysql.connector.abstracts import MySQLConnectionAbstract
40+
from mysql.ai.ml.model import ML_TASK, MyModel
41+
from mysql.ai.utils import copy_dict
3842

3943

4044
class MyBaseMLModel(BaseEstimator):
@@ -80,7 +84,11 @@ def __init__(
8084
self._model = MyModel(db_connection, task=task, model_name=model_name)
8185
self.fit_extra_options = copy_dict(fit_extra_options)
8286

83-
def fit(self, X: pd.DataFrame, y: Optional[pd.DataFrame] = None) -> "MyBaseMLModel":
87+
def fit(
88+
self,
89+
X: pd.DataFrame, # pylint: disable=invalid-name
90+
y: Optional[pd.DataFrame] = None,
91+
) -> "MyBaseMLModel":
8492
"""
8593
Fit the underlying ML model using pandas DataFrames.
8694
Delegates to MyMLModelPandasHelper.fit.
@@ -120,7 +128,8 @@ def _delete_model(self) -> bool:
120128

121129
def get_model_info(self) -> Optional[dict]:
122130
"""
123-
Checks if the model name is available. Model info will only be present in the catalog if the model has previously been fitted.
131+
Checks if the model name is available. Model info will only be present in the
132+
catalog if the model has previously been fitted.
124133
125134
Returns:
126135
True if the model name is not part of the model catalog

0 commit comments

Comments
 (0)