o
    Zh                     @  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 )z!Experiment with different models.    )annotations)ListOptionalSequence)BaseLLMPromptTemplate)get_color_mapping
print_text)ChainLLMChainc                   @  s8   e Zd ZdZddddZe	ddddZdddZdS )ModelLaboratoryzMA utility to experiment with and compare the performance of different models.NchainsSequence[Chain]namesOptional[List[str]]c                 C  s   |D ])}t |tstdt|jdkrtd|j t|jdkr+td|j q|dur<t|t|kr<td|| _dd tt| jD }t|| _	|| _
dS )	a  Initialize the ModelLaboratory with chains to experiment with.

        Args:
            chains (Sequence[Chain]): A sequence of chains to experiment with.
            Each chain must have exactly one input and one output variable.
        names (Optional[List[str]]): Optional list of names corresponding to each chain.
            If provided, its length must match the number of chains.


        Raises:
            ValueError: If any chain is not an instance of `Chain`.
            ValueError: If a chain does not have exactly one input variable.
            ValueError: If a chain does not have exactly one output variable.
            ValueError: If the length of `names` does not match the number of chains.
        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     g | ]}t |qS  str).0ir   r   Q/var/www/html/lang_env/lib/python3.10/site-packages/langchain/model_laboratory.py
<listcomp>7       z,ModelLaboratory.__init__.<locals>.<listcomp>)
isinstancer   
ValueErrorlenZ
input_keysZoutput_keysr   ranger	   chain_colorsr   )selfr   r   chainZchain_ranger   r   r   __init__   s2   


zModelLaboratory.__init__llmsList[BaseLLM]promptOptional[PromptTemplate]returnc                   sB    du rt dgdd  fdd|D }dd |D }| ||dS )	a  Initialize the ModelLaboratory with LLMs and an optional prompt.

        Args:
            llms (List[BaseLLM]): A list of LLMs to experiment with.
            prompt (Optional[PromptTemplate]): An optional prompt to use with the LLMs.
                If provided, the prompt must contain exactly one input variable.

        Returns:
            ModelLaboratory: An instance of `ModelLaboratory` initialized with LLMs.
        N_inputz{_input})Zinput_variablestemplatec                   s   g | ]}t | d qS ))llmr'   r   r   r,   r'   r   r   r   K   s    z-ModelLaboratory.from_llms.<locals>.<listcomp>c                 S  r   r   r   r-   r   r   r   r   L   r   )r   r   )clsr%   r'   r   r   r   r.   r   	from_llms;   s
   zModelLaboratory.from_llmstextr   Nonec                 C  st   t d| d t| jD ]*\}}| jdur| 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

)colorr4   )print	enumerater   r   r   r
   runr!   )r"   r1   r   r#   nameoutputr   r   r   compareO   s   


zModelLaboratory.compare)N)r   r   r   r   )r%   r&   r'   r(   r)   r   )r1   r   r)   r2   )__name__
__module____qualname____doc__r$   classmethodr0   r;   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    