
    -j2              
         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j        e          Z G d	 d
e          Z G d de          Z eddddddddddgddg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&          ZdS )'    )annotationsN)BaseMessageHumanMessage)PromptValue)	BaseModelroot_validator)BaseRagasLLM)json_loader)get_cache_dirc                  *    e Zd ZU ded<   ddZd	dZdS )
r   str
prompt_strreturnt.List[BaseMessage]c                H    t          |                                           gS )z$Return prompt as a list of Messages.)content)r   	to_stringselfs    [/lsinfo/ai/hellotax_ai/base_platform/venv/lib/python3.11/site-packages/ragas/llms/prompt.pyto_messageszPromptValue.to_messages   s     T^^%5%566677    c                    | j         S Nr   r   s    r   r   zPromptValue.to_string   s
    r   N)r   r   r   r   )__name__
__module____qualname____annotations__r   r    r   r   r   r      sG         OOO8 8 8 8     r   r   c                      e Zd ZU dZded<   ded<   dZ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            Z
d'dZd(dZd)dZ	 d*d+d"Zd*d,d$Zed-d%            ZdS ).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           	     x   |                     d          |                     d          dk    rt          d          |                     d          |                     d          g k    rt          d          |                     d          |                     d          dk    rt          d          |                     d	          r|d         }t          |d	                   D ]\  }}|d         D ]}||vrt          d
|dz    d| d           ||vrt          d
|dz    d| d          |d                                         dk    rg	 ||v r5t	          ||         t
                    rt          j        ||                    # t          $ r}t          | d|dz    d|           d}~ww xY w|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es          r   validate_promptzPrompt.validate_prompt8   s'   
 ::m$$,

=0I0IR0O0O:;;;::l##+vzz-/H/HB/N/N9:::::l##+vzz,/G/G2/M/M9:::::j!! 	-J(
);<<  G%l3  Gg--(dr!tddddd   . W,,$c2a4ccZccc   -(..00F::%00)'**=sCC @ $
7:+> ? ? ?%   ()^^r!t^^[\^^   ; s   9F
F6F11F6c                D   | j         g}| j        rE|                    d| j                            dd                              dd          z              d                    |          dz   }| j        r|dz  }| j        D ]}|                                D ]\  }}t          |t          t          f          }t          j        |d                              d	                                          }| j                                        d
k    r*|                    dd                              dd          n|}||sd| d| nd| d| dz  }|dz  }|dz  }| j        r'|d                    d | j        D                       z  }| j        r|d| j         dz  }|S )z@
        Generate the prompt string from the variables.
        
{{{}}}z
Examples:
Fensure_asciiutf8r+   : z: ``````z
Your actual task:
r&   c              3  (   K   | ]}d | d| dV  dS )r@   z: {rC   Nr!   ).0keys     r   	<genexpr>z#Prompt.to_string.<locals>.<genexpr>   s6      !S!SC"7s"7"7"7"7"7!S!S!S!S!S!Sr   z: 
)r%   r'   appendreplacejoinr(   itemsr7   dictlistr+   dumpsencodedecoder,   r6   r)   r*   )r   prompt_elementsr   r;   rL   valueis_jsons          r   r   zPrompt.to_string\   s     +,) 	""088dCCKKCQUVVW   YY//$6
= 	#/)J= # #")--//  JC(t==G
5u===DDVLLSSUU 
  +1133v== c40088dCCC" 
 &7+S++E+++6#66E666JJ
 d"

--
? 	T"''!S!S4?!S!S!SSSSJ? 	54t4444Jr   
example_nointc                   |t          | j                  k    rt          d| d          | j        |         }d}|                                D ]\  }}t	          j        |d                              d                                          }| j        	                                dk    r*|
                    dd	          
                    d
d          n|}|d| d| z  }d|z   dz   S )zA
        Get the example string from the example number.
        zexample number z is out of ranger&   FrE   rG   r+   rA   rB   rC   rD   r@   rH   rI   )lenr(   r4   rQ   r+   rT   rU   rV   r,   r6   rO   )r   rZ   r;   example_strrL   rX   s         r   get_example_strzPrompt.get_example_str   s    T]++++KzKKKLLL-
+!--// 	/ 	/JCJu5999@@HHOOQQE #))++v55 c4((00d;;; 
 ...u...KK{"U**r   kwargst.Anyr   c           	     @   t          | j                  t          |                                          k    r9t          d| j         dt	          |                                                     |                                 }t           |j        di |          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   r!   )setr)   keysr4   rS   r   r   format)r   r`   prompts      r   re   zPrompt.format   s     t3v{{}}#5#555q4?qq\`agalalanan\o\oqq   !!mfm&=&=f&=&=>>>>r   Nllmr	   	cache_dirt.Optional[str]c                	  	
 
fd
| j         k    r| S |r|nt                      }t          j                            t          j                            || j         d                    r|                     | j        |          S t          	                    d| j                   g }g | j
        D ]	|                    	fd| j        D                        |                    | j                                        dk    r4t                               	                    | j                            n3t(                              	                    | j                                       | j                                        dk    rǉ	                    | j                  }t+          |t,                    rt/          j        |          }t+          |t2                    r                      
|                     Wt+          |t4                    r:t7          d |D                       r!                    
fd|D                        g |D ]@}                    |                    |          j        d	         d	         j                   At?          | j                  d
z   fdtA          d	t?                              D             }t?          |          t?          | j
                  k    s
J d            tC          |          D ]\  	i }|"                    d tG          | j        	d t?          | j                                     D                        | j                                        dk    rtI          j%        	d         |          n	d         || j        <   | j                                        dk    r|| j                 }t+          |t2                    ratM          |'                                                   k    s5J dtM          |'                                          d                     nTt+          |t4                    r?t7          d |D                       r&t7          fd|D                       s
J d            || j
        <   | _         | S )Nc                    t                      }|                                 D ]M\  }}|                    |           t          |t                    r|                     |                    }N|S r   )rc   rQ   addr7   rR   union)nested_jsonrd   rL   rX   get_all_keyss       r   ro   z"Prompt.adapt.<locals>.get_all_keys   sm    55D)//11 ; ;
UeT** ;::ll5&9&9::DKr   .jsonzAdapting %s to %sc                n    g | ]1}t                                                   |                     2S )translate_toinput)str_translationre   r3   )rK   rL   r;   r.   s     r   
<listcomp>z Prompt.adapt.<locals>.<listcomp>   sQ         $**%-W[[5E5E +    r   r+   rr   c              3  @   K   | ]}t          |t                    V  d S r   r7   rR   rK   items     r   rM   zPrompt.adapt.<locals>.<genexpr>   =       6 6/3JtT**6 6 6 6 6 6r   c                &    g | ]} |          S r!   r!   )rK   rz   ro   s     r   rv   z Prompt.adapt.<locals>.<listcomp>   s#    'N'N'NtT(:(:'N'N'Nr   r   r2   c                *    g | ]}||z            S r!   r!   )rK   iper_example_itemsresultss     r   rv   z Prompt.adapt.<locals>.<listcomp>   s9     
 
 
 A---.
 
 
r   z5examples and adapted examples must be of equal lengthc                    i | ]\  }}||	S r!   r!   )rK   kvs      r   
<dictcomp>z Prompt.adapt.<locals>.<dictcomp>   s    XXX$!QAXXXr   z'Adapted output keys set(output.keys())=z< do not match with the original output keys: output_keys[i]=c              3  @   K   | ]}t          |t                    V  d S r   rx   ry   s     r   rM   zPrompt.adapt.<locals>.<genexpr>   r{   r   c              3  j   K   | ]-}t          |                                                   v V  .d S r   )rc   rd   )rK   rz   r~   output_keyss     r   rM   zPrompt.adapt.<locals>.<genexpr>   sM        ?CDIIKK((KN:     r   z>Adapted output keys do not match with the original output keys)(r.   r   ospathexistsrP   r$   _loadloggerinfor(   extendr)   rN   r,   r6   json_translatationre   r3   r*   ru   r7   r   r+   r8   rR   rS   allgenerate_textgenerationstextr]   ranger5   updatezipr
   
_safe_loadrc   rd   )r   r.   rg   rh   promptsoutputpgrouped_resultsexample_dictr;   ro   r~   r   r   r   s    `       @@@@@@r   adaptzPrompt.adapt   s   	 	 	 	 	 =H$$K "+?II	7>>"',,y(ty<O<O<OPPQQ 	>::h	9==='H===} 	P 	PGNN      $	     NN #))++v55 #))!)T_1M1M *    %++!)T_1M1M ,     %%''611 T_55fc** 0!Z//Ffd++ P&&||F';';<<<<-- P# 6 67=6 6 6 3 3 P  &&'N'N'N'Nv'N'N'NOOO  	H 	HANN3,,Q//;A>qAFGGGG0014
 
 
 
 
1c'll,=>>
 
 
 ?##sM(
 (
 
 
 
B
 
 
 $O44 	, 	,JAwLXX#dow?UT_AUAU?U7V"W"WXXX  
 #))++v55 &wr{C888R[ ) %%''611%do6fd++ 	XFKKMM**k!n<<< Bc&++--.@.@  B  Bp{|}p~  B  B =<<<-- X# 6 67=6 6 6 3 3 X      GM     X XWX X   ,DM!  r   Nonec                   |r|nt                      }t          j                            || j                  }t          j                            |          st          j        |           t          j                            || j         d          }t          |d          5 }t          j
        |                                 |d           d d d            d S # 1 swxY w Y   d S )Nrp   w   )indent)r   r   r   rP   r.   r   makedirsr$   openr+   dumprR   )r   rh   
cache_pathfiles       r   savezPrompt.save  s   !*?II	GLLDM::	w~~i(( 	#K	"""W\\)	-@-@-@AA
*c"" 	3dIdiikk42222	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3s   #*CC!Cc           	         t                               d||           t          j                            ||| d          } | di t          j        t          |                    S )NzLoading %s from %srp   r!   )r   r   r   r   rP   r+   loadr   )r9   r.   r$   rh   r   s        r   r   zPrompt._load  s\    ($	:::w||IxD@@s++TYtDzz**+++r   )r/   r0   r   r0   r   )rZ   r[   r   r   )r`   ra   r   r   r   )r.   r   rg   r	   rh   ri   r   r#   )rh   ri   r   r   )r.   r   r$   r   rh   r   r   r#   )r   r   r   __doc__r    r'   r(   r,   r.   r   r>   r   r_   re   r   r   classmethodr   r!   r   r   r#   r#       sL          III%''''' "H""""OOO,2K2222H! ! ! ^!F( ( ( (T+ + + +$	? 	? 	? 	? NR] ] ] ] ]~3 3 3 3 3 , , , [, , ,r   r#   ru   zLanguage translationhindiz7Who was  Albert Einstein and what is he best known for?u   अल्बर्ट आइंस्टीन कौन थे और वे किसके लिए सबसे ज्यादा प्रसिद्ध हैं?)rs   rt   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?rs   rt   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+   loggingr   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#   ru   r   r!   r   r   <module>r      sG   " " " " " "   				     = = = = = = = = G G G G G G @ @ @ @ @ @ @ @ # # # # # # , , , , , , % % % % % %
&ae
		8	$	$    /   p, p, p, p, p,Y p, p, p,f &	& $N D	
 	
 $NV	
 	
 (#  ( V	b $:R  J \	
 	
  $5= ;?	
 	
!@ (K& & &   r   