
    K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dS )
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_connectionNdb_connectionoptionsc                     t                                                       || _        t          |          | _        |pi }t          |          \  | _        | _        dS )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      T/var/www/html/analyses/venv/lib/python3.11/site-packages/mysql/ai/genai/embedding.pyr   zMyEmbeddings.__init__F   sZ    0 	+(77-R8H8Q8Q5 $"5"5"5    textsreturnc           	      $   |sg S t          j        t          t          |                    |d          }t	          | j                  5 }t          | j                  5 }t          || j        |          \  }}|	                    | j        |f           d| d| d| j
         d}t          ||| j                   t          || j        |          }|d                                                                         st          d |d         D                       rt!          d	          |d                                         }	d
 |	D             }	|	cddd           cddd           S # 1 swxY w Y   ddd           dS # 1 swxY w Y   dS )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 V  	d S N .0es     r   	<genexpr>z/MyEmbeddings.embed_documents.<locals>.<genexpr>   s?       A AT	A A A A A Ar   z:Failure to generate embeddings for one or more text entry.c                 ,    g | ]}t          |          S r*   )listr+   s     r   
<listcomp>z0MyEmbeddings.embed_documents.<locals>.<listcomp>   s    666a$q''666r   N)pd	DataFramerangelenr   r   r   r   r   appendr   r	   r   r   isnullany
ValueErrortolist)
r   r   dfcursortemporary_tablesqualified_table_name
table_nameembed_querydf_embeddingsr'   s
             r   embed_documentszMyEmbeddings.embed_documentsd   s   8  	I\s5zz!2!2EBBCC t233!	7= !455!	9I/@("0 0, * ##T%5z$BCCC( (  +    D4GHHHH ,FD4DjQQM\*11337799 S A A#0#>A A A > >  !P  
 '|4;;==J66:666JC!	 !	 !	 !	 !	 !	 !	 !	 !	 !	 !	 !	 !	 !	 !	 !	 !	 !	 !	 !	 !	 !	 !	 !	 !	 !	 !	 !	 !	 !	 !	 !	 !	 !	 !	 !	 !	 !	 !	 !	 !	s7   
FC5E-F-E1	1F4E1	5FF	F	r#   c                     t          | j                  5 }t          |d| j         d|g| j        R            t          |                                d                   cddd           S # 1 swxY w Y   dS )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   r0   fetchone)r   r#   r<   s      r   r@   zMyEmbeddings.embed_query   s    .   344 	.L1ILLL3t233   
 ))!,--	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	.s   AA--A14A1r)   )__name__
__module____qualname____doc__r   r   r   __annotations__r   r   r   r   strfloatrB   r@   __classcell__)r   s   @r   r   r   6   s           /:kmmN+;;; QUR R4R?G~R R R R R R<BT#Y B4U3D B B B BH. .U . . . . . . . .r   r   )rH   typingr   r   r   pandasr2   langchain_core.embeddingsr   pydanticr   mysql.ai.utilsr   r	   r
   r   r   r   r   mysql.connector.abstractsr   r   r*   r   r   <module>rS      s  :  ( ' ' ' ' ' ' ' ' '     0 0 0 0 0 0                              > = = = = =O. O. O. O. O.: O. O. O. O. O.r   