
    -j(                       d dl mZ d dl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mZ d dlmZ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j        e          Z G d
 de          Z ee          Z ee          Z eddedde                     g d          !                                ddde                     dg          !                                ddde                     g           !                                dgddgdd          Z" G d de          Z# G d d e          Z$ ee$          Z% ee$          Z& ed!d"e%d#g d$e$                     d%d&d d'd(d)d d'd*d+d,d'd-d.d d'g          !                                d/d0d1ge$                     d1d2d d'g          !                                d/gd3dgdd          Z'e G d4 d5e                      Z( e(            Z)dS )6    )annotationsN)	dataclassfield)	BaseModelField)RagasoutputParserget_json_format_instructions)Prompt)EvaluationModeMetricWithLLM)	Callbacks)PromptValuec                  <    e Zd ZU  edd          Zded<   d
dZd	S )StatementsAnswers.z the list of extracted statementsdescriptiont.List[str]__root__returnt.List[t.Dict]c                6    |                                  d         S Nr   dictselfs    e/lsinfo/ai/hellotax_ai/base_platform/venv/lib/python3.11/site-packages/ragas/metrics/_faithfulness.pydictszStatementsAnswers.dicts       yy{{:&&    Nr   r   )__name__
__module____qualname__r   r   __annotations__r    r    r   r   r      sK         !E#3UVVVHVVVV' ' ' ' ' 'r    r   )pydantic_object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.)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answer
statementszNCadmium Chloride is slightly soluble in this chemical, it is also called what?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*   r+   json)nameinstructionoutput_format_instructionexamples
input_keys
output_keyoutput_typec                  |    e Zd ZU  edd          Zded<    edd          Zded<    edd	          Zded
<   dS )StatementFaithfulnessAnswer.z$the original statement, word-by-wordr   str	statementz%the verdict(0/1) of the faithfulness.intverdictzthe reason of the verdictreasonN)r"   r#   r$   r   r8   r%   r:   r;   r&   r    r   r6   r6   G   su         U3,RSSSISSSS5*QRRRGRRRR%)DEEEFEEEEEEr    r6   c                  "    e Zd ZU ded<   ddZdS )StatementFaithfulnessAnswersz#t.List[StatementFaithfulnessAnswer]r   r   r   c                6    |                                  d         S r   r   r   s    r   r   z"StatementFaithfulnessAnswers.dictsP   r   r    Nr!   )r"   r#   r$   r%   r   r&   r    r   r=   r=   M   s6         1111' ' ' ' ' 'r    r=   nli_statementsa   Your task is to judge the faithfulness of a series of statements based on a given context. For each statement you must return verdict as 1 if the statement can be verified based on the context or 0 if the statement can not be verified based on the context.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.)John is majoring in Biology.3John is taking a course on Artificial Intelligence.John is a dedicated student.John has a part-time job.r@   zwJohn's major is explicitly mentioned as Computer Science. There is no information suggesting he is majoring in Biology.)r8   r;   r:   rA   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.rB   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.   rC   zOThere is no information given in the context about John having a part-time job.)contextr+   r*   zuPhotosynthesis is a process used by plants, algae, and certain bacteria to convert light energy into chemical energy.zAlbert Einstein was a genius.z'The context and statement are unrelatedrE   c                      e Zd ZU dZded<   ej        Zded<    ed           Z	ded	<    ed
           Z
ded<   dZded<   d)dZd*dZd+dZd,d!Zd-d.d'Zd-d/d(Zd"S )0Faithfulnessfaithfulnessr7   r.   r   evaluation_modec                     t           S N)LONG_FORM_ANSWER_PROMPTr&   r    r   <lambda>zFaithfulness.<lambda>   s     7 r    )default_factoryr
   long_form_answer_promptc                     t           S rK   )NLI_STATEMENTS_MESSAGEr&   r    r   rM   zFaithfulness.<lambda>   s     6 r    nli_statements_messagerD   r9   max_retriesrowt.Dictr   r   c                ^    |d         |d         }}| j                             ||          }|S )Nr)   r*   )r)   r*   )rO   format)r   rT   r)   r*   prompt_values        r   _create_answer_promptz"Faithfulness._create_answer_prompt   s?    z?CM& 3::f ; 
 
 r    r+   r   c                    | j         
J d            |d         }d                    |          }t          j        |          }| j                            ||          }|S )Nz llm must be set to compute scorecontexts
)rE   r+   )llmjoinr-   dumpsrR   rW   )r   rT   r+   r[   contexts_strstatements_strrX   s          r   _create_nli_promptzFaithfulness._create_nli_prompt   sl    x##%G###z? IIh//"j44299 ^ : 
 
 r    answersr=   c                    t          d |j        D                       }t          |j                  }|r||z  }n&t                              d           t
          j        }|S )Nc              3  ,   K   | ]}|j         rd ndV  dS )rD   r   N)r:   ).0r*   s     r   	<genexpr>z.Faithfulness._compute_score.<locals>.<genexpr>   s>       "
 "
+1&AAQ"
 "
 "
 "
 "
 "
r    z-No statements were generated from the answer.)sumr   lenloggerwarningnpnan)r   rc   faithful_statementsnum_statementsscores        r   _compute_scorezFaithfulness._compute_score   sv    ! "
 "
5<5E"
 "
 "
 
 
 W-.. 	'.8EENNJKKKFEr    r   t.Self	callbacksr   is_asyncboolfloatc                  K   | j         
J d            |                     |          }| j                             |||           d{V }|j        d         d         j        }t
                              ||| j         | j                   d{V }|t          j	        S | 
                    ||j                  }| j                             |||           d{V }|j        d         d         j        }	t                              |	|| j         | j                   d{V }
|
t          j	        S |                     |
          S )z?
        returns the NLI score for each (q, c, a) pair
        NLLM is not set)rs   rt   r   )r]   rY   generategenerationstext_statements_output_parseraparserS   rl   rm   rb   r   _faithfulness_output_parserrq   )r   rT   rs   rt   p_valueanswer_resultanswer_result_textr+   
nli_resultnli_result_textrH   s              r   _ascorezFaithfulness._ascore   s      x##%5###,,S11"h//y8 0 
 
 
 
 
 
 
 
 +6q9!<A4;;43C
 
 
 
 
 
 
 

 6M))#z/BCC8,,y8 - 
 
 
 
 
 
 
 

 %03A6;8??Wdh0@
 
 
 
 
 
 
 
 6M""<000r    Nlanguage	cache_dirt.Optional[str]Nonec                    | j         
J d            t                              d|            | j                            || j         |          | _        | j                            || j         |          | _        d S )Nrx   z Adapting Faithfulness metric to )r]   rj   inforO   adaptrR   )r   r   r   s      r   r   zFaithfulness.adapt   s    x##%5###AxAABBB'+'C'I'Idh	(
 (
$ '+&A&G&Gdh	'
 '
###r    c                n    | j                             |           | j                            |           d S rK   )rO   saverR   )r   r   s     r   r   zFaithfulness.save   s6    $)))444#((33333r    )rT   rU   r   r   )rT   rU   r+   r   r   r   )rc   r=   )
r   rr   rT   rU   rs   r   rt   ru   r   rv   rK   )r   r7   r   r   r   r   )r   r   r   r   )r"   r#   r$   r.   r%   r   qacrI   r   rO   rR   rS   rY   rb   rq   r   r   r   r&   r    r   rG   rG      s5        D&4&8O8888&+e77' ' '     &+U66& & &     K   
 
 
 
   1 1 1 1@	
 	
 	
 	
 	
4 4 4 4 4 4 4r    rG   )*
__future__r   r-   loggingtypingtdataclassesr   r   numpyrl   langchain_core.pydantic_v1r   r   ragas.llms.output_parserr   r	   ragas.llms.promptr
   ragas.metrics.baser   r   TYPE_CHECKINGlangchain_core.callbacksr   r   	getLoggerr"   rj   r   _statements_output_instructionsr|   	parse_objr   rL   r6   r=   !_faithfulness_output_instructionsr~   rQ   rG   rH   r&   r    r   <module>r      s<   " " " " " "       ( ( ( ( ( ( ( (     7 7 7 7 7 7 7 7 T T T T T T T T $ $ $ $ $ $ < < < < < < < <? .222222------		8	$	$' ' ' ' '	 ' ' ' #?">?P"Q"Q -->OPPP  !&	W= R v+55    egg	
 	
 i+55CD egg	
 	
 TG+55b99??AA	
 	
+6 H%C" " " JF F F F F) F F F' ' ' ' '9 ' ' ' %A$@ % % ! 0/0     	 S? D   3<< &D #\#$  &[ #V#$  &D #}#$  &A"s#$ ! . egg? 	
  	
D S:;2<< &E"K#$   egg	
 	
E/` <(m7 7 7 t [4 [4 [4 [4 [4= [4 [4 [4| |~~r    