
    dh%                     r    S SK JrJrJrJr  S SKJr  S SKJr  S SK	J
r
Jr   " S S\
\5      r " S S\5      rg	)
    )AnyDictListOptional)
Embeddings)get_from_dict_or_env)	BaseModelmodel_validatorc                   V   \ rS rSr% Sr\\S'   Sr\\S'    Sr	\
\   \S'    Sr\\S	'    Sr\
\   \S
'    Sr\\S'    Sr\
\   \S'    Sr\\S'    Sr\
\   \S'    Sr\\S'    Sr\\S'    Sr\\S'    \" SS9\S\S\4S j5       5       rS\\   S\\\      4S jrS\S\\   4S jrSrg)%AlephAlphaAsymmetricSemanticEmbedding   a  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           	          [        USS5      n SSKJn  U" UUS   US   US   US   US	   S
9US'   U$ ! [         a    [        S5      ef = f)z?Validate that api key and python package exists in environment.r   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    s       b/var/www/html/shao/venv/lib/python3.13/site-packages/langchain_community/embeddings/aleph_alpha.pyvalidate_environment:AlephAlphaAsymmetricSemanticEmbedding.validate_environmentS   s     3)+@
	1%)F^y)(./H(I$_5F^ F8   	K 	s	   #4 A
textsc                     SSK JnJnJn  / nU H  nUR                  U5      UR                  U R                  U R                  U R                  U R                  S.nU" S0 UD6nU R                  R                  XR                  S9n	UR                  U	R                  5        M     U$ ! [         a    [	        S5      ef = f)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"   prompt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textdocument_paramsdocument_requestdocument_responses
             r&   embed_documents5AlephAlphaAsymmetricSemanticEmbedding.embed_documentsm   s    
	  !D **40"8"A"A$($9$9!^^040Q0Q(,(A(AO  8J/J $ : :(

 !; !  &&'8'B'BC " #"1  	K 	s   
B) )B?r<   c                 L    SSK JnJnJn  UR                  U5      UR                  U R                  U R                  U R                  U R                  S.nU" S0 UD6nU R                  R                  X`R                  S9nUR                  $ ! [         a    [	        S5      ef = f)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/   r2   r4   )r#   r,   r-   r.   r$   r5   Queryr   r   r   r   r   r7   r   r9   )r:   r<   r,   r-   r.   symmetric_paramssymmetric_requestsymmetric_responses           r&   embed_query1AlephAlphaAsymmetricSemanticEmbedding.embed_query   s    
	  &&t,4:: $ 5 5,0,M,M$($=$=
 5H7GH![[77%ZZ 8 
 "+++'  	K 	   
B B#r4   )__name__
__module____qualname____firstlineno____doc__r   __annotations__r   strr   r   intr   boolr   r   r   r   r   r   r   r   r
   classmethodr   r'   r   floatr@   rH   __static_attributes__r4       r&   r   r      s4   . K !E3 &*hsm*.It226 (3-6+!%$% *.#-&-D#-9!GXc]!4 $'S&W M3 D$> (#$ 3   $0'#T#Y '#4U3D '#R!, !,U !,rW   r   c                   l    \ rS rSrSrS\S\\   4S jrS\\   S\\\      4S jr	S\S\\   4S jr
S	rg
)$AlephAlphaSymmetricSemanticEmbedding   a'  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)
r<   r   c                 L    SSK JnJnJn  UR                  U5      UR                  U R                  U R                  U R                  U R                  S.nU" S0 UD6nU R                  R                  X`R                  S9nUR                  $ ! [         a    [	        S5      ef = f)Nr   r+   r"   r/   r2   r4   )r#   r,   r-   r.   r$   r5   	Symmetricr   r   r   r   r   r7   r   r9   )r:   r<   r,   r-   r.   query_paramsquery_requestquery_responses           r&   _embed+AlephAlphaSymmetricSemanticEmbedding._embed   s    
	  &&t,4>> $ 5 5,0,M,M$($=$=
 1@<@33! 4 
 ''''  	K 	rJ   r)   c                 \    / nU H#  nUR                  U R                  U5      5        M%     U$ )zCall out to Aleph Alpha's Document endpoint.

Args:
    texts: The list of texts to embed.

Returns:
    List of embeddings, one for each text.
)r8   r`   )r:   r)   r;   r<   s       r&   r@   4AlephAlphaSymmetricSemanticEmbedding.embed_documents   s1     !D&&t{{4'89 ""rW   c                 $    U R                  U5      $ )rC   )r`   )r:   r<   s     r&   rH   0AlephAlphaSymmetricSemanticEmbedding.embed_query   s     {{4  rW   r4   N)rK   rL   rM   rN   rO   rQ   r   rU   r`   r@   rH   rV   r4   rW   r&   rY   rY      sU    $(3 (4; (8#T#Y #4U3D #! !U !rW   rY   N)typingr   r   r   r   langchain_core.embeddingsr   langchain_core.utilsr   pydanticr	   r
   r   rY   r4   rW   r&   <module>rj      s4    , , 0 5 /o,Iz o,dF!+P F!rW   