o
    Zhg#                     @  sF  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g didddddgiddddg idgddgdddZe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+gd0dgdddZeG d1d2 d2eZe ZdS )3    )annotationsN)	dataclassfield)json_loader)Prompt)EvaluationModeMetricWithLLM)	Callbacks)PromptValueZlong_form_answerzECreate one or more statements from each sentence in the given answer.z7Who was  Albert Einstein and what is he best known for?a  He was a German-born theoretical physicist, widely acknowledged to be one of the greatest and most influential physicists of all time. He was best known for developing the theory of relativity, he also made important contributions to the development of the theory of quantum mechanics.
statements)z~Albert Einstein, a German-born theoretical physicist, is renowned for being one of the most influential physicists in history.z<Albert Einstein was best known for his theory of relativity.zNEinstein's contributions significantly advanced the field of quantum mechanicszURecognized globally, Einstein's work has profoundly impacted the scientific communityzXEinstein's groundbreaking theories continue to shape our understanding of physics today.)questionanswerr   zNCadmium Chloride is slightly soluble in this chemical, it is also called what?Zalcoholz0Cadmium Chloride is slightly soluble in alcohol.z9Were Hitler and Benito Mussolini of the same nationality?z/Sorry, I can't provide answer to that question.r   r   JSON)nameZinstructionZexamplesZ
input_keysZ
output_keyoutput_typeZnli_statementsz8Natural language inference. Use only 'Yes' (1), 'No' (0)af  John is a student at XYZ University. He is pursuing a degree in Computer Science. He is enrolled in several courses this semester, including Data Structures, Algorithms, and Database Management. John is a diligent student and spends a significant amount of time studying and completing assignments. He often stays late in the library to work on his projects.z
            statement_1: John is majoring in Biology.
            statement_2: John is taking a course on Artificial Intelligence.
            statement_3: John is a dedicated student.
            statement_4: John has a part-time job.
            zJohn is majoring in Biology.zwJohn's major is explicitly mentioned as Computer Science. There is no information suggesting he is majoring in Biology.0)Zstatement_1reasonverdictz3John is taking a course on Artificial Intelligence.zThe context mentions the courses John is currently enrolled in, and Artificial Intelligence is not mentioned. Therefore, it cannot be deduced that John is taking a course on AI.)Zstatement_2r   r   zJohn is a dedicated student.zThe context states that he spends a significant amount of time studying and completing assignments. Additionally, it mentions that he often stays late in the library to work on his projects, which implies dedication.1)Zstatement_3r   r   zJohn has a part-time job.zOThere is no information given in the context about John having a part-time job.)Zstatement_4r   r   )contextr   r   zuPhotosynthesis is a process used by plants, algae, and certain bacteria to convert light energy into chemical energy.z*statement_1: Albert Einstein was a genius.zAlbert Einstein was a genius.z'The context and statement are unrelatedr   c                   @  s   e Zd ZU dZded< ejZded< edd d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"Zd0d1d(d)Zd0d2d*d+Zd#S )3Faithfulnessfaithfulnessstrr   r   evaluation_modec                   C     t S N)LONG_FORM_ANSWER_PROMPT r   r   R/var/www/html/lang_env/lib/python3.10/site-packages/ragas/metrics/_faithfulness.py<lambda>p       zFaithfulness.<lambda>)default_factoryr   long_form_answer_promptc                   C  r   r   )NLI_STATEMENTS_MESSAGEr   r   r   r   r   s   r    nli_statements_messagerowt.Dictreturnr
   c                 C  s&   |d |d }}| j j||d}|S )Nr   r   )r   r   )r"   format)selfr%   r   r   prompt_valuer   r   r   _create_answer_promptv   s
   z"Faithfulness._create_answer_promptr   t.Anyc                 C  sP   | j d us	J d|d }d|}ddd t|D }| jj||d}|S )Nz llm must be set to compute scorecontexts
c                 S  s$   g | ]\}}d |d  d| qS )Z
statement_   z: r   ).0istr   r   r   
<listcomp>   s   $ z3Faithfulness._create_nli_prompt.<locals>.<listcomp>)r   r   )llmjoin	enumerater$   r(   )r)   r%   r   r-   Zcontexts_strZstatements_strr*   r   r   r   _create_nli_prompt   s   
zFaithfulness._create_nli_promptoutputc                   s`   ddd t |tr|n|g}t fdd|D }t|}|r&|| }|S td tj}|S )Nr/   r   )r   r   c                 3  s:    | ]}t |tr t|d dtjntjV  qdS )r    N)
isinstancedictgetr   npnan)r0   Zstatement_with_validationZverdict_score_mapr   r   	<genexpr>   s    
z.Faithfulness._compute_score.<locals>.<genexpr>z=Invalid JSON response. Expected dictionary with key 'verdict')r:   listsumlenloggerwarningr=   r>   )r)   r8   Zfaithful_statementsZnum_statementsZscorer   r?   r   _compute_score   s   
zFaithfulness._compute_scorer)   t.Self	callbacksr	   is_asyncboolfloatc           	        s   | j dus
J d| |}| j j|||dI dH }tj|jd d j| j ||dI dH }t|tr5|ni }|	dg }|re| 
||}| j j|||dI dH }tj|jd d j| j ||dI dH }ni g}| |S )z?
        returns the NLI score for each (q, c, a) pair
        NLLM is not set)rH   rI   r   )textr4   rH   rI   r   )r4   r+   generater   Z	safe_loadZgenerationsrM   r:   r;   r<   r7   rF   )	r)   r%   rH   rI   pZanswer_resultr   Z
nli_resultZjson_outputr   r   r   _ascore   s6   

zFaithfulness._ascoreNlanguage	cache_dirt.Optional[str]Nonec                 C  sN   | j d us	J dtd|  | j|| j || _| j|| j || _d S )NrL   z Adapting Faithfulness metric to )r4   rD   infor"   adaptr$   )r)   rQ   rR   r   r   r   rV      s   
zFaithfulness.adaptc                 C  s   | j | | j| d S r   )r"   saver$   )r)   rR   r   r   r   rW      s   zFaithfulness.save)r%   r&   r'   r
   )r%   r&   r   r,   r'   r
   )r8   r,   )
r)   rG   r%   r&   rH   r	   rI   rJ   r'   rK   r   )rQ   r   rR   rS   r'   rT   )rR   rS   r'   rT   )__name__
__module____qualname__r   __annotations__r   Zqacr   r   r"   r$   r+   r7   rF   rP   rV   rW   r   r   r   r   r   k   s   
 

	

#r   )
__future__r   loggingtypingtdataclassesr   r   numpyr=   Zragas.llms.json_loadr   Zragas.llms.promptr   Zragas.metrics.baser   r   TYPE_CHECKINGZlangchain_core.callbacksr	   r
   	getLoggerrX   rD   r   r#   r   r   r   r   r   r   <module>   s    
$ *3
k