
    {hF                       d dl m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mZ d dlmZ  ej                  d      Z ed	e
      Z ed      Z ed      Z ed      Z	 	 	 	 	 	 d%dZ	 	 	 	 	 	 d%dZ	 	 	 	 	 	 d%dZ	 	 	 	 	 	 d%dZ	 	 	 	 	 	 d%dZ	 	 	 	 	 	 d%dZ	 	 	 	 	 	 d%dZ	 	 	 	 	 	 d%dZ	 	 	 	 	 	 d%dZ	 	 	 	 	 	 d%dZ	 	 	 	 	 	 d%dZ	 	 	 	 	 	 d%dZ 	 	 	 	 	 	 d%dZ!d%dZ"	 	 	 	 	 	 d%dZ#	 	 	 	 	 	 d%dZ$	 	 	 	 	 	 d%dZ%	 	 	 	 	 	 d%dZ&	 	 	 	 	 	 d%d Z'	 	 	 	 	 	 d%d!Z(	 	 	 	 	 	 d%d"Z)	 	 	 	 	 	 d%d#Z*	 	 	 	 	 	 	 	 d&d$Z+y)'    )annotationsN)AnyTypeVar)Mode)dump_message)	BaseModelValidationError)	ParamSpec
instructorT_Model)boundT_RetvalT_ParamSpecTc                   | j                         } ddlm} t        ||      sJ d       g }d }|j                  D ]h  }|j                  |j                                |j                  dk(  s2t        |t              sC|j                  |j                  k(  s]|j                  }j d|dg}||j                  dd|d	| d
ddgd       n|j                  dd| d
d       | d   j                  |       | S )Nr   Message$Response must be a Anthropic Messagetool_use	assistantrolecontentusertool_resultValidation Error found:
.
Recall the function correctly, fix the errorsT)typetool_use_idr   is_errorz,Validation Error due to no tool invocation:
messages)copyanthropic.typesr   
isinstancer   append
model_dumpr   r	   nametitleidextend)kwargsresponse	exceptionr   assistant_contentr   r   
reask_msgss           Q/var/www/html/hubwallet-dev/venv/lib/python3.12/site-packages/instructor/reask.pyreask_anthropic_toolsr1      s"   
 [[]F'h(P*PP(K## %  !3!3!56LLJ&9o6	/!**K% '3DEFJ !.'2%>ykIx#y$(	
	
 	J9+  VE  F	
 :j)M    c                   | j                         } ddlm} t        ||      sJ d       |j                  D cg c]  }|j
                  dk(  s| }}|sd}n|d   j                  }dd| d	| d
}| d   j                  |       | S c c}w )Nr   r   r   textz!No text content found in responser   Validation Errors found:
O
Recall the function correctly, fix the errors found in the following attempt:
r   r!   )r"   r#   r   r$   r   r   r4   r%   )r+   r,   r-   r   ctext_blockstext_content	reask_msgs           r0   reask_anthropic_jsonr<   B   s    
 [[]F'h(P*PP( '..C!&&F2B1CKC: #2++ 1)  =N  O[  N\  _I :i(M Ds   BBc                    | j                  dd      }d| v r| d   j                  d|d       n	d|dg| d<   d|j                   d| | d<   | S )Nmessage chat_historyr   )r   r>   MCorrect the following JSON response, based on the errors given below:

JSON:


Exceptions:
)getr%   r4   )r+   r,   r-   r>   s       r0   reask_cohere_toolsrD   ]   su     jjB'G ~%%v'&JK ,2g"F!G~	-- 1)	> 9 Mr2   c           
        ddl m} d|j                  |j                  d   j                  j
                  |j                  d   j                  j                        gdd|j                  |j                  |j                  d   j                  j
                  dd| i	      
      gdddgdg}| d   j                  |       | S )Nr   )generativelanguagemodelr'   argsr   partsfunctionerrorzValidation Error(s) found:
)r'   r,   )function_responser   z:Recall the function arguments correctly and fix the errorscontents)
	google.airF   FunctionCallrK   function_callr'   rI   PartFunctionResponser*   )r+   r,   r-   glmr/   s        r0   reask_gemini_toolsrV   u   s    
 4   !*88==!*88== ! 	
 &)&:&:%^^A.<<AA")-I)+U!V '; '  
	
 RS	
+J4 :j)Mr2   c                V    | d   j                  dd|j                   d| gd       | S )NrO   r   rA   rB   rJ   )r%   r4   )r+   r,   r-   s      r0   reask_gemini_jsonrX      sF    
 :"--(9)F	
 Mr2   c                    ddl m} | j                         } |j                  d   j                   |||      g}| d   j                  |       | S )N   )!vertexai_function_response_parserr   rO   )client_vertexair[   r"   
candidatesr   r*   )r+   r,   r-   r[   r/   s        r0   reask_vertexai_toolsr^      sR    
 C[[]FA&&)(I>J :j)Mr2   c                    ddl m} | j                         } |j                  d   j                   |dd| d|j
                   d      g}| d   j                  |       | S )	NrZ   )vertexai_message_parserr   r   r6   r7   r   rO   )r\   r`   r"   r]   r   r4   r*   )r+   r,   r-   r`   r/   s        r0   reask_vertexai_jsonra      su    
 9[[]F 	A&&0 <GGO}}oW	
J :j)Mr2   c           	     T   | j                         } t        |j                  d   j                        g}|j                  d   j                  j                  D ]:  }|j                  d|j                  |j                  j                  d| dd       < | d   j                  |       | S Nr   toolr   r   )r   tool_call_idr'   r   r!   
r"   r   choicesr>   
tool_callsr%   r)   rL   r'   r*   r+   r,   r-   r/   	tool_calls        r0   reask_toolsrk          
 [[]Fx//2::;<J%%a(00;; 

	 )!**///	{:ij		


 :j)Mr2   c                    | j                         } g }|j                  D ]&  }|j                  dd| d|j                   d       ( | d   j	                  |       | S )Nr   r   z4
Recall the function correctly, fix the errors with r   r!   )r"   outputr%   	argumentsr*   )r+   r,   r-   reask_messagesrj   s        r0   reask_responses_toolsrq      s    
 [[]FN__ 
	/	{:opy  qD  qD  pE  F	

 :n-Mr2   c                p   | j                         } t        |j                  d   j                        g}|j                  d   j                  j                  D ]H  }|j                  dd| d|j                  j                   d|j                  j                   dd       J | d   j                  |       | S )	Nr   r   r   zA
Recall the function correctly, fix the errors and call the tool z. again, taking into account the problems with z that was previously generated.r   r!   )
r"   r   rg   r>   rh   r%   rL   r'   ro   r*   ri   s        r0   reask_cerebras_toolsrs      s    
 [[]Fx//2::;<J%%a(00;; 

	/	{ ;88A8J8J8O8O7P Q==F=O=O=Y=Y<ZZy{		


 :j)Mr2   c                    | j                         } t        |j                  d   j                        g}|j	                  dd| d       | d   j                  |       | S Nr   r   @Correct your JSON ONLY RESPONSE, based on the following errors:
r   r!   r"   r   rg   r>   r%   r*   r+   r,   r-   r/   s       r0   reask_md_jsonry     i    
 [[]Fx//2::;<JZ[dZef	
 :j)Mr2   c                    | j                         } |d   d   g}|j                  ddd| igd       | d   j                  |       | S )Nrn   r>   r   r4   rv   r   r!   )r"   r%   r*   rx   s       r0   reask_bedrock_jsonr|   '  sl    
 [[]F8$Y/0J _`i_jk	
	 :j)Mr2   c                    | j                         } t        |j                  d   j                        g}|j	                  dd| d       | d   j                  |       | S )Nr   r   z@Recall the function correctly, fix the errors, exceptions found
r   r!   rw   rx   s       r0   reask_defaultr~   <  si    
 [[]Fx//2::;<JST]S^_	
 :j)Mr2   c           	     T   | j                         } t        |j                  d   j                        g}|j                  d   j                  j                  D ]:  }|j                  d|j                  |j                  j                  d| dd       < | d   j                  |       | S rc   rf   ri   s        r0   reask_fireworks_toolsr   O  s    [[]Fx//2::;<J%%a(00;; 

	 )!**///	{:ij		


 :j)Mr2   c                    | j                         } t        |j                  d   j                        g}|j	                  dd| d       | d   j                  |       | S ru   rw   rx   s       r0   reask_fireworks_jsonr   a  rz   r2   c                    | j                         } t        |j                  d   j                        g}|j	                  dd| dd       | d   j                  |       | S )Nr   r   r   a@  
 Fix errors and fill tool call arguments/name correctly. Just update arguments dict values or update name. Don't change the structure of them. You have to call function by passing desired functions name/args as part of special attribute with name tools_calls, not as text in attribute with name content. IT'S IMPORTANT!r   r!   rw   rx   s       r0   reask_writer_toolsr   r  sp    
 [[]Fx//2::;<J+I; 7N O		
 :j)Mr2   c                
   | j                         } t        |j                  d   j                        g}|j	                  dd|j                  d   j                  j
                   d| d       | d   j                  |       | S )Nr   r   zCorrect your JSON response: z!, based on the following errors:
r   r!   )r"   r   rg   r>   r%   r   r*   rx   s       r0   reask_writer_jsonr     s    
 [[]Fx//2::;<J5h6F6Fq6I6Q6Q6Y6Y5Z [//8k;	
 :j)Mr2   c                    | j                         } t        |j                  d   j                        g}|j	                  dd| d       | d   j                  |       | S )z)Handle reasking for Perplexity JSON mode.r   r   rv   r   r!   rw   rx   s       r0   reask_perplexity_jsonr     si     [[]Fx//2::;<JZ[dZef	
 :j)Mr2   c           
     x   ddl m} | j                         } |j                  d   j                  j
                  d   j                  }| d   j                  |j                  |j                  j                  |j                  |j                        |j                  j                  d| d      g             | S )	Nr   typesrO   rH   r   r   r4   rK   )google.genair   r"   r]   r   rK   rR   r%   ModelContentrS   from_function_callr'   rI   	from_text)r+   r,   r-   r   rR   s        r0   reask_genai_toolsr     s    
 #[[]F''*2288;IIM
:

--&++&++ .  

$$4YK?no %  	 
	
 Mr2   c           
         ddl m} | j                         } |rt        |d      r|j                  nd}| d   j                  |j                  |j                  j                  d| d|       g	             | S )
Nr   r   r4   z]You must generate a response to the user's request that is consistent with the response modelrO   r   zI
Recall the function correctly, fix the errors in the following attempt:
r   r   )	r   r   r"   hasattrr4   r%   r   rS   r   )r+   r,   r-   r   genai_responses        r0   reask_genai_structured_outputsr     s    
 #[[]F &1 	l  :

$$4YK  @K  LZ  K[  \ %  	 	
 Mr2   c                    | j                         } d|j                  d   j                  j                  dg}|j	                  dd| dd       | d   j                  |       | S )Nr   r   r   r   r   r   r!   )r"   rg   r>   r   r%   r*   rx   s       r0    reask_mistral_structured_outputsr     sy    
 [[]F  ''*22::	
J +I;6ef	
 :j)Mr2   c           	     T   | j                         } t        |j                  d   j                        g}|j                  d   j                  j                  D ]:  }|j                  d|j                  |j                  j                  d| dd       < | d   j                  |       | S rc   rf   ri   s        r0   reask_mistral_toolsr     rl   r2   c                   | j                         }|t        j                  t        j                  hv rt	        |||      S |t        j
                  k(  rt        |||      S |t        j                  t        j                  hv rt        |||      S |t        j                  k(  rt        |||      S |t        j                  k(  rt        |||      S |t        j                  k(  rt        |||      S |t        j                   k(  rt#        |||      S |t        j$                  t        j&                  hv rt)        |||      S |t        j*                  k(  rt-        |||      S |t        j.                  t        j0                  hv rt3        |||      S |t        j4                  k(  rt7        |||      S |t        j8                  k(  rt;        |||      S |t        j<                  k(  rt?        |||      S |t        j@                  k(  rtC        |||      S |t        jD                  k(  rtG        |||      S |t        jH                  k(  rtK        |||      S |t        jL                  k(  rtO        |||      S |t        jP                  k(  rtS        |||      S |t        jT                  k(  rtW        |||      S |t        jX                  k(  rt[        |||      S |t        j\                  k(  rt_        |||      S ta        |||      S )N)1r"   r   ANTHROPIC_TOOLSANTHROPIC_REASONING_TOOLSr1   ANTHROPIC_JSONr<   COHERE_TOOLSCOHERE_JSON_SCHEMArD   GEMINI_TOOLSrV   GEMINI_JSONrX   VERTEXAI_TOOLSr^   VERTEXAI_JSONra   TOOLSTOOLS_STRICTrk   CEREBRAS_TOOLSrs   RESPONSES_TOOLS"RESPONSES_TOOLS_WITH_INBUILT_TOOLSrq   MD_JSONry   FIREWORKS_TOOLSr   FIREWORKS_JSONr   WRITER_TOOLSr   WRITER_JSONr   BEDROCK_JSONr|   PERPLEXITY_JSONr   GENAI_TOOLSr   GENAI_STRUCTURED_OUTPUTSr   MISTRAL_STRUCTURED_OUTPUTSr   MISTRAL_TOOLSr   r~   )r+   moder,   r-   kwargs_copys        r0   handle_reask_kwargsr     s    ++-K $$d&D&DEE$[(IFF	$$	$#K9EE	$##T%<%<=	=!+xCC	""	"!+xCC	!!	! h	BB	$$	$#K9EE	##	#";)DD	$**d//0	0;)<<	$$	$#K9EE	$&&(O(OP	P$[(IFF		[(I>>	%%	%$[(IFF	$$	$#K9EE	""	"!+xCC	!!	! h	BB	""	"!+xCC	%%	%$[(IFF	!!	! h	BB	..	.-k8YOO	00	0/XyQQ	##	#";)DD[(I>>r2   )r+   dict[str, Any]r,   r   r-   	Exception)r+   r   r   r   r,   r   r-   r   ),
__future__r   loggingtypingr   r   instructor.moder   instructor.utilsr   pydanticr   r	   typing_extensionsr
   	getLoggerloggerr   r   r   r   r1   r<   rD   rV   rX   r^   ra   rk   rq   rs   ry   r|   r~   r   r   r   r   r   r   r   r   r   r    r2   r0   <module>r      s   "     ) / '			<	(
)9
-:&CL,,, ,^ 6 0""" "J "   2 , , , " * &$ " . $ $ 2 6 0 ,6?6?
6? 6? 	6?r2   