U
    h                     @  sv   d Z ddlmZ ddlmZmZmZ ddlmZ ddl	m
Z
 ddlmZmZ ddlmZ ddlmZ G d	d
 d
ZdS )!Experiment with different models.    )annotations)ListOptionalSequence)BaseLLMPromptTemplate)get_color_mapping
print_text)ChainLLMChainc                   @  sJ   e Zd ZdZddddddZeddd	d d
ddZdddddZdS )ModelLaboratoryr   NzSequence[Chain]zOptional[List[str]])chainsnamesc                 C  s   |D ]R}t |tstdt|jdkr8td|j t|jdkrtd|j q|dk	rxt|t|krxtd|| _dd tt| jD }t|| _	|| _
dS )	zyInitialize with chains to experiment with.

        Args:
            chains: list of chains to experiment with.
        zModelLaboratory should now be initialized with Chains. If you want to initialize with LLMs, use the `from_llms` method instead (`ModelLaboratory.from_llms(...)`)   z;Currently only support chains with one input variable, got z<Currently only support chains with one output variable, got Nz0Length of chains does not match length of names.c                 S  s   g | ]}t |qS  str).0ir   r   >/tmp/pip-unpacked-wheel-bo69hh5q/langchain/model_laboratory.py
<listcomp>-   s     z,ModelLaboratory.__init__.<locals>.<listcomp>)
isinstancer   
ValueErrorlenZ
input_keysZoutput_keysr   ranger
   chain_colorsr   )selfr   r   chainZchain_ranger   r   r   __init__   s(    



zModelLaboratory.__init__zList[BaseLLM]zOptional[PromptTemplate])llmspromptreturnc                   sB    dkrt dgdd  fdd|D }dd |D }| ||dS )	a)  Initialize with LLMs to experiment with and optional prompt.

        Args:
            llms: list of LLMs to experiment with
            prompt: Optional prompt to use to prompt the LLMs. Defaults to None.
                If a prompt was provided, it should only have one input variable.
        N_inputz{_input})Zinput_variablestemplatec                   s   g | ]}t | d qS ))llmr#   r   r   r'   r#   r   r   r   >   s     z-ModelLaboratory.from_llms.<locals>.<listcomp>c                 S  s   g | ]}t |qS r   r   r(   r   r   r   r   ?   s     )r   r   )clsr"   r#   r   r   r   r)   r   	from_llms1   s
    zModelLaboratory.from_llmsr   None)textr$   c                 C  st   t d| d t| jD ]T\}}| jdk	r8| j| }nt|}t|dd ||}t|| jt| dd qdS )a3  Compare model outputs on an input text.

        If a prompt was provided with starting the laboratory, then this text will be
        fed into the prompt. If no prompt was provided, then the input text is the
        entire prompt.

        Args:
            text: input text to run all models on.
        z[1mInput:[0m

N)endz

)colorr/   )print	enumerater   r   r   r   runr   )r   r-   r   r    nameoutputr   r   r   compareB   s    


zModelLaboratory.compare)N)N)__name__
__module____qualname____doc__r!   classmethodr+   r6   r   r   r   r   r      s    r   N)r:   
__future__r   typingr   r   r   Z#langchain_core.language_models.llmsr   Zlangchain_core.prompts.promptr	   Zlangchain_core.utils.inputr
   r   Zlangchain.chains.baser   Zlangchain.chains.llmr   r   r   r   r   r   <module>   s   