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mZ d dl	m
Z
 d dlmZ d dlmZmZ ejr9d dlmZ eeZed	d
dgddddg didddg didddg didddg didgdZeG dd deZeddZdS )    )annotationsN)	dataclassfield)Dict)json_loader)Prompt)EvaluationModeMetricWithLLM)	CallbacksZtext_entity_extractionzGiven a text, extract unique entities without repetition. Ensure you consider different forms or mentions of the same entity as a single entity.textoutputjsona  The Eiffel Tower, located in Paris, France, is one of the most iconic landmarks globally. 
            Millions of visitors are attracted to it each year for its breathtaking views of the city. 
            Completed in 1889, it was constructed in time for the 1889 World's Fair.entities)zEiffel TowerZParisZFranceZ1889zWorld's Fair)r   r   an  The Colosseum in Rome, also known as the Flavian Amphitheatre, stands as a monument to Roman architectural and engineering achievement. 
            Construction began under Emperor Vespasian in AD 70 and was completed by his son Titus in AD 80. 
            It could hold between 50,000 and 80,000 spectators who watched gladiatorial contests and public spectacles.)Z	ColosseumZRomezFlavian AmphitheatreZ	VespasianzAD 70ZTituszAD 80aL  The Great Wall of China, stretching over 21,196 kilometers from east to west, is a marvel of ancient defensive architecture. 
            Built to protect against invasions from the north, its construction started as early as the 7th century BC. 
            Today, it is a UNESCO World Heritage Site and a major tourist attraction.)zGreat Wall of Chinaz21,196 kilometersz7th century BCzUNESCO World Heritage SiteaJ  The Apollo 11 mission, which launched on July 16, 1969, marked the first time humans landed on the Moon. 
            Astronauts Neil Armstrong, Buzz Aldrin, and Michael Collins made history, with Armstrong being the first man to step on the lunar surface. 
            This event was a significant milestone in space exploration.)zApollo 11 missionzJuly 16, 1969ZMoonzNeil ArmstrongzBuzz AldrinzMichael Collins)nameZinstructionZ
input_keysZ
output_keyoutput_typeZexamplesc                   @  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Zded< d'ddZd(ddZd)dd Zd*d+d%d&Zd!S ),ContextEntityRecallu  
    Calculates recall based on entities present in ground truth and context.
    Let CN be the set of entities present in context,
    GN be the set of entities present in the ground truth.

    Then we define can the context entity recall as follows:
    Context Entity recall = | CN ∩ GN | / | GN |

    If this quantity is 1, we can say that the retrieval mechanism has
    retrieved context which covers all entities present in the ground truth,
    thus being a useful retrieval. Thus this can be used to evaluate retrieval
    mechanisms in specific use cases where entities matter, for example, a
    tourism help chatbot.

    Attributes
    ----------
    name : str
    batch_size : int
        Batch size for openai completion.
    context_entity_recallstrr   r   evaluation_modec                   C  s   t S N)TEXT_ENTITY_EXTRACTION r   r   ]/var/www/html/lang_env/lib/python3.10/site-packages/ragas/metrics/_context_entities_recall.py<lambda>j   s    zContextEntityRecall.<lambda>)default_factoryr   context_entity_recall_prompt   int
batch_sizeground_truth_entitiessetcontext_entitiesreturnfloatc                 C  s&   t t|t|}|t |d  S )Ng:0yE>)lenr    intersection)selfr   r!   Znum_entities_in_bothr   r   r   _compute_scoren   s   z"ContextEntityRecall._compute_scorer   	callbacksr
   is_asyncboolr   c                   st   | j d us
J d| j j| jj|d||dI d H }tj|jd d j| j |dI d H }t|t	r6|}|S i }|S )NzLLM is not initialized)r   )promptr(   r)   r   )r)   )
Zllmgenerater   formatr   Z	safe_loadZgenerationsr   
isinstancedict)r&   r   r(   r)   resultZresponse_dictr   r   r   get_entitiesv   s    z ContextEntityRecall.get_entitiesrowc                   sb   |d |d }}| j |||dI d H }| j d|||dI d H }| |dg |dg S )Nground_truthcontexts)r(   r)   
r   )r1   joinr'   get)r&   r2   r(   r)   r3   r4   r   r   r   _ascore   s   zContextEntityRecall._ascoreN	cache_dir
str | NoneNonec                 C  s   | j |S r   )r   save)r&   r9   r   r   r   r<      s   zContextEntityRecall.save)r   r    r!   r    r"   r#   )r   r   r(   r
   r)   r*   r"   r   )r2   r   r(   r
   r)   r*   r"   r#   r   )r9   r:   r"   r;   )__name__
__module____qualname____doc__r   __annotations__r   gcr   r   r   r   r'   r1   r8   r<   r   r   r   r   r   P   s   
 


r   r   )r   )
__future__r   loggingtypingtdataclassesr   r   r   Zragas.llms.json_loadr   Zragas.llms.promptr   Zragas.metrics.baser   r	   TYPE_CHECKINGZlangchain.callbacks.baser
   	getLoggerr=   loggerr   r   r   r   r   r   r   <module>   sL    
	?O