o
    Zh%                     @   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dedefddZdee deee  fddZdedee f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   sb   t |dd}zddlm} |||d |d |d |d |d	 d
|d< W |S  ty0   tdw )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"   a/var/www/html/lang_env/lib/python3.10/site-packages/langchain_community/embeddings/aleph_alpha.pyvalidate_environmentS   s&   z:AlephAlphaAsymmetricSemanticEmbedding.validate_environmenttextsc           
      C   s   zddl m}m}m} W n ty   tdw g }|D ])}|||j| j| j| j	| j
d}|d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   Nr"   )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_documentsr4   c                 C   sx   zddl m}m}m} W n ty   tdw |||j| j| j| j	| j
d}|d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,   Nr"   )r   r'   r(   r)   r    r.   ZQueryr   r   r   r   r   r/   r   r1   )r2   r4   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   floatr5   r7   r"   r"   r"   r#   r
      s<   
 	)r
   c                   @   sZ   e Zd ZdZdedee fddZdee deee  fddZdedee f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)
    r4   r   c                 C   sx   zddl m}m}m} W n ty   tdw |||j| j| j| j	| j
d}|di |}| jj|| jd}|jS )Nr   r&   r   r*   r,   r"   )r   r'   r(   r)   r    r.   Z	Symmetricr   r   r   r   r   r/   r   r1   )r2   r4   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.
        )r0   rC   )r2   r%   r3   r4   r"   r"   r#   r5      s   	z4AlephAlphaSymmetricSemanticEmbedding.embed_documentsc                 C   s
   |  |S )r6   )rC   )r2   r4   r"   r"   r#   r7      s   
z0AlephAlphaSymmetricSemanticEmbedding.embed_queryN)
r8   r9   r:   r;   r=   r   rA   rC   r5   r7   r"   r"   r"   r#   rB      s
    rB   N)typingr   r   r   r   Zlangchain_core.embeddingsr   Zlangchain_core.utilsr   Zpydanticr   r	   r
   rB   r"   r"   r"   r#   <module>   s     3