o
    U|%i                     @   s|   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	 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                       sv   e Zd ZU dZe Zeed< 	ddedee	 f fddZ
dee d	eee  fd
dZded	ee fddZ  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                    s6   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__ U/home/air/sos_test/back/venv/lib/python3.10/site-packages/mysql/ai/genai/embedding.pyr   F   s
   

zMyEmbeddings.__init__textsreturnc           
   	   C   s.  |sg S t tt||d}t| jx}t| jb}t|| j|\}}|	| j|f d| d| d| j
 d}t||| jd t|| j|}|d   s\tdd	 |d D r`td
|d  }	dd |	D }	|	W  d   W  d   S 1 sw   Y  W d   dS 1 sw   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                 s   s    | ]}|d u V  qd S Nr   .0er   r   r   	<genexpr>   s    
z/MyEmbeddings.embed_documents.<locals>.<genexpr>z:Failure to generate embeddings for one or more text entry.c                 S   s   g | ]}t |qS r   )listr&   r   r   r   
<listcomp>   s    z0MyEmbeddings.embed_documents.<locals>.<listcomp>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$   r   r   r   embed_documentsd   sB   RzMyEmbeddings.embed_documentsr    c                 C   s`   t | j!}t|d| j d|g| jR d t| d W  d   S 1 s)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   r*   fetchone)r   r    r6   r   r   r   r:      s   $zMyEmbeddings.embed_queryr%   )__name__
__module____qualname____doc__r   r   r   __annotations__r   r   r   r   strfloatr<   r:   __classcell__r   r   r   r   r   6   s   
 Dr   )rA   typingr   r   r   pandasr,   langchain_core.embeddingsr   pydanticr   mysql.ai.utilsr   r   r	   r
   r   r   r   mysql.connector.abstractsr   r   r   r   r   r   <module>   s   $	