o
    Zh                     @  s  d dl mZ d dlZd dlZd dlmZmZ d dlZ	d dl
mZ d dlmZ d dlmZmZ ejr=d dlmZ d dlmZ eeZed	d
dddddddddddddddddddgddddd d!dddd"d#d$d%d&dddgg d'd(d)d*ZeG d+d, d,eZe ZdS )-    )annotationsN)	dataclassfield)json_loader)Prompt)EvaluationModeMetricWithLLM)	Callbacks)PromptValuecontext_recallzGiven a context, and an answer, analyze each sentence in the answer and classify if the sentence can be attributed to the given context or not. Use only "Yes" (1) or "No" (0) as a binary classification. Output json with reason.z2What can you tell me about albert Albert Einstein?ua  Albert Einstein (14 March 1879 – 18 April 1955) was a German-born theoretical physicist, widely held to be one of the greatest and most influential scientists of all time. Best known for developing the theory of relativity, he also made important contributions to quantum mechanics, and was thus a central figure in the revolutionary reshaping of the scientific understanding of nature that modern physics accomplished in the first decades of the twentieth century. His mass–energy equivalence formula E = mc2, which arises from relativity theory, has been called 'the world's most famous equation'. He received the 1921 Nobel Prize in Physics 'for his services to theoretical physics, and especially for his discovery of the law of the photoelectric effect', a pivotal step in the development of quantum theory. His work is also known for its influence on the philosophy of science. In a 1999 poll of 130 leading physicists worldwide by the British journal Physics World, Einstein was ranked the greatest physicist of all time. His intellectual achievements and originality have made Einstein synonymous with genius.a;  Albert Einstein born in 14 March 1879 was  German-born theoretical physicist, widely held to be one of the greatest and most influential scientists of all time. He received the 1921 Nobel Prize in Physics for his services to theoretical physics. He published 4 papers in 1905.  Einstein moved to Switzerland in 1895zAlbert Einstein, born on 14 March 1879, was a German-born theoretical physicist, widely held to be one of the greatest and most influential scientists of all time.zBThe date of birth of Einstein is mentioned clearly in the context.1)Zstatement_1reason
AttributedzUHe received the 1921 Nobel Prize in Physics 'for his services to theoretical physics.z3The exact sentence is present in the given context.)Zstatement_2r   r   zHe published 4 papers in 1905.z?There is no mention about papers he wrote in the given context.0)Zstatement_3r   r   z&Einstein moved to Switzerland in 1895.z>There is no supporting evidence for this in the given context.)Zstatement_4r   r   )questioncontextanswerclassificationzwho won 2020 icc world cup?aT  The 2022 ICC Men's T20 World Cup, held from October 16 to November 13, 2022, in Australia, was the eighth edition of the tournament. Originally scheduled for 2020, it was postponed due to the COVID-19 pandemic. England emerged victorious, defeating Pakistan by five wickets in the final to clinch their second ICC Men's T20 World Cup title.ZEnglandz-England won the 2022 ICC Men's T20 World Cup.zMFrom context it is clear that England defeated Pakistan to win the World Cup.z%What is the primary fuel for the Sun?ZNULLZHydrogenz#The Sun's primary fuel is hydrogen.z#The context contains no informationr   r   r   r   json)nameZinstructionZexamplesZ
input_keysZ
output_keyoutput_typec                   @  sx   e Zd ZU dZdZded< ejZded< e	dd d	Z
d
ed< d&ddZd'ddZd(ddZd)d*d"d#Zd)d+d$d%ZdS ),ContextRecallz
    Estimates context recall by estimating TP and FN using annotated answer and
    retrieved context.

    Attributes
    ----------
    name : str
    r   strr   r   evaluation_modec                   C  s   t S N)CONTEXT_RECALL_RA r   r   T/var/www/html/lang_env/lib/python3.10/site-packages/ragas/metrics/_context_recall.py<lambda>\   s    zContextRecall.<lambda>)default_factoryr   context_recall_promptrowt.Dictreturnr
   c                 C  sF   |d |d |d }}}t |trd|n|}| jj|||dS )Nr   ZcontextsZground_truth
r   )
isinstancelistjoinr!   format)selfr"   Zqstnctxgtr   r   r   _create_context_recall_prompt^   s   z+ContextRecall._create_context_recall_promptresponset.Anyfloatc                 C  sn   t |tr|n|g}dd |D }dd |D }t|}t|}|dkr(|| ntj}t|r5td |S )Nc                 S  s   g | ]}t |tr|ni qS r   )r&   dict.0itemr   r   r   
<listcomp>f   s    z0ContextRecall._compute_score.<locals>.<listcomp>c                 S  s2   g | ]}| d rt| d  dkntjqS )r   r   )getintstripnpnanr2   r   r   r   r5   g   s    r   z@Invalid JSON response. Expected dictionary with key 'Attributed')	r&   r'   lensumr9   r:   isnanloggerwarning)r*   r.   denom	numeratorZscorer   r   r   _compute_scored   s   
zContextRecall._compute_score	callbacksr	   is_asyncboolc                   s`   | j d us
J d| j j| |||dI d H }tj|jd d j| j |dI d H }| |S )Nset LLM before use)rC   rD   r   )rD   )llmgenerater-   r   Z	safe_loadZgenerationstextrB   )r*   r"   rC   rD   resultr.   r   r   r   _ascorex   s   
zContextRecall._ascoreNlanguage	cache_dir
str | NoneNonec                 C  s:   | j d us	J dtd|  | j|| j || _d S )NrF   zAdapting Context Recall to )rG   r>   infor!   adapt)r*   rL   rM   r   r   r   rQ      s
   
zContextRecall.adaptc                 C  s   | j | d S r   )r!   save)r*   rM   r   r   r   rR      s   zContextRecall.save)r"   r#   r$   r
   )r.   r/   r$   r0   )r"   r#   rC   r	   rD   rE   r$   r0   r   )rL   r   rM   rN   r$   rO   )rM   rN   r$   rO   )__name__
__module____qualname____doc__r   __annotations__r   Zqcgr   r   r!   r-   rB   rK   rQ   rR   r   r   r   r   r   N   s   
 	


r   )
__future__r   loggingtypingtdataclassesr   r   numpyr9   Zragas.llms.json_loadr   Zragas.llms.promptr   Zragas.metrics.baser   r   TYPE_CHECKINGZlangchain_core.callbacksr	   r
   	getLoggerrS   r>   r   r   r   r   r   r   r   <module>   sx    
1:
A