
    Wi:(                       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          a/home/sietch6/trending-topics-pipeline/venv/lib/python3.12/site-packages/instructor/core/patch.py__call__z'InstructorChatCompletionCreate.__call__&   s         NNNr   )r'   type[T_Model] | Noner(   dict[str, Any] | Noner)   r2   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   r0   )r'   r1   r(   r2   r)   r2   r*   int | AsyncRetryingr+   r   r,   r   r4   r   r5   r$   r/   r-   r:   r:   1   sY     0448)-+,, 2 '	
 )   
r/   r:   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.r   z:'validation_context' is deprecated. Use 'context' instead.r   
stacklevel)
exceptionsr>   warningswarnDeprecationWarning)r)   r(   r>   rB   s       r-   handle_contextrE   =   sX     1=2 \
 	
 %'/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   r   store_cached_responsettlr$   )rW   rT   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     	LK"(**Wd";

;5's;M 	 !*<=%: &
)&
8>&
"
 'zgN
 !; #
3 2>>*-2>>.1 nnW--#*4#9TZZs4yC 'uc>BC
$)#

 

 !;	5h,=)%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   r   rd   rf   r$   )rW   rT   rU   rV   rh   ri   rj   rE   r   r   rk   rl   r_   rm   r   rn   r   re   ro   rp   s                       r-   new_create_synczpatch.<locals>.new_create_sync   s|    	LK"(**Wd";

;5's;M 	 !*<=%: &
)&
8>&
"
 'zgN
 !; #
3 2>>*-2>>.1 nnW--#*4#9TZZs4yC 'uc>BC
)#

 !;	5h,=)%hIN x ' s   $D3 3	E ?E )NNNr   TN)r'   r1   r(   r2   r)   r2   r*   r<   rb   boolrc   Hooks | Noner+   T_ParamSpec.argsr,   T_ParamSpec.kwargsr4   r   )r'   r1   r(   r2   r)   r2   r*   r3   rb   rz   rc   r{   r+   r|   r,   r}   r4   r   )loggerdebugchatcompletionsrN   
ValueErrorr   r   )rH   rN   rI   func_is_asyncrw   ry   
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)   r2   r(   r2   r4   r2   )rH   r   rI   r   r4   r   )rH   r   rI   r   r4   r   )rN   zCallable[T_ParamSpec, T_Retval]rI   r   r4   r!   )rN   zAwaitable[T_Retval]rI   r   r4   r!   )rH   zOpenAI | AsyncOpenAI | NonerN   z&Callable[T_ParamSpec, T_Retval] | NonerI   r   r4   zOpenAI | AsyncOpenAI)/
__future__r   	functoolsr   typingr   r   r   r   r	   collections.abcr
   typing_extensionsr   openair   r   rn   r   processing.responser   retryr   r   utilsr   rc   r   
templatingr   rI   r   loggingtenacityr   r   	getLoggerr~   r   r   r   r!   r:   rE   TOOLSrJ   r   r$   r/   r-   <module>r      s   "   & ' &  7 *   *  
 
		<	(
)9
-:&	X 		( 	 &*04"- 8 
 
  
 
 
  
 
 )+)
) $) 
) 
 ))
) $) 
) +/59m'm2m m 	m` .2ZZ $r/   