a
    bg%                     @   sf   d dl mZmZmZmZ d dlmZ d dlmZ d dl	m
Z
mZ G dd de
eZG dd deZd	S )
    )AnyDictListOptional)
Embeddings)get_from_dict_or_env)	BaseModelmodel_validatorc                   @   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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before)mode)valuesreturnc                 C   sh   t |dd}z<ddlm} |||d |d |d |d |d	 d
|d< W n tyb   tdY n0 |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"   x/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/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y2   tdY n0 g }|D ]R}|||j| j| j| j	| j
d}|f i |}| 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_textZ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_documentsm   s*    	
	z5AlephAlphaAsymmetricSemanticEmbedding.embed_documentsr5   r   c                 C   s|   zddl m}m}m} W n ty2   tdY n0 |||j| j| j| j	| j
d}|f i |}| 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	   classmethodr   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   s|   zddl m}m}m} W n ty2   tdY n0 |||j| j| j| j	| j
d}|f i |}| 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   rE   )r3   r&   r4   r5   r"   r"   r#   r6      s    	z4AlephAlphaSymmetricSemanticEmbedding.embed_documentsc                 C   s
   |  |S )r8   )rE   )r3   r5   r"   r"   r#   r9      s    z0AlephAlphaSymmetricSemanticEmbedding.embed_queryN)
r:   r;   r<   r=   r?   r   rC   rE   r6   r9   r"   r"   r"   r#   rD      s   rD   N)typingr   r   r   r   Zlangchain_core.embeddingsr   Zlangchain_core.utilsr   Zpydanticr   r	   r
   rD   r"   r"   r"   r#   <module>   s    3