U
    h                     @   sf   d Z ddl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mZmZ G dd de	eZdS )	z#This file is for LLMRails Embedding    )DictListOptionalN)
Embeddings)	BaseModel	SecretStr)convert_to_secret_strget_from_dict_or_envpre_initc                   @   s   e Zd ZU dZdZeed< dZee	 ed< G dd 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 )LLMRailsEmbeddingsa  LLMRails embedding models.

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

    Model can be one of ["embedding-english-v1","embedding-multi-v1"]

    Example:
        .. code-block:: python

            from langchain_community.embeddings import LLMRailsEmbeddings
            cohere = LLMRailsEmbeddings(
                model="embedding-english-v1", api_key="my-api-key"
            )
    zembedding-english-v1modelNapi_keyc                   @   s   e Zd ZdZdS )zLLMRailsEmbeddings.ConfigZforbidN)__name__
__module____qualname__extra r   r   L/tmp/pip-unpacked-wheel-9gdii04g/langchain_community/embeddings/llm_rails.pyConfig#   s   r   )valuesreturnc                 C   s   t t|dd}||d< |S )z,Validate that api key exists in environment.r   ZLLM_RAILS_API_KEY)r   r	   )clsr   r   r   r   r   validate_environment&   s
    
z'LLMRailsEmbeddings.validate_environment)textsr   c                 C   s:   t jdd| j i|| jddd}dd | d D S )	zCall out to Cohere's embedding endpoint.

        Args:
            texts: The list of texts to embed.

        Returns:
            List of embeddings, one for each text.
        z&https://api.llmrails.com/v1/embeddingsz	X-API-KEY)inputr   <   )headersjsontimeoutc                 S   s   g | ]}|d  qS )Z	embeddingr   ).0itemr   r   r   
<listcomp>>   s     z6LLMRailsEmbeddings.embed_documents.<locals>.<listcomp>data)requestspostr   Zget_secret_valuer   r   )selfr   responser   r   r   embed_documents/   s    	
z"LLMRailsEmbeddings.embed_documents)textr   c                 C   s   |  |gd S )zCall out to Cohere's embedding endpoint.

        Args:
            text: The text to embed.

        Returns:
            Embeddings for the text.
        r   )r'   )r%   r(   r   r   r   embed_query@   s    	zLLMRailsEmbeddings.embed_query)r   r   r   __doc__r   str__annotations__r   r   r   r   r
   r   r   r   floatr'   r)   r   r   r   r   r      s   
r   )r*   typingr   r   r   r#   Zlangchain_core.embeddingsr   Zlangchain_core.pydantic_v1r   r   Zlangchain_core.utilsr   r	   r
   r   r   r   r   r   <module>   s   