
    *i                        S SK Jr  S SKrS SKrS SKrS SKJrJrJr  S SK	J
r
JrJrJrJrJr  S SKJr  S SKJrJrJr  S SKJr  S SKJr  S S	KJrJrJr   " S
 S\5      r " S S5      r " S S\5      r g)    )annotationsN)compileMatchPattern)Callable	CoroutineOptionalTupleUnionDict)	TypedDict)RequestResponseHTTPException)JSONResponse)APIRoute)CreateCompletionRequestCreateEmbeddingRequestCreateChatCompletionRequestc                  B    \ rS rSr% SrS\S'   S\S'   S\S'   S\S'   S	rg
)ErrorResponse   zOpenAI style error responsestrmessagetypezOptional[str]paramcode N)__name__
__module____qualname____firstlineno____doc____annotations____static_attributes__r       U/var/www/html/ai-backend/venv/lib/python3.13/site-packages/llama_cpp/server/errors.pyr   r      s    %L
I
r&   r   c                  P    \ rS rSrSr\    SS j5       r\    SS j5       rSrg)ErrorResponseFormatters#   a  Collection of formatters for error responses.

Args:
    request (Union[CreateCompletionRequest, CreateChatCompletionRequest]):
        Request body
    match (Match[str]): Match object from regex pattern

Returns:
    Tuple[int, ErrorResponse]: Status code and error response
c           	        [        UR                  S5      5      n[        UR                  S5      5      nU R                  n[        U S5      (       a  SnOSnS[	        UR                  UU=(       d    SU-   UU5      SSS	S
94$ )z+Formatter for context length exceeded error      messageszThis model's maximum context length is {} tokens. However, you requested {} tokens ({} in the messages, {} in the completion). Please reduce the length of the messages or completion.zThis model's maximum context length is {} tokens, however you requested {} tokens ({} in your prompt; {} for the completion). Please reduce your prompt; or completion length.  r   invalid_request_errorcontext_length_exceededr   r   r   r   )intgroup
max_tokenshasattrr   format)requestmatchcontext_windowprompt_tokenscompletion_tokensr   s         r'   r1   /ErrorResponseFormatters.context_length_exceeded/   s     U[[^,EKKN+#..7J''J C  MNN"'a=8!	 )*

 

 
	
r&   c                ^    [        UR                  S5      5      nSU S3nS[        USSSS94$ )	z#Formatter for model_not_found errorr-   zThe model `z` does not existr/   r0   Nmodel_not_foundr2   )r   r4   r   )r8   r9   
model_pathr   s       r'   r?   'ErrorResponseFormatters.model_not_foundU   sE     Q(

|+;<M("	
 
 	
r&   r   N)r8   z?Union['CreateCompletionRequest', 'CreateChatCompletionRequest']returnTuple[int, ErrorResponse])	r   r    r!   r"   r#   staticmethodr1   r?   r%   r   r&   r'   r)   r)   #   sT    	 #
P#
 
##
 #
J 
P
 
#
 
r&   r)   c                     ^  \ rS rSr% Sr\" S5      \R                  \" S5      \R                  0r	S\
S'    S
     SS jjr  SU 4S jjrS	rU =r$ )RouteErrorHandlerf   z>Custom APIRoute that handles application errors and exceptionsz9Requested tokens \((\d+)\) exceed context window of (\d+)zModel path does not exist: (.+)zDict['Pattern[str]', Callable[[Union['CreateCompletionRequest', 'CreateChatCompletionRequest'], 'Match[str]'], Tuple[int, ErrorResponse]]]pattern_and_formattersc                   Ube  [        U[        [        45      (       aJ  U R                  R	                  5        H,  u  p4UR                  [        U5      5      nUc  M$  U" X%5      s  $    [        S[        U5       3[        R                  S9  [        R                  " [        R                  S9  S[        [        U5      SSSS94$ )z2Wraps error message in OpenAI style error responseNzException: )filei  internal_server_errorr2   )
isinstancer   r   rH   itemssearchr   printsysstderr	traceback	print_excr   )selferrorbodypatterncallbackr9   s         r'   error_message_wrapper'RouteErrorHandler.error_message_wrapper}   s     
'+!
 !
 &*%@%@%F%F%H!s5z2$#D00 &I 	CJ<(szz:, MJ(	
 
 	
r&   c                :   >^ ^ [         TT ]  5       mSUU 4S jjnU$ )z_Defines custom route handler that catches exceptions and formats
in OpenAI style error responsec                  >#     [         R                  " 5       nT
" U 5      I S h  vN n[        [         R                  " 5       U-
  S-  5      nU UR                  S'   U$  N:! [         a  nUeS nAf[
         a  nU R                  5       I S h  vN  n SU;   a  [        S0 UD6nO0SU;   a  [        S0 UD6nO[        S0 UD6nO! [
         a    S n Of = fTR                  XWS9u  nn	[        SU	0US9s S nA$ S nAff = f7f)	Ni  zopenai-processing-msr.   prompt)rU   rV   rU   )status_coder   )timeperf_counterr3   headersr   	Exceptionjsonr   r   r   rY   r   )r8   	start_secresponseelapsed_time_msunauthorizedexc	json_bodyrV   r^   error_messageoriginal_route_handlerrT   s             r'   custom_route_handlerARouteErrorHandler.get_route_handler.<locals>.custom_route_handler   s"    ' --/	!7!@@"%t'8'8':Y'F$&N"O>M=N  !78 A ! #"" "),,.00	 !Y. 8D)D  "Y.6CC  6B	B   D  ..S.D!#m, + 7s   D A" A 9A" D A" "
C?,A..C?;C:BC:C&C:'C8C:9CC:CC:C C:4C?5D:C??D)r8   r   rB   r   )superget_route_handler)rT   rl   rk   	__class__s   ` @r'   ro   #RouteErrorHandler.get_route_handler   s&     "'!:!<(	 (	T $#r&   r   )N)rU   rb   rV   zcOptional[Union['CreateChatCompletionRequest', 'CreateCompletionRequest', 'CreateEmbeddingRequest']]rB   rC   )rB   z4Callable[[Request], Coroutine[None, None, Response]])r   r    r!   r"   r#   r   r)   r1   r?   rH   r$   rY   ro   r%   __classcell__)rp   s   @r'   rF   rF   f   s}    H 	H	
"::.	
"22	  	 6 #
#

#
 
##
J2$	=2$ 2$r&   rF   )!
__future__r   rP   rR   r_   rer   r   r   typingr   r   r	   r
   r   r   typing_extensionsr   fastapir   r   r   fastapi.responsesr   fastapi.routingr   llama_cpp.server.typesr   r   r   r   r)   rF   r   r&   r'   <module>r{      sc    " 
   & & D D ' 
 + $ I @
 @
Fn$ n$r&   