
    7|h                     p    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 d dlmZmZmZ  G d dee	      Zy)    )AnyDictListMappingOptionalTupleN)
Embeddingsget_from_dict_or_env)	BaseModel
ConfigDictmodel_validatorc            	       6   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<    ed      Z ed      ededefd              Zedeeef   fd       Z	 ddeeeef      dedeee
      fdZdee   deee
      fdZdedee
   fdZy
)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_tokenforbid)extrabefore)modevaluesreturnc                 *    t        |dd      }||d<   |S )z?Validate that api key and python package exists in environment.r   MOSAICML_API_TOKENr
   )clsr   r   s      f/var/www/html/test/engine/venv/lib/python3.12/site-packages/langchain_community/embeddings/mosaicml.pyvalidate_environmentz1MosaicMLInstructorEmbeddings.validate_environment0   s*     2(*>
 (:#$    c                     d| j                   iS )zGet the identifying parameters.r   )r   )selfs    r   _identifying_paramsz0MosaicMLInstructorEmbeddings._identifying_params:   s      1 122r!   inputis_retryc                    d|i}| j                    dd}	 t        j                  | j                  ||      }	 |j                  dk(  rL|s2dd l}|j                  | j                         | j                  |d	      S t        d
|j                         |j                         }t        |t              rQg d}	|	D ]  }
|
|v s||
   } n t        d|       t        |t               rt        |d   t               r|}|S |g}	 |S t        d|       # t        j                  j
                  $ r}t        d|       d }~ww xY w# t        j                  j"                  $ r }t        d| d|j                         d }~ww xY w)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er5   parsed_responseoutput_keyskeyoutput_item
embeddingss                r   r7   z#MosaicMLInstructorEmbeddings._embed?   s    U# !% 7 78.
	I}}T%6%6gVH'	##s*JJt//0;;ut;<< U}}o' 
 'mmoO /40;& Co-&5c&:
 %=o=NO  k40ZAPT5U!,J  #.J  !#=o=N!OPPK ""33 	ICA3GHH	IN ""22 	1!M(--Q 	sH   "D AE <AE ;E ?E E E0D>>EF#E>>Ftextsc                 h    |D cg c]  }| j                   |f }}| j                  |      }|S c c}w )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.
        )r   r7   )r#   rE   r8   instruction_pairsrD   s        r   embed_documentsz,MosaicMLInstructorEmbeddings.embed_documents{   s@     INNd44d;NN[[!23
 Os   /r8   c                 L    | j                   |f}| j                  |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   r7   )r#   r8   instruction_pair	embeddings       r   embed_queryz(MosaicMLInstructorEmbeddings.embed_query   s2     !22D9KK!1 23A6	r!   )F)__name__
__module____qualname____doc__r   str__annotations__r   r   r   floatr   r   r   model_configr   classmethodr   r   r    propertyr   r$   r   r   boolr7   rH   rL    r!   r   r   r   	   s-   ( 	M #  EsE.F s  /KE(,,L (#$ 3   $ 3WS#X%6 3 3
 >C:%S/*:6::	d5k	:xT#Y 4U3D  U r!   r   )typingr   r   r   r   r   r   r/   langchain_core.embeddingsr	   langchain_core.utilsr   pydanticr   r   r   r   rX   r!   r   <module>r]      s*    < <  0 5 ; ;J9j Jr!   