U
    h                     @   sf   d dl mZmZmZmZmZmZ d dl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dS )    )AnyDictListMappingOptionalTupleN)
Embeddings)	BaseModelroot_validatorget_from_dict_or_envc                   @   s   e Zd ZU dZdZeed< dZeed< dZeed< dZ	e
ed	< d
Zee ed< G dd dZeddeedddZeeeef dddZd eeeef  e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 )!MosaicMLInstructorEmbeddingsaa  MosaicML embedding service.

    To use, you should have the
    environment variable ``MOSAICML_API_TOKEN`` set with your API token, or pass
    it as a named parameter to the constructor.

    Example:
        .. code-block:: python

            from langchain_community.llms import MosaicMLInstructorEmbeddings
            endpoint_url = (
                "https://models.hosted-on.mosaicml.hosting/instructor-large/v1/predict"
            )
            mosaic_llm = MosaicMLInstructorEmbeddings(
                endpoint_url=endpoint_url,
                mosaicml_api_token="my-api-key"
            )
    zBhttps://models.hosted-on.mosaicml.hosting/instructor-xl/v1/predictendpoint_urlz&Represent the document for retrieval: embed_instructionz<Represent the question for retrieving supporting documents: query_instructiong      ?retry_sleepNmosaicml_api_tokenc                   @   s   e Zd ZdZdS )z#MosaicMLInstructorEmbeddings.ConfigZforbidN)__name__
__module____qualname__extra r   r   K/tmp/pip-unpacked-wheel-9gdii04g/langchain_community/embeddings/mosaicml.pyConfig,   s   r   T)pre)valuesreturnc                 C   s   t |dd}||d< |S )z?Validate that api key and python package exists in environment.r   ZMOSAICML_API_TOKENr   )clsr   r   r   r   r   validate_environment/   s      z1MosaicMLInstructorEmbeddings.validate_environment)r   c                 C   s
   d| j iS )zGet the identifying parameters.r   )r   selfr   r   r   _identifying_params8   s    z0MosaicMLInstructorEmbeddings._identifying_paramsF)inputis_retryr   c              
   C   sn  d|i}| j  dd}ztj| j||d}W n4 tjjk
r` } ztd| W 5 d }~X Y nX z|jdkr|sdd l}|	| j
 | j|dd	W S td
|j | }t|trdddg}	|	D ]}
|
|kr||
 } qqtd| t|trt|d tr|}n|g}ntd| W n> tjjk
rh } ztd| d|j W 5 d }~X Y nX |S )Ninputszapplication/json)AuthorizationzContent-Type)headersjsonz$Error raised by inference endpoint: i  r   T)r#   z>Error raised by inference API: rate limit exceeded.
Response: dataoutputoutputsz#No key data or output in response: zUnexpected response type: zError raised by inference API: z.
Response: )r   requestspostr   
exceptionsRequestException
ValueErrorstatus_codetimesleepr   _embedtextr'   
isinstancedictlistJSONDecodeError)r    r"   r#   payloadr&   responseer1   Zparsed_responseZoutput_keyskeyZoutput_item
embeddingsr   r   r   r3   =   sH     


z#MosaicMLInstructorEmbeddings._embed)textsr   c                    s     fdd|D }  |}|S )zEmbed documents using a MosaicML deployed instructor embedding model.

        Args:
            texts: The list of texts to embed.

        Returns:
            List of embeddings, one for each text.
        c                    s   g | ]} j |fqS r   )r   ).0r4   r   r   r   
<listcomp>   s     z@MosaicMLInstructorEmbeddings.embed_documents.<locals>.<listcomp>)r3   )r    r>   Zinstruction_pairsr=   r   r   r   embed_documentsy   s    	
z,MosaicMLInstructorEmbeddings.embed_documents)r4   r   c                 C   s   | j |f}| |gd }|S )zEmbed a query using a MosaicML deployed instructor embedding model.

        Args:
            text: The text to embed.

        Returns:
            Embeddings for the text.
        r   )r   r3   )r    r4   Zinstruction_pairZ	embeddingr   r   r   embed_query   s    	
z(MosaicMLInstructorEmbeddings.embed_query)F)r   r   r   __doc__r   str__annotations__r   r   r   floatr   r   r   r
   r   r   propertyr   r   r!   r   r   boolr3   rA   rB   r   r   r   r   r   	   s*   


  
<r   )typingr   r   r   r   r   r   r+   Zlangchain_core.embeddingsr   Zlangchain_core.pydantic_v1r	   r
   Zlangchain_core.utilsr   r   r   r   r   r   <module>   s
    