a
    •˜ëj'  ã                   @  sz   d Z ddlmZ ddlmZ ddl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)ÚSequence)ÚOptional)Ú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 ]V}t |tƒsd}t|ƒ‚t|jƒdkr@d|j› �}t|ƒ‚t|jƒdkrd|j› �}q|dur€t|ƒt|ƒkr€d}t|ƒ‚|| _dd„ tt| jƒƒD ƒ}t|ƒ| _	|| _
dS )	aÐ  Initialize the ModelLaboratory with chains to experiment with.

        Args:
            chains: 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.
        z¡ModelLaboratory 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   úY/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/langchain/model_laboratory.pyÚ
<listcomp>:   ó    z,ModelLaboratory.__init__.<locals>.<listcomp>)Ú
isinstancer
   Ú
ValueErrorÚlenZ
input_keysZoutput_keysr   Úranger   Úchain_colorsr   )Úselfr   r   ÚchainÚmsgZchain_ranger   r   r   Ú__init__   s.    
ÿÿÿÿÿ
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   P   r   z-ModelLaboratory.from_llms.<locals>.<listcomp>c                 S  s   g | ]}t |ƒ‘qS r   r   r(   r   r   r   r   Q   r   )r   r   )Úclsr"   r#   r   r   r   r)   r   Ú	from_llms>   s
    zModelLaboratory.from_llmsr   ÚNone)Útextr$   c                 C  sr   t d|› d�ƒ t| jƒD ]R\}}| jdur6| 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   ÚcompareT   s    

zModelLaboratory.compare)N)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r!   Úclassmethodr+   r6   r   r   r   r   r      s   + ýr   N)r:   Ú
__future__r   Úcollections.abcr   Útypingr   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   