File: //usr/local/lib/python3.10/dist-packages/langchain/__pycache__/model_laboratory.cpython-310.pyc
o
���g� � @ sv d Z ddlmZ ddlmZmZmZ ddlmZ ddl m
Z
ddlmZm
Z
ddlmZ ddlmZ G d d
� d
�ZdS )z!Experiment with different models.� )�annotations)�List�Optional�Sequence)�BaseLLM��PromptTemplate)�get_color_mapping�
print_text)�Chain��LLMChainc @ s8 e Zd ZdZdddd�Ze dddd��Zddd�ZdS )�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
td��t|j�dkrtd|j� ���t|j�dkr+td|j� ���q|dur<t|�t|�kr<td��|| _dd� tt| j��D �}t|�| _ || _
dS ) 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.
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 � g | ]}t |��qS � ��str)�.0�ir r �E/usr/local/lib/python3.10/dist-packages/langchain/model_laboratory.py�
<listcomp>7 � z,ModelLaboratory.__init__.<locals>.<listcomp>)�
isinstancer �
ValueError�len�
input_keys�output_keysr �ranger �chain_colorsr )�selfr r �chain�chain_ranger r r �__init__ s2
������
zModelLaboratory.__init__�llms�
List[BaseLLM]�prompt�Optional[PromptTemplate]�returnc sB � du rt dgdd�� � fdd�|D �}dd� |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})�input_variables�templatec s g | ]}t |� d ��qS ))�llmr* r �r r0 �r* r r r K s z-ModelLaboratory.from_llms.<locals>.<listcomp>c S r r r r1 r r r r L r )r r )�clsr( r* r r r r2 r � from_llms; s
zModelLaboratory.from_llms�textr �Nonec C st t d|� d�� t| j�D ]*\}}| jdur| j| }nt|�}t|dd� |�|�}t|| jt|� dd� q
dS )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
)�colorr8 )�print� enumerater r r r
�runr# )r$ r5 r r% �name�outputr r r �compareO s
�zModelLaboratory.compare)N)r r r r )r( r) r* r+ r, r )r5 r r, r6 )�__name__�
__module__�__qualname__�__doc__r'