o
    /if2                  	   @  s2  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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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   J/var/www/html/corbot_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Zded< ded< dZded< g Zded< dgZd	ed
< dZ	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d0dd Z	!d1d2d&d'Zd1d3d)d*Zed4d+d,Zd!S )5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.
        output_format_instruction (str): The output format 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 (Literal["json", "str"]): The type of the output (default: "json").
        language (str): The language of the prompt (default: "english").
     r   nameinstructionoutput_format_instructionzt.List[Example]exampleszt.List[str]
input_keys
output_keyjsonzt.Literal['json', 'str']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    Nr   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exampleinp_keyer   r   r   validate_prompt8   sB   

zPrompt.validate_promptc                 C  s*  | j g}| jr|d| jdddd  d|d }| jrs|d7 }| jD ]J}| D ]?\}}t|tt	f}t
j|ddd	 }| j d
krV|ddddn|}||scd| d| nd| d| d7 }q.|d7 }q(|d7 }| jr|ddd | jD 7 }| jr|d| j d7 }|S )z@
        Generate the prompt string from the variables.
        
{{{}}}z
Examples:
Fensure_asciiutf8r%   : z: ``````z
Your actual task:
r   c                 s  s"    | ]}d | d| dV  qdS )r8   z: {r;   Nr   .0keyr   r   r   	<genexpr>   s     z#Prompt.to_string.<locals>.<genexpr>z: 
)r    r!   appendreplacejoinr"   itemsr0   dictlistr%   dumpsencodedecoder&   r/   r#   r$   )r   prompt_elementsr   r4   rD   valueis_jsonr   r   r   r   \   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d| d S )zA
        Get the example string from the example number.
        zexample number z is out of ranger   Fr=   r?   r%   r9   r:   r;   r<   r8   r@   rA   )lenr"   r-   rI   r%   rL   rM   rN   r&   r/   rG   )r   rR   r4   example_strrD   rP   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-   rK   r   r   format)r   rW   promptr   r   r   r[      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ri|| j }t|trHt&|'  ksGJ dt&|' d n!t|tritdd
 |D ritfdd
|D siJ d|| j
< q| _ | S )Nc                   s>   t  }|  D ]\}}|| t|tr| |}q|S r   )rY   rI   addr0   rJ   union)nested_jsonrZ   rD   rP   get_all_keysr   r   rd      s   

z"Prompt.adapt.<locals>.get_all_keys.jsonzAdapting %s to %sc                   s    g | ]}t j |d qS )translate_toinput)str_translationr[   r,   rB   )r4   r(   r   r   
<listcomp>   s    
z Prompt.adapt.<locals>.<listcomp>r%   rf   c                 s      | ]}t |tV  qd S r   r0   rJ   rC   itemr   r   r   rE          

zPrompt.adapt.<locals>.<genexpr>c                   s   g | ]} |qS r   r   rm   rc   r   r   rj      s    r   r+   c                   s   g | ]
}||   qS r   r   )rC   i)per_example_itemsresultsr   r   rj      s    z5examples and adapted examples must be of equal lengthc                 S  s   i | ]\}}||qS r   r   )rC   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  rk   r   rl   rm   r   r   r   rE      ro   c                 3  s$    | ]}t |   v V  qd S r   )rY   rZ   rm   )rp   output_keysr   r   rE      s    
z>Adapted output keys do not match with the original output keys)(r(   r
   ospathexistsrH   r   _loadloggerinfor"   extendr#   rF   r&   r/   json_translatationr[   r,   r$   ri   r0   r   r%   r1   rJ   rK   allgenerate_textgenerationstextrT   ranger.   updatezipr	   
_safe_loadrY   rZ   )	r   r(   r]   r^   promptsoutputpgrouped_resultsexample_dictr   )r4   rd   rp   r(   rw   rq   rr   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 )Nre   w   )indent)r
   rx   ry   rH   r(   rz   makedirsr   openr%   dumprJ   )r   r^   
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 %sre   r   )r|   r}   rx   ry   rH   r%   loadr   )r2   r(   r   r^   ry   r   r   r   r{     s   zPrompt._load)r)   r*   r   r*   r   )rR   rS   r   r   )rW   rX   r   r   r   )r(   r   r]   r   r^   r_   r   r   )r^   r_   r   r   )r(   r   r   r   r^   r   r   r   )r   r   r   __doc__r   r   r!   r"   r#   r$   r&   r(   r   r7   r   rV   r[   r   r   classmethodr{   r   r   r   r   r       s(   
 
#
*
_
r   ri   zLanguage translationhindiz7Who was  Albert Einstein and what is he best known for?u   अल्बर्ट आइंस्टीन कौन थे और वे किसके लिए सबसे ज्यादा प्रसिद्ध हैं?)rg   rh   r   dutchz7Who was queen Elizabeth and what is she best known for?z>Wie was koningin Elizabeth en waar is zij het meest bekend om?rg   rh   r   r   )r   r    r"   r#   r$   r&   json_translationzPTranslate values in given json to target language and output the translated json
statementsz$Albert Einstein was born in Germany.z<Albert Einstein was best known for his theory of relativity.us   अल्बर्ट आइंस्टीन का जन्म जर्मनी में हुआ था।u   अल्बर्ट आइंस्टीन अपने सापेक्षता के सिद्धांत के लिए सबसे अधिक प्रसिद्ध थे।zParis is the capital of France.z'Croissants are a popular French pastry.z%Parijs is de hoofdstad van Frankrijk.z)Croissants zijn een populair Frans gebak.r%   ) 
__future__r   r%   loggingrx   typingtlangchain_core.messagesr   r   langchain_core.prompt_valuesr   BasePromptValuelangchain_core.pydantic_v1r   r   
ragas.llmsr   ragas.llms.json_loadr	   ragas.utilsr
   Dictr   AnyExample	getLoggerr   r|   r   ri   r   r   r   r   r   <module>   s    
 t 
