
     sg                        d dl mZ d dlZd dlZd dlZd dlmZmZmZ d dl	m
Z
mZmZmZmZmZ d dlmZ d dlmZmZmZ d dlmZ d dlmZ d d	lmZmZmZ  G d
 de      Z G d d      Z G d de      Z y)    )annotationsN)compileMatchPattern)Callable	CoroutineOptionalTupleUnionDict)	TypedDict)RequestResponseHTTPException)JSONResponse)APIRoute)CreateCompletionRequestCreateEmbeddingRequestCreateChatCompletionRequestc                  :    e Zd ZU dZded<   ded<   ded<   ded<   y)	ErrorResponsezOpenAI style error responsestrmessagetypezOptional[str]paramcodeN)__name__
__module____qualname____doc____annotations__     J/var/www/html/venv/lib/python3.12/site-packages/llama_cpp/server/errors.pyr   r      s    %L
I
r#   r   c                  D    e Zd ZdZe	 	 	 	 dd       Ze	 	 	 	 dd       Zy)ErrorResponseFormattersa1  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           	         t        |j                  d            }t        |j                  d            }| j                  }t        | d      rd}nd}dt	        |j                  ||xs d|z   ||      ddd	
      fS )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$   r-   z/ErrorResponseFormatters.context_length_exceeded/   s     U[[^,EKKN+#..7J'J C  MNN"'a=8!	 )*

 

 
	
r#   c                d    t        |j                  d            }d| d}dt        |ddd      fS )	z#Formatter for model_not_found errorr)   zThe model `z` does not existr+   r,   Nmodel_not_foundr.   )r   r0   r   )r4   r5   
model_pathr   s       r$   r:   z'ErrorResponseFormatters.model_not_foundU   sE     Q(

|+;<M("	
 
 	
r#   N)r4   z?Union['CreateCompletionRequest', 'CreateChatCompletionRequest']returnTuple[int, ErrorResponse])r   r   r   r    staticmethodr-   r:   r"   r#   r$   r&   r&   #   sT    	 #
P#
 
##
 #
J 
P
 
#
 
r#   r&   c                       e Zd ZU dZ ed      ej                   ed      ej                  iZde	d<   	 d	 	 	 	 	 d	dZ
	 	 d
 fdZ xZS )RouteErrorHandlerz>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                   |_t        |t        t        f      rI| j                  j	                         D ],  \  }}|j                  t        |            }|# |||      c S  t        dt        |       t        j                         t        j                  t        j                         dt        t        |      ddd      fS )z2Wraps error message in OpenAI style error responseNzException: )filei  internal_server_errorr.   )
isinstancer   r   rA   itemssearchr   printsysstderr	traceback	print_excr   )selferrorbodypatterncallbackr5   s         r$   error_message_wrapperz'RouteErrorHandler.error_message_wrapper}   s     
'+!
 &*%@%@%F%F%H 1!s5z2$#D%001 	CJ<(szz:, MJ(	
 
 	
r#   c                6     t                   d fd}|S )zgDefines custom route handler that catches exceptions and formats
        in OpenAI style error responsec                  K   	 t        j                         } 
|        d {   }t        t        j                         |z
  dz        }| |j                  d<   |S 7 9# t        $ r}|d }~wt
        $ r}| j                          d {  7  }	 d|v rt        di |}nd|v rt        di |}nt        di |}n# t
        $ r d }Y nw xY wj                  ||      \  }}	t        d|	i|      cY d }~S d }~ww xY ww)	Ni  zopenai-processing-msr*   prompt)rN   rO   rN   )status_coder"   )timeperf_counterr/   headersr   	Exceptionjsonr   r   r   rR   r   )r4   	start_secresponseelapsed_time_msunauthorizedexc	json_bodyrO   rV   error_messageoriginal_route_handlerrM   s             r$   custom_route_handlerzARouteErrorHandler.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  ..St.D!#m, + 7s   D A  A8A  D A   	C=)A++C=7C8
BC8+B=<C8=CC8
C'C82C=3D 8C==D )r4   r   r<   r   )superget_route_handler)rM   rd   rc   	__class__s   ` @r$   rf   z#RouteErrorHandler.get_route_handler   s!     "'!:!<(	T $#r#   )N)rN   rZ   rO   zcOptional[Union['CreateChatCompletionRequest', 'CreateCompletionRequest', 'CreateEmbeddingRequest']]r<   r=   )r<   z4Callable[[Request], Coroutine[None, None, Response]])r   r   r   r    r   r&   r-   r:   rA   r!   rR   rf   __classcell__)rg   s   @r$   r@   r@   f   s}    H 	H	
"::.	
"22	  	 6 #
#

#
 
##
J2$	=2$ 2$r#   r@   )!
__future__r   rI   rK   rW   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&   r@   r"   r#   r$   <module>rq      sc    " 
   & & D D ' 
 + $ I @
 @
Fn$ n$r#   