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mZ eeZejrDd dlmZ d dlmZ ed	d
dddd dddddd dddddd ddddddddgddgdddZeG dd  d eeZe ZdS )!    )annotationsN)	dataclassfield)json_loader)Prompt)EvaluationModeMetricWithEmbeddingsMetricWithLLM)	Callbacks)PromptValuequestion_generationa2  Generate a question for the given answer and Identify if answer is noncommittal. Give noncommittal as 1 if the answer is noncommittal and 0 if the answer is committal. A noncommittal answer is one that is evasive, vague, or ambiguous. For example, "I don't know" or "I'm not sure" are noncommittal answers.z$Albert Einstein was born in Germany.zAlbert Einstein was a German-born theoretical physicist who is widely held to be one of the greatest and most influential scientists of all timezWhere was Albert Einstein born?)questionnoncommittal)answercontextoutputzIIt can change its skin color based on the temperature of its environment.zA recent scientific study has discovered a new species of frog in the Amazon rainforest that has the unique ability to change its skin color based on the temperature of its environment.zCWhat unique ability does the newly discovered species of frog have?ZEverestzdThe tallest mountain on Earth, measured from sea level, is a renowned peak located in the Himalayas.z&What is the tallest mountain on Earth?z|I don't know about the  groundbreaking feature of the smartphone invented in 2023 as am unaware of information beyond 2022. zIn 2023, a groundbreaking invention was announced: a smartphone with a battery life of one month, revolutionizing the way people use mobile technology.zGWhat was the groundbreaking feature of the smartphone invented in 2023?   r   r   r   json)nameZinstructionZexamplesZ
input_keysZ
output_keyoutput_typec                   @  s   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Zded< d0ddZd1ddZd2dd Zd3d%d&Zd4d5d,d-Zd4d6d.d/Zd'S )7AnswerRelevancya"  
    Scores the relevancy of the answer according to the given question.
    Answers with incomplete, redundant or unnecessary information is penalized.
    Score can range from 0 to 1 with 1 being the best.

    Attributes
    ----------
    name: string
        The name of the metrics
    strictness: int
        Here indicates the number questions generated per answer.
        Ideal range between 3 to 5.
    embeddings: Embedding
        The langchain wrapper of Embedding object.
        E.g. HuggingFaceEmbeddings('BAAI/bge-base-en')
    answer_relevancystrr   r   evaluation_modec                   C  s   t S N)QUESTION_GEN r   r   V/var/www/html/lang_env/lib/python3.10/site-packages/ragas/metrics/_answer_relevance.py<lambda>T   s    zAnswerRelevancy.<lambda>)default_factoryr   r      int
strictnessselft.Selfr   generated_questions	list[str]c                 C  s~   | j d usJ t| j |dd}t| j |t|d}tjj|ddtjj|dd }t	||j
d| S )Nr   )Zaxis)Z
embeddingsnpZasarrayZembed_queryZreshapeZembed_documentslenZlinalgnormdotT)r#   r   r%   Zquestion_vecZgen_question_vecr*   r   r   r   calculate_similarityW   s    
z$AnswerRelevancy.calculate_similarityresponset.Sequence[t.Any]rowt.Dictreturnfloatc                 C  sr   |d }dd |D }t dd |D }tdd |D r(td t j}|S | ||}| t|  }|S )Nr   c                 S  s"   g | ]}t |tr|d dqS )r    )
isinstancedictget.0itemr   r   r   
<listcomp>k   s
    

z4AnswerRelevancy._calculate_score.<locals>.<listcomp>c                 S  s&   g | ]}t |trt|d dqS )r   r   )r5   r6   boolr7   r8   r   r   r   r;   o   s    c                 s  s    | ]}|d kV  qdS )r4   Nr   )r9   qr   r   r   	<genexpr>u   s    z3AnswerRelevancy._calculate_score.<locals>.<genexpr>z>Invalid JSON response. Expected dictionary with key 'question')	r(   anyallloggerwarningnanr-   meanr!   )r#   r.   r0   r   Zgen_questionsZ	committalZscoreZ
cosine_simr   r   r   _calculate_scorei   s$   z AnswerRelevancy._calculate_scorer   c                 C  s(   |d |d }}| j j|d|dS )Nr   Zcontexts
)r   r   )r   formatjoin)r#   r0   Zansctxr   r   r   _create_question_gen_prompt   s   z+AnswerRelevancy._create_question_gen_prompt	callbacksr
   is_asyncr<   c                   sf   j d us
J d|}j j|j| dI d H } fdd|jd D I d H }||S )NLLM is not set)nrK   rL   c                   s(   g | ]}t j|jj d I dH qS ))rL   N)r   Z	safe_loadtextllm)r9   rrL   r#   r   r   r;      s
    z+AnswerRelevancy._ascore.<locals>.<listcomp>r   )rP   rJ   generater"   ZgenerationsrE   )r#   r0   rK   rL   promptresultr.   r   rR   r   _ascore   s   
zAnswerRelevancy._ascoreNlanguage	cache_dir
str | NoneNonec                 C  s:   | j d us	J dtd|  | j|| j || _d S )NrM   z#Adapting AnswerRelevancy metric to )rP   rA   infor   adapt)r#   rW   rX   r   r   r   r\      s
   
zAnswerRelevancy.adaptc                 C  s   | j | d S r   )r   save)r#   rX   r   r   r   r]      s   zAnswerRelevancy.save)r#   r$   r   r   r%   r&   )r.   r/   r0   r1   r2   r3   )r0   r1   r2   r   )r0   r1   rK   r
   rL   r<   r2   r3   r   )rW   r   rX   rY   r2   rZ   )rX   rY   r2   rZ   )__name__
__module____qualname____doc__r   __annotations__r   Zqacr   r   r   r"   r-   rE   rJ   rV   r\   r]   r   r   r   r   r   ?   s   
 



r   )
__future__r   loggingtypingtdataclassesr   r   numpyr(   Zragas.llms.json_loadr   Zragas.llms.promptr   Zragas.metrics.baser   r   r	   	getLoggerr^   rA   TYPE_CHECKINGZlangchain_core.callbacksr
   r   r   r   r   r   r   r   r   <module>   s^    
			"+
a