a
    ag                     @  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                   @  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 )ModelLaboratoryzMA utility to experiment with and compare the performance of different models.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urxt|t|krxt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  s   g | ]}t |qS  str).0ir   r   h/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/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__   s0    

zModelLaboratory.__init__zList[BaseLLM]zOptional[PromptTemplate])llmsprompt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   r   z-ModelLaboratory.from_llms.<locals>.<listcomp>c                 S  s   g | ]}t |qS r   r   r(   r   r   r   r   L   r   )r   r   )clsr"   r#   r   r   r   r)   r   	from_llms;   s
    zModelLaboratory.from_llmsr   None)textr$   c                 C  st   t d| d t| jD ]T\}}| jdu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   compareO   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   