
    {h(                       d dl mZ d dlmZ d dlmZmZmZ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 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 d dlmZ d dl Z d dl!m"Z"m#Z#  e jH                  d      Z% ede      Z& ed      Z' ed      Z( G d de      Z) G d de      Z*	 	 d 	 	 	 	 	 d!dZ+e	ejX                  f	 	 	 	 	 d"d       Z-e	ejX                  f	 	 	 	 	 d#d       Z-e	ejX                  f	 	 	 	 	 d$d       Z-e	ejX                  f	 	 	 	 	 d%d       Z-ddejX                  f	 	 	 	 	 	 	 d&dZ-ejX                  fd#dZ.y)'    )annotations)wraps)AnyCallableProtocolTypeVaroverload)	Awaitable)	ParamSpec)AsyncOpenAIOpenAI	BaseModel)handle_response_model)retry_async
retry_sync)is_async)Hooks)handle_templating)ModeN)AsyncRetryingRetrying
instructorT_Model)boundT_RetvalT_ParamSpecc                  8    e Zd Z	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 ddZy)InstructorChatCompletionCreateNc                     y N selfresponse_modelvalidation_contextcontextmax_retriesargskwargss          Q/var/www/html/hubwallet-dev/venv/lib/python3.12/site-packages/instructor/patch.py__call__z'InstructorChatCompletionCreate.__call__&   s         NNN   )r%   type[T_Model] | Noner&   dict[str, Any] | Noner'   r1   r(   int | Retryingr)   r   r*   r   returnr   __name__
__module____qualname__r,   r"   r-   r+   r   r   %   sY     0448)-&', 2 '	
 $   
r-   r   c                  8    e Zd Z	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 ddZy)#AsyncInstructorChatCompletionCreateNc                   K   y wr!   r"   r#   s          r+   r,   z,AsyncInstructorChatCompletionCreate.__call__2   s      s   r.   )r%   r0   r&   r1   r'   r1   r(   int | AsyncRetryingr)   r   r*   r   r3   r   r4   r"   r-   r+   r9   r9   1   sY     0448)-+,, 2 '	
 )   
r-   r9   c                n    | |ddl m}  |d      | | ddl}|j                  dt        d       |} | S )z
    Handle the context and validation_context parameters.
    If both are provided, raise an error.
    If validation_context is provided, issue a deprecation warning and use it as context.
    If neither is provided, return None.
    Nr   )ConfigurationErrorzNCannot provide both 'context' and 'validation_context'. Use 'context' instead.z:'validation_context' is deprecated. Use 'context' instead.   
stacklevel)instructor.exceptionsr=   warningswarnDeprecationWarning)r'   r&   r=   rB   s       r+   handle_contextrE   =   sX     1=< \
 	
 %'/H 	 	

 %Nr-   c                     y r!   r"   clientmodes     r+   patchrJ   Y   s     r-   c                     y r!   r"   rG   s     r+   rJ   rJ   `   s     r-   c                     y r!   r"   createrI   s     r+   rJ   rJ   g        &)r-   c                     y r!   r"   rM   s     r+   rJ   rJ   n   rO   r-   c                   t         j                  d       ||n.| !| j                  j                  j                  nt        d      t              }t              	 	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dfd       }t              	 	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dfd       }|r|n|}| || j                  j                  _        | S |S )a  
    Patch the `client.chat.completions.create` method

    Enables the following features:

    - `response_model` parameter to parse the response from OpenAI's API
    - `max_retries` parameter to retry the function if the response is not valid
    - `validation_context` parameter to validate the response using the pydantic model
    - `strict` parameter to use strict json parsing
    - `hooks` parameter to hook into the completion process
    z4Patching `client.chat.completions.create` with mode=z(Either client or create must be providedc                  K   ddl m}m}	m}
 |j	                  dd       }|j	                  dd       }t        |t              r|nd }t        ||      }t        d| d|\  } }t        ||      }||  |	|j                  d      xs$ |j                  d      xs |j                  d	      |j                  d
      | t        d      rj                  n
t                    } |
|||       }||S t        | ||||||	       d {   }|)| '	 ddlm} t        ||      rddl m}  ||||       |S |S 7 1# t$        $ r Y |S w xY ww)Nr   	BaseCachemake_cache_keyload_cached_responsecache	cache_ttlr%   rI   rI   r'   messagescontentschat_historymodelvaluer[   r^   r%   rI   )	funcr%   r'   r(   r)   r*   strictrI   hooksr   store_cached_responsettlr"   )instructor.cacherT   rU   rV   pop
isinstanceintrE   r   r   gethasattrr_   strr   pydanticr   re   ModuleNotFoundErrorr%   r&   r'   r(   rb   rc   r)   r*   rT   rU   rV   rW   cache_ttl_rawrX   
new_kwargskeyobjresponse_BMre   ra   rI   s                       r+   new_create_asynczpatch.<locals>.new_create_async   s     	UT"(**Wd";

;5's;M 	 !*<=%: &
)&
8>&
"
 'zgN
 !; #
3 2>>*-2>>.1 nnW--#*4#9TZZs4yC 'uc>BC
$)#

 

 !;	5h,F)%hIN x1

, ' s6   DED=E$D? 9E?	EEEEc                   ddl m}m}	m}
 |j	                  dd       }|j	                  dd       }t        |t              r|nd }t        ||      }t        d| d|\  } }t        ||      }||  |	|j                  d      xs$ |j                  d      xs |j                  d	      |j                  d
      | t        d      rj                  n
t                    } |
|||       }||S t        | ||||||	      }|)| '	 ddlm} t        ||      rddl m}  ||||       |S |S # t$        $ r Y |S w xY w)Nr   rS   rW   rX   rY   rZ   r[   r\   r]   r^   r_   r`   )	ra   r%   r'   r(   r)   rc   rb   r*   rI   r   rd   rf   r"   )rh   rT   rU   rV   ri   rj   rk   rE   r   r   rl   rm   r_   rn   r   ro   r   re   rp   rq   s                       r+   new_create_synczpatch.<locals>.new_create_sync   s|    	UT"(**Wd";

;5's;M 	 !*<=%: &
)&
8>&
"
 'zgN
 !; #
3 2>>*-2>>.1 nnW--#*4#9TZZs4yC 'uc>BC
)#

 !;	5h,F)%hIN x ' s   $D3 3	E ?E )NNNr/   TN)r%   r0   r&   r1   r'   r1   r(   r;   rb   boolrc   Hooks | Noner)   T_ParamSpec.argsr*   T_ParamSpec.kwargsr3   r   )r%   r0   r&   r1   r'   r1   r(   r2   rb   r{   rc   r|   r)   r}   r*   r~   r3   r   )loggerdebugchatcompletionsrN   
ValueErrorr   r   )rH   rN   rI   func_is_asyncrx   rz   
new_createra   s     `    @r+   rJ   rJ   u   s   " LLH4'JK		{{&&--CDDTNM
4[/348)-+,"B,B1B 'B )	B
 B B  B %B 
B BH 4[/348)-&'"C,C1C 'C $	C
 C C  C %C 
C CJ &3!J)3&r-   c                T    ddl }|j                  dt        d       t        | |      S )a  
    No longer necessary, use `patch` instead.

    Patch the `client.chat.completions.create` method

    Enables the following features:

    - `response_model` parameter to parse the response from OpenAI's API
    - `max_retries` parameter to retry the function if the response is not valid
    - `validation_context` parameter to validate the response using the pydantic model
    - `strict` parameter to use strict json parsing
    r   Nz'apatch is deprecated, use patch insteadr>   r?   )rI   )rB   rC   rD   rJ   )rH   rI   rB   s      r+   apatchr   %  s0     MM1  
 d##r-   )NN)r'   r1   r&   r1   r3   r1   )rH   r   rI   r   r3   r   )rH   r   rI   r   r3   r   )rN   zCallable[T_ParamSpec, T_Retval]rI   r   r3   r   )rN   zAwaitable[T_Retval]rI   r   r3   r   )rH   zOpenAI | AsyncOpenAI | NonerN   z&Callable[T_ParamSpec, T_Retval] | NonerI   r   r3   zOpenAI | AsyncOpenAI)/
__future__r   	functoolsr   typingr   r   r   r   r	   collections.abcr
   typing_extensionsr   openair   r   ro   r   instructor.process_responser   instructor.retryr   r   instructor.utilsr   instructor.hooksr   instructor.templatingr   instructor.moder   loggingtenacityr   r   	getLoggerr   r   r   r   r   r9   rE   TOOLSrJ   r   r"   r-   r+   <module>r      s   "   & ' &  = 4 % " 3   
 
		<	(
)9
-:&	X 		( 	 &*04"- 8 
 
  
 
 
  
 
 )+)
) $) 
) 
 ))
) $) 
) +/59m'm2m m 	m` .2ZZ $r-   