U
    hd                     @   s   d dl Z d dlmZ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mZmZ d dlmZmZmZ d dlmZ d dlmZ e eZG d	d
 d
e
eZdS )    N)AnyDictListMappingOptional)CallbackManagerForLLMRun)LLM)	BaseModelField	SecretStrroot_validator)convert_to_secret_strget_from_dict_or_envpre_init)
get_fields)enforce_stop_tokensc                   @   s   e Zd ZU dZdZeed< eedZ	e
eef ed< dZee ed< G dd	 d	Zed
de
eef e
eef dddZee
e
dddZeeeef dddZeedddZdeeee  ee eedddZdS )
PipelineAIa  PipelineAI large language models.

    To use, you should have the ``pipeline-ai`` python package installed,
    and the environment variable ``PIPELINE_API_KEY`` set with your API key.

    Any parameters that are valid to be passed to the call can be passed
    in, even if not explicitly saved on this class.

    Example:
        .. code-block:: python

            from langchain_community.llms import PipelineAI
            pipeline = PipelineAI(pipeline_key="")
     pipeline_key)default_factorypipeline_kwargsNpipeline_api_keyc                   @   s   e Zd ZdZdS )zPipelineAI.ConfigZforbidN)__name__
__module____qualname__extra r   r   G/tmp/pip-unpacked-wheel-9gdii04g/langchain_community/llms/pipelineai.pyConfig-   s   r   T)pre)valuesreturnc                 C   s   dd t |  D }|di }t|D ]H}||kr*||krNtd| dt| d| d ||||< q*||d< |S )z>Build extra kwargs from additional params that were passed in.c                 S   s   h | ]
}|j qS r   )alias).0fieldr   r   r   	<setcomp>3   s     z)PipelineAI.build_extra.<locals>.<setcomp>r   zFound z supplied twice.zM was transferred to pipeline_kwargs.
                    Please confirm that z is what you intended.)r   r    getlist
ValueErrorloggerwarningpop)clsr    Zall_required_field_namesr   
field_namer   r   r   build_extra0   s    zPipelineAI.build_extrac                 C   s   t t|dd}||d< |S )z?Validate that api key and python package exists in environment.r   ZPIPELINE_API_KEY)r   r   )r,   r    r   r   r   r   validate_environmentB   s
    
zPipelineAI.validate_environment)r!   c                 C   s   d| j id| jiS )zGet the identifying parameters.r   r   )r   r   selfr   r   r   _identifying_paramsK   s    zPipelineAI._identifying_paramsc                 C   s   dS )zReturn type of llm.Zpipeline_air   r0   r   r   r   	_llm_typeS   s    zPipelineAI._llm_type)promptstoprun_managerkwargsr!   c           
      K   s   zddl m} W n tk
r,   tdY nX || j d}| jpFi }||}|| j||g}z|jd d }	W n" t	k
r   t	d| Y nX |dk	rt
|	|}	|	S )z Call to Pipeline Cloud endpoint.r   )PipelineCloudz^Could not import pipeline-ai python package. Please install it with `pip install pipeline-ai`.)tokenzAA pipeline run should have a `result_preview` attribute.Run was: N)Zpipeliner8   ImportErrorr   Zget_secret_valuer   Zrun_pipeliner   Zresult_previewAttributeErrorr   )
r1   r4   r5   r6   r7   r8   clientparamsruntextr   r   r   _callX   s&    



zPipelineAI._call)NN)r   r   r   __doc__r   str__annotations__r
   dictr   r   r   r   r   r   r   r   r.   r   r/   propertyr   r2   r3   r   r   r@   r   r   r   r   r      s,   
"  
r   )loggingtypingr   r   r   r   r   Zlangchain_core.callbacksr   Z#langchain_core.language_models.llmsr   Zlangchain_core.pydantic_v1r	   r
   r   r   Zlangchain_core.utilsr   r   r   Zlangchain_core.utils.pydanticr   Zlangchain_community.llms.utilsr   	getLoggerr   r)   r   r   r   r   r   <module>   s   
