o
    Zh-                     @  s  d dl mZ d dlZd dl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mZ d dlmZ d dlmZ d dlmZ ejeejf ZeeZG d	d
 d
eZG dd deZeddddddgddgdddZeddddddgidddgidgddgdddZdS )     )annotationsN)BaseMessageHumanMessage)PromptValue)	BaseModelroot_validator)BaseRagasLLM)json_loader)get_cache_dirc                   @  s*   e Zd ZU ded< d
ddZdddZd	S )r   str
prompt_strreturnt.List[BaseMessage]c                 C  s   t |  dgS )z$Return prompt as a list of Messages.)content)r   	to_stringself r   H/var/www/html/lang_env/lib/python3.10/site-packages/ragas/llms/prompt.pyto_messages   s   zPromptValue.to_messagesc                 C  s   | j S Nr   r   r   r   r   r      s   zPromptValue.to_stringN)r   r   r   r   )__name__
__module____qualname____annotations__r   r   r   r   r   r   r      s   
 
r   c                   @  s   e Zd ZU dZded< ded< g Zded< ded< ded	< d
Zded< dZded< ed*ddZ	d+ddZ
d,ddZd-ddZ	d.d/d#d$Zd.d0d&d'Zed1d(d)ZdS )2Prompta  
    Prompt is a class that represents a prompt for the ragas metrics.

    Attributes:
        name (str): The name of the prompt.
        instruction (str): The instruction for the prompt.
        examples (List[Dict[str, Any]]): List of example inputs and outputs for the prompt.
        input_keys (List[str]): List of input variable names.
        output_key (str): The output variable name.
        output_type (str): The type of the output (default: "json").
        language (str): The language of the prompt (default: "en").
    r   nameinstructionzt.List[Example]exampleszt.List[str]
input_keys
output_keyjsonoutput_typeenglishlanguagevaluest.Dict[str, t.Any]r   c                 C  sX  | ddu s| ddkrtd| ddu s | dg kr$td| ddu s2| ddkr6td| d	r|d }t|d	 D ]d\}}|d D ]}||vr`td
|d  d| dqM||vrrtd
|d  d| d|d  dkrz||v rt|| trt||  W qE ty } zt| d|d  d| d}~ww qE|S )zV
        Validate the template string to ensure that it is in desired format.
        r   N zinstruction cannot be emptyr!   zinput_keys cannot be emptyr"   zoutput_key cannot be emptyr    zexample    z does not have the variable z in the definitionr$   r#   z in example z is not in valid json format: )get
ValueError	enumeratelower
isinstancer   r#   loads)clsr'   r"   noexampleZinp_keyer   r   r   validate_prompt6   sB   

zPrompt.validate_promptc                 C  s   | j  dkr	dnd}| j| d }| jrQ| jD ]8}| D ]-\}}tj|ddd }| j  dkr?|	dd		d
dn|}|d| d| 7 }q|d7 }q| j
ra|ddd | j
D 7 }| jrm|d| j d7 }|S )z@
        Generate the prompt string from the variables.
        r#   z"
Output in only valid JSON format.r)   
Fensure_asciiutf8{{{}}}: c                 s  s"    | ]}d | d| dV  qdS )r6   z: {r<   Nr   .0keyr   r   r   	<genexpr>u   s     z#Prompt.to_string.<locals>.<genexpr>z: 
)r$   r.   r   r    itemsr#   dumpsencodedecodereplacer!   joinr"   )r   Zadded_json_instructionr   r3   rA   valuer   r   r   r   Z   s*   

zPrompt.to_string
example_nointc                 C  s   |t | jkrtd| d| j| }d}| D ]-\}}tj|ddd }| j	 dkr;|
dd	
d
dn|}|d| d| 7 }q|S )zA
        Get the example string from the example number.
        zexample number z is out of ranger)   Fr7   r9   r#   r:   r;   r<   r=   r6   r>   )lenr    r,   rC   r#   rD   rE   rF   r$   r.   rG   )r   rJ   r3   Zexample_strrA   rI   r   r   r   get_example_str{   s   
zPrompt.get_example_strkwargst.Anyr   c                 K  sR   t | jt | krtd| j dt|  |  }t|jdi |dS )zb
        Format the Prompt object into a ChatPromptTemplate object to be used in metrics.
        zInput variables z( do not match with the given parameters r   Nr   )setr!   keysr,   listr   r   format)r   rN   promptr   r   r   rS      s   zPrompt.formatNllmr   	cache_dirt.Optional[str]c           	   
     s  fdd| j kr| S |r|nt }tjtj|| j dr,| | j|S t	d| j g }g | j
D ]n | fdd| jD  || j dkr_tj | jdn
tj | jd | j dkr | j}t|trt|}t|tr| q;t|trtd	d
 |D rfdd|D  q;g |D ]}||jd d j qt| jd fddt dtD }t|t| j
ksJ dt!|D ]\ i }|"dd t#| j d t| j D  | j dkrt$% d |n d || j< | j dkrm|| j }t&| t|trLt'|(  ksKJ dt'|( d n!t|trmtdd
 |D rmtfdd
|D smJ d|| j
< q| _ | S )Nc                   s>   t  }|  D ]\}}|| t|tr| |}q|S r   )rP   rC   addr/   dictunion)Znested_jsonrQ   rA   rI   get_all_keysr   r   r\      s   

z"Prompt.adapt.<locals>.get_all_keys.jsonzAdapting %s to %sc                   s    g | ]}t j |d qS )translate_toinput)str_translationrS   r+   r?   )r3   r&   r   r   
<listcomp>   s    
z Prompt.adapt.<locals>.<listcomp>r#   r^   c                 s      | ]}t |tV  qd S r   r/   rY   r@   itemr   r   r   rB          

zPrompt.adapt.<locals>.<genexpr>c                   s   g | ]} |qS r   r   re   r[   r   r   rb      s    r   r*   c                   s   g | ]
}||   qS r   r   )r@   i)per_example_itemsresultsr   r   rb      s    z5examples and adapted examples must be of equal lengthc                 S  s   i | ]\}}||qS r   r   )r@   kvr   r   r   
<dictcomp>   s    z Prompt.adapt.<locals>.<dictcomp>z'Adapted output keys set(output.keys())=z< do not match with the original output keys: output_keys[i]=c                 s  rc   r   rd   re   r   r   r   rB      rg   c                 3  s$    | ]}t |   v V  qd S r   )rP   rQ   re   )rh   output_keysr   r   rB      s    
z>Adapted output keys do not match with the original output keys))r&   r
   ospathexistsrH   r   _loadloggerinfor    extendr!   appendr$   r.   json_translatationrS   r+   r"   ra   r/   r   r#   r0   rY   rR   allZgenerate_textZgenerationstextrL   ranger-   updatezipr	   Z
_safe_loadprintrP   rQ   )	r   r&   rU   rV   ZpromptsoutputpZgrouped_resultsZexample_dictr   )r3   r\   rh   r&   ro   ri   rj   r   adapt   s   
 
	


"

zPrompt.adaptNonec                 C  s   |r|nt  }tj|| j}tj|st| tj|| j d}t|d}t	j
|  |dd W d    d S 1 s@w   Y  d S )Nr]   w   )indent)r
   rp   rq   rH   r&   rr   makedirsr   openr#   dumprY   )r   rV   
cache_pathfiler   r   r   save   s   
"zPrompt.savec                 C  s<   t d|| tj||| d}| di tt|S )NzLoading %s from %sr]   r   )rt   ru   rp   rq   rH   r#   loadr   )r1   r&   r   rV   rq   r   r   r   rs     s   zPrompt._load)r'   r(   r   r(   r   )rJ   rK   r   r   )rN   rO   r   r   r   )r&   r   rU   r   rV   rW   r   r   )rV   rW   r   r   )r&   r   r   r   rV   r   r   r   )r   r   r   __doc__r   r    r$   r&   r   r5   r   rM   rS   r   r   classmethodrs   r   r   r   r   r       s&   
 
#
!
`
r   ra   zLanguage translationZhindiz7Who was  Albert Einstein and what is he best known for?u   अल्बर्ट आइंस्टीन कौन थे और वे किसके लिए सबसे ज्यादा प्रसिद्ध हैं?)r_   r`   r   r_   r`   r   r   )r   r   r    r!   r"   r$   Zjson_translationzPTranslate values in given json to target language and output the translated jsonZ
statementsz$Albert Einstein was born in Germany.z<Albert Einstein was best known for his theory of relativity.us   अल्बर्ट आइंस्टीन का जन्म जर्मनी में हुआ था।u   अल्बर्ट आइंस्टीन अपने सापेक्षता के सिद्धांत के लिए सबसे अधिक प्रसिद्ध थे।JSON) 
__future__r   r#   loggingrp   typingtZlangchain_core.messagesr   r   Zlangchain_core.prompt_valuesr   ZBasePromptValueZlangchain_core.pydantic_v1r   r   Z
ragas.llmsr   Zragas.llms.json_loadr	   Zragas.utilsr
   Dictr   AnyZExample	getLoggerr   rt   r   ra   rx   r   r   r   r   <module>   s`    
 j
