U
    h%                     @   sf   d dl mZmZmZmZ d dlmZ d dlmZm	Z	 d dl
mZ G dd deeZG dd deZd	S )
    )AnyDictListOptional)
Embeddings)	BaseModelroot_validator)get_from_dict_or_envc                   @   s   e Zd ZU dZeed< dZeed< dZe	e
 ed< dZeed< dZe	e
 ed	< d
Zeed< dZe	e ed< dZeed< dZe	e ed< dZe
ed< dZe
ed< dZeed< ed
deedddZee eee  dddZeee dddZdS )%AlephAlphaAsymmetricSemanticEmbeddinga6  Aleph Alpha's asymmetric semantic embedding.

    AA provides you with an endpoint to embed a document and a query.
    The models were optimized to make the embeddings of documents and
    the query for a document as similar as possible.
    To learn more, check out: https://docs.aleph-alpha.com/docs/tasks/semantic_embed/

    Example:
        .. code-block:: python
            from aleph_alpha import AlephAlphaAsymmetricSemanticEmbedding

            embeddings = AlephAlphaAsymmetricSemanticEmbedding(
                normalize=True, compress_to_size=128
            )

            document = "This is a content of the document"
            query = "What is the content of the document?"

            doc_result = embeddings.embed_documents([document])
            query_result = embeddings.embed_query(query)

    clientzluminous-basemodelNcompress_to_sizeF	normalizecontextual_control_thresholdTcontrol_log_additivealeph_alpha_api_keyzhttps://api.aleph-alpha.comhosthostingi1  request_timeout_seconds   total_retriesnice)pre)valuesreturnc                 C   sj   t |dd}z<ddlm} |||d |d |d |d |d	 d
|d< W n tk
rd   tdY nX |S )z?Validate that api key and python package exists in environment.r   ZALEPH_ALPHA_API_KEYr   )Clientr   r   r   r   r   )tokenr   r   r   r   r   r   lCould not import aleph_alpha_client python package. Please install it with `pip install aleph_alpha_client`.)r	   aleph_alpha_clientr   ImportError)clsr   r   r    r!   N/tmp/pip-unpacked-wheel-9gdii04g/langchain_community/embeddings/aleph_alpha.pyvalidate_environmentS   s(      
z:AlephAlphaAsymmetricSemanticEmbedding.validate_environmenttextsr   c           
      C   s   zddl m}m}m} W n tk
r4   tdY nX g }|D ]N}|||j| j| j| j	| j
d}|f |}| jj|| jd}	||	j q>|S )zCall out to Aleph Alpha's asymmetric Document endpoint.

        Args:
            texts: The list of texts to embed.

        Returns:
            List of embeddings, one for each text.
        r   PromptSemanticEmbeddingRequestSemanticRepresentationr   promptZrepresentationr   r   r   r   requestr   )r   r'   r(   r)   r   	from_textDocumentr   r   r   r   r   semantic_embedr   append	embedding)
selfr%   r'   r(   r)   document_embeddingstextZdocument_paramsZdocument_requestZdocument_responser!   r!   r"   embed_documentsl   s,    	
	
 z5AlephAlphaAsymmetricSemanticEmbedding.embed_documentsr5   r   c                 C   sz   zddl m}m}m} W n tk
r4   tdY nX |||j| j| j| j	| j
d}|f |}| jj|| jd}|jS )Call out to Aleph Alpha's asymmetric, query embedding endpoint
        Args:
            text: The text to embed.

        Returns:
            Embeddings for the text.
        r   r&   r   r*   r,   )r   r'   r(   r)   r   r.   ZQueryr   r   r   r   r   r0   r   r2   )r3   r5   r'   r(   r)   Zsymmetric_paramsZsymmetric_requestZsymmetric_responser!   r!   r"   embed_query   s&    
	
 z1AlephAlphaAsymmetricSemanticEmbedding.embed_query)__name__
__module____qualname____doc__r   __annotations__r   strr   r   intr   boolr   r   r   r   r   r   r   r   r   r   r#   r   floatr6   r9   r!   r!   r!   r"   r
      s"   

	)r
   c                   @   sT   e Zd ZdZeee dddZee eee  dddZeee ddd	Z	d
S )$AlephAlphaSymmetricSemanticEmbeddingaW  Symmetric version of the Aleph Alpha's semantic embeddings.

    The main difference is that here, both the documents and
    queries are embedded with a SemanticRepresentation.Symmetric
    Example:
        .. code-block:: python

            from aleph_alpha import AlephAlphaSymmetricSemanticEmbedding

            embeddings = AlephAlphaAsymmetricSemanticEmbedding(
                normalize=True, compress_to_size=128
            )
            text = "This is a test text"

            doc_result = embeddings.embed_documents([text])
            query_result = embeddings.embed_query(text)
    r7   c                 C   sz   zddl m}m}m} W n tk
r4   tdY nX |||j| j| j| j	| j
d}|f |}| jj|| jd}|jS )Nr   r&   r   r*   r,   )r   r'   r(   r)   r   r.   Z	Symmetricr   r   r   r   r   r0   r   r2   )r3   r5   r'   r(   r)   Zquery_paramsZquery_requestZquery_responser!   r!   r"   _embed   s&    
	
 z+AlephAlphaSymmetricSemanticEmbedding._embedr$   c                 C   s"   g }|D ]}| | | q|S )zCall out to Aleph Alpha's Document endpoint.

        Args:
            texts: The list of texts to embed.

        Returns:
            List of embeddings, one for each text.
        )r1   rD   )r3   r%   r4   r5   r!   r!   r"   r6      s    	z4AlephAlphaSymmetricSemanticEmbedding.embed_documentsc                 C   s
   |  |S )r8   )rD   )r3   r5   r!   r!   r"   r9      s    z0AlephAlphaSymmetricSemanticEmbedding.embed_queryN)
r:   r;   r<   r=   r?   r   rB   rD   r6   r9   r!   r!   r!   r"   rC      s   rC   N)typingr   r   r   r   Zlangchain_core.embeddingsr   Zlangchain_core.pydantic_v1r   r   Zlangchain_core.utilsr	   r
   rC   r!   r!   r!   r"   <module>   s    2