o
    Zh                     @  s   d Z ddlmZ ddlZddlZddlmZmZ ddl	m
Z
 ddlmZ ddlmZ ddlmZ ejrEdd	lmZ dd
lmZ ddlmZ eddZe
G dd deZe
G dd deZe
G dd deZdS )z
Q - question
A - answer: generated_text from RAG pipeline
C - contexts: context used for generation
G - ground_truth: ground truth answer
    )annotationsN)ABCabstractmethod)	dataclass)Enum)	new_group)	RunConfig)	Callbacks)BaseRagasEmbeddings)BaseRagasLLMEvaluationModezqac qa qc gc ga qga qcgc                   @  s~   e Zd Zeed%ddZeed&ddZed'd
dZd(d)ddZd(d*ddZ	d(d+ddZ
	d,d-d!d"Zed.d#d$ZdS )/Metricreturnstrc                 C     d S N selfr   r   I/var/www/html/lang_env/lib/python3.10/site-packages/ragas/metrics/base.pyname      zMetric.namer   c                 C  r   r   r   r   r   r   r   evaluation_mode#   r   zMetric.evaluation_mode
run_configr   c                 C  s   dS )z=
        This method will lazy initialize the model.
        Nr   r   r   r   r   r   init(   s   zMetric.initNlanguage	cache_dirt.Optional[str]Nonec                 C     t d| j)z;
        Adapt the metric to a different language.
        (adapt() is not implemented for {} metricNotImplementedErrorformatr   )r   r   r   r   r   r   adapt/      
zMetric.adaptc                 C  r    )z,
        Save the metric to a path.
        r!   r"   )r   r   r   r   r   save7   r&   zMetric.saver   t.Selfrowt.Dict	callbacksr	   floatc              
   C  s~   |pg }t | j||dd\}}zt| j||dd}W n ty2 } z
|js,|| |d }~ww |js=|d|i |S )NFZinputsr+   is_asyncr)   r+   r.   output)	r   r   asynciorun_ascore	Exceptionendedon_chain_erroron_chain_end)r   r)   r+   rmgroup_cmscoreer   r   r   r:   ?   s"   


zMetric.scoreTr.   boolc              
     s   |pg }t | j||dd\}}z| j|||dI d H }W n ty3 } z
|js-|| |d }~ww |js>|d|i |S )NTr-   r/   r0   )r   r   r3   r4   r5   r6   r7   )r   r)   r+   r.   r8   r9   r:   r;   r   r   r   ascoreQ   s    


zMetric.ascorec                   s   d S r   r   )r   r)   r+   r.   r   r   r   r3   c   s   zMetric._ascore)r   r   )r   r   r   r   r   )r   r   r   r   r   r   )r   r   r   r   )r   r(   r)   r*   r+   r	   r   r,   )NT)
r   r(   r)   r*   r+   r	   r.   r<   r   r,   )r)   r*   r+   r	   r.   r<   r   r,   )__name__
__module____qualname__propertyr   r   r   r   r%   r'   r:   r=   r3   r   r   r   r   r      s     r   c                   @  $   e Zd ZU dZded< dddZdS )	MetricWithLLMNzt.Optional[BaseRagasLLM]llmr   r   c                 C  ,   | j du rtd| j d| j | dS )
        Init any models in the metric, this is invoked before evaluate()
        to load all the models
        Also check if the api key is valid for OpenAI and AzureOpenAI
        NMetric 'zc' has no valid LLM provided (self.llm is None). Please initantiate a the metric with an LLM to run.)rE   
ValueErrorr   set_run_configr   r   r   r   r   l   
   
zMetricWithLLM.initr>   )r?   r@   rA   rE   __annotations__r   r   r   r   r   rD   h      
 rD   c                   @  rC   )	MetricWithEmbeddingsNzt.Optional[BaseRagasEmbeddings]
embeddingsr   r   c                 C  rF   )rG   NrH   zx' has no valid embeddings provided (self.embeddings is None). Please initantiate a the metric with an embeddings to run.)rO   rI   r   rJ   r   r   r   r   r   }   rK   zMetricWithEmbeddings.initr>   )r?   r@   rA   rO   rL   r   r   r   r   r   rN   y   rM   rN   )__doc__
__future__r   r1   typingtabcr   r   dataclassesr   enumr   Zragas.callbacksr   Zragas.run_configr   TYPE_CHECKINGZlangchain_core.callbacksr	   Zragas.embeddingsr
   Z
ragas.llmsr   r   r   rD   rN   r   r   r   r   <module>   s(    
K