
    3i                         d Z ddlmZmZmZ ddlZddlmZ ddl	m
Z
 ddlmZmZmZmZmZmZmZ ddlmZ  G d d	e      Zy)
zEmbeddings 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                        e Zd ZU dZ e       Zeed<   	 ddedee	   f fdZ
dee   deee      fdZd	edee   fd
Z xZS )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        |      | _        |xs i }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__r   r   schema_namer
   options_placeholderoptions_params)selfr   r   	__class__s      =D:\jyotish\venv\Lib\site-packages\mysql/ai/genai/embedding.pyr   zMyEmbeddings.__init__F   sC    0 	+(7-R8H8Q5 $"5    textsreturnc           	         |sg S t        j                  t        t        |            |d      }t	        | j
                        5 }t        | j
                        5 }t        || j                  |      \  }}|j                  | j                  |f       d| d| d| j                   d}t        ||| j                         t        || j                  |      }|d   j                         j                         st        d |d   D              rt!        d	      |d   j#                         }	|	D 
cg c]  }
t%        |
       }	}
|	cd
d
d
       cd
d
d
       S c c}
w # 1 sw Y   nxY w	 d
d
d
       y
# 1 sw Y   y
xY w)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textzCALL sys.ML_EMBED_TABLE('z	.text', 'z.embeddings', )params
embeddingsc              3   $   K   | ]  }|d u  
 y wN ).0es     r   	<genexpr>z/MyEmbeddings.embed_documents.<locals>.<genexpr>   s      A#>aT	#>s   z:Failure to generate embeddings for one or more text entry.N)pd	DataFramerangelenr   r   r   r   r   appendr   r	   r   r   isnullany
ValueErrortolistlist)r   r   dfcursortemporary_tablesqualified_table_name
table_nameembed_querydf_embeddingsr'   r,   s              r   embed_documentszMyEmbeddings.embed_documentsd   s   8 I\\s5z!2EBC t223v !4!459I/@(("0, * ##T%5%5z$BC() *()++,	  D4G4GH ,FD4D4DjQM\*113779S A#0#>A > !P 
 '|4;;=J+56:a$q':J6? 65 43< 7; 655 433s7   E2CE/EE	E2EE%	!E22E;r#   c                     t        | j                        5 }t        |d| j                   d|g| j                         t        |j                         d         cddd       S # 1 sw Y   yxY w)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, ...]
        zSELECT sys.ML_EMBED_ROW("%s", r$   r%   r   N)r   r   r	   r   r   r7   fetchone)r   r#   r9   s      r   r=   zMyEmbeddings.embed_query   se    .   3 3401I1I0J!L3t223
 )!,- 544s   AA&&A/r)   )__name__
__module____qualname____doc__r   r   r   __annotations__r   r   r   r   strfloatr?   r=   __classcell__)r   s   @r   r   r   6   su     /:mN+; QUR4R?G~R<BT#Y B4U3D BH. .U .r   r   )rE   typingr   r   r   pandasr.   langchain_core.embeddingsr   pydanticr   mysql.ai.utilsr   r	   r
   r   r   r   r   mysql.connector.abstractsr   r   r*   r   r   <module>rP      s=   : ( '  0     >O.: O.r   