hyb
2025-12-23 7e5db3a16b423ec4a43459805e277979bcac7db5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
Ë
Wñúh‰ãó‚—dZddlmZmZmZddlZddlmZddl    m
Z
ddl m Z m Z mZmZmZmZmZddlmZGd„d    e«Zy)
z«Embeddings integration utilities for MySQL Connector/Python.
 
Provides MyEmbeddings class to generate embeddings via MySQL HeatWave
using ML_EMBED_TABLE and ML_EMBED_ROW.
é)ÚDictÚListÚOptionalN)Ú
Embeddings)Ú PrivateAttr)Úatomic_transactionÚ execute_sqlÚformat_value_sqlÚ source_schemaÚsql_table_from_dfÚsql_table_to_dfÚtemporary_sql_tables)ÚMySQLConnectionAbstractc󈇗eZdZUdZe«Zeed<    d dedee    fˆfd„ Z
de e de e e fd„Zd    e de e fd
„ZˆxZS) Ú MyEmbeddingsa€
    Embedding generator class that uses a MySQL database to compute embeddings for input text.
 
    This class batches input text into temporary SQL tables, invokes MySQL's ML_EMBED_TABLE
    to generate embeddings, and retrieves the results as lists of floats.
 
    Attributes:
        _db_connection (MySQLConnectionAbstract): MySQL connection used for all database operations.
        schema_name (str): Name of the database schema to use.
        options_placeholder (str): SQL-ready placeholder string for ML_EMBED_TABLE options.
        options_params (dict): Dictionary of concrete option values to be passed as SQL parameters.
    Ú_db_connectionÚ db_connectionÚoptionsc󌕗t‰|«||_t|«|_|xsi}t |«\|_|_y)a“
        Initialize MyEmbeddings with a database connection and optional embedding parameters.
 
        References:
            https://dev.mysql.com/doc/heatwave/en/mys-hwgenai-ml-embed-row.html
                A full list of supported options can be found under "options"
 
        NOTE: The supported "options" are the intersection of the options provided in
            https://dev.mysql.com/doc/heatwave/en/mys-hwgenai-ml-embed-row.html
            https://dev.mysql.com/doc/heatwave/en/mys-hwgenai-ml-embed-table.html
 
        Args:
            db_connection: Active MySQL connector database connection.
            options: Optional dictionary of options for embedding operations.
 
        Raises:
            ValueError: If the schema name is not valid
            DatabaseError:
                If a database connection issue occurs.
                If an operational error occurs during execution.
        N)ÚsuperÚ__init__rr Ú schema_namer
Úoptions_placeholderÚoptions_params)ÚselfrrÚ    __class__s   €úKH:\Change_password\venv_build\Lib\site-packages\mysql/ai/genai/embedding.pyrzMyEmbeddings.__init__FsCø€ô0    ‰ÑÔØ+ˆÔÜ(¨Ó7ˆÔØ’-˜RˆÜ8HÈÓ8QÑ5ˆÔ  $Õ"5óÚtextsÚreturnc    óú—|sgStjtt|««|dœ«}t    |j
«5}t |j
«5}t||j|«\}}|j|j|f«d|›d|›d|j›d}t|||j¬«t||j|«}|dj«j«std„|dD««r t!d    «‚|dj#«}    |    D
cgc] }
t%|
«‘Œ}    }
|    cd
d
d
«cd
d
d
«Scc}
w#1swYnxYwd
d
d
«y
#1swYy
xYw) a†
        Generate embeddings for a list of input texts using the MySQL ML embedding procedure.
 
        References:
            https://dev.mysql.com/doc/heatwave/en/mys-hwgenai-ml-embed-table.html
 
        Args:
            texts: List of input strings to embed.
 
        Returns:
            List of lists of floats, with each inner list containing the embedding for a text.
 
        Raises:
            DatabaseError:
                If provided options are invalid or unsupported.
                If a database connection issue occurs.
                If an operational error occurs during execution.
            ValueError:
                If one or more text entries were unable to be embedded.
 
        Implementation notes:
            - Creates a temporary table to pass input text to the MySQL embedding service.
            - Adds a primary key to ensure results preserve input order.
            - Calls ML_EMBED_TABLE and fetches the resulting embeddings.
            - Deletes the temporary table after use to avoid polluting the database.
            - Embedding vectors are extracted from the "embeddings" column of the result table.
        )ÚidÚtextzCALL sys.ML_EMBED_TABLE('z    .text', 'z.embeddings', ú)©ÚparamsÚ
embeddingsc3ó$K—|]}|du–—Œ
y­w©N©)Ú.0Úes  rú    <genexpr>z/MyEmbeddings.embed_documents.<locals>.<genexpr>›sèø€òAؐT”    ñAùs‚z:Failure to generate embeddings for one or more text entry.N)ÚpdÚ    DataFrameÚrangeÚlenrrrr rÚappendrr    rr ÚisnullÚanyÚ
ValueErrorÚtolistÚlist) rrÚdfÚcursorÚtemporary_tablesÚqualified_table_nameÚ
table_nameÚ embed_queryÚ df_embeddingsr'r,s            rÚembed_documentszMyEmbeddings.embed_documentsds€ñ8؈Iä \‰\¤¤s¨5£zÓ!2¸EÑBÓ Cˆô ˜t×2Ñ2Ó 3ð!    Ø7=Ü   ×!4Ñ!4Ó 5ð!    à9Iä/@ؘ×(Ñ(¨"ó0Ñ ,Ð   *ð × #Ñ # T×%5Ñ%5°zÐ$BÔ CðØ(Ð)ð*Ø(Ð)¨Ø×+Ñ+Ð,Øð    ð ô ˜  °D×4GÑ4GÕ Hô,¨F°D×4DÑ4DÀjÓQˆMà˜\Ñ*×1Ñ1Ó3×7Ñ7Ô9¼SñAØ#0°Ñ#>ôAô>ô!ØPóðð
' |Ñ4×;Ñ;Ó=ˆJØ+5Ö6 aœ$˜q'Ð6ˆJÐ6à÷C!    ÷!    ò!    ùò>7÷?!    ð!    ú÷!    ÷!    ñ!    ús7ÁE1ÁCEÄ/EÅEÅ    E1ÅEÅE%    Å!E1Å1E:r#cóä—t|j«5}t|d|j›d|g|j¢­¬«t |j «d«cddd«S#1swYyxYw)a…
        Generate an embedding for a single text string.
 
        References:
            https://dev.mysql.com/doc/heatwave/en/mys-hwgenai-ml-embed-row.html
 
        Args:
            text: The input string to embed.
 
        Returns:
            List of floats representing the embedding vector.
 
        Raises:
            DatabaseError:
                If provided options are invalid or unsupported.
                If a database connection issue occurs.
                If an operational error occurs during execution.
 
        Example:
            >>> MyEmbeddings(db_conn).embed_query("Hello world")
            [0.1, 0.2, ...]
        zSELECT sys.ML_EMBED_ROW("%s", r$r%rN)rrr    rrr7Úfetchone)rr#r9s   rr=zMyEmbeddings.embed_query¨sn€ô.  × 3Ñ 3Ó 4ð    .¸Ü ØØ0°×1IÑ1IÐ0JÈ!ÐLØÐ3˜t×2Ñ2Ñ3õ ô
˜Ÿ™Ó)¨!Ñ,Ó-÷     .÷    .ò    .ús –AA&Á&A/r))Ú__name__Ú
__module__Ú __qualname__Ú__doc__rrrÚ__annotations__rrrrÚstrÚfloatr?r=Ú __classcell__)rs@rrr6suø…ñ ñ/:«m€NÐ+Ó;ðQUñRØ4ðRØ?Gȹ~õRð<B T¨#¡YðB°4¸¸U¹ Ñ3DóBðH. ð.¨¨U© ÷.rr)rEÚtypingrrrÚpandasr.Úlangchain_core.embeddingsrÚpydanticrÚmysql.ai.utilsrr    r
r r r rÚmysql.connector.abstractsrrr*rrú<module>rPs=ðñ:÷ (Ñ'ãå0Ý ÷÷ñõ>ôO.:õO.r