
    Pi                        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 dS )    )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<   dS )	ErrorResponsezOpenAI style error responsestrmessagetypezOptional[str]paramcodeN)__name__
__module____qualname____doc____annotations__     k/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/llama_cpp/server/errors.pyr   r      sB         %%LLLIIIr#   r   c                  B    e Zd ZdZed	d            Zed	d            ZdS )
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
    request?Union['CreateCompletionRequest', 'CreateChatCompletionRequest']returnTuple[int, ErrorResponse]c           	     "   t          |                    d                    }t          |                    d                    }| j        }t          | d          rd}nd}dt	          |                    ||p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)r'   matchcontext_windowprompt_tokenscompletion_tokensr   s         r$   r1   z/ErrorResponseFormatters.context_length_exceeded/   s     U[[^^,,EKKNN++#.7J'' 	J GC  MNN"'a=8!	  )*

 

 

 

 
	
r#   c                |    t          |                    d                    }d| d}dt          |ddd          fS )	z#Formatter for model_not_found errorr-   zThe model `z` does not existr/   r0   Nmodel_not_foundr2   )r   r4   r   )r'   r8   
model_pathr   s       r$   r=   z'ErrorResponseFormatters.model_not_foundU   sU     Q((
<
<<<M("	
 
 
 
 	
r#   N)r'   r(   r)   r*   )r   r   r   r    staticmethodr1   r=   r"   r#   r$   r&   r&   #   s_        	 	 #
 #
 #
 \#
J 
 
 
 \
 
 
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_formattersNerror	ExceptionbodycOptional[Union['CreateChatCompletionRequest', 'CreateCompletionRequest', 'CreateEmbeddingRequest']]r)   r*   c                   |mt          |t          t          f          rQ| j                                        D ]7\  }}|                    t          |                    }| |||          c S 8t          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_errorr2   )
isinstancer   r   rB   itemssearchr   printsysstderr	traceback	print_excr   )selfrC   rE   patterncallbackr8   s         r$   error_message_wrapperz'RouteErrorHandler.error_message_wrapper}   s     
'+!
 !
 &*%@%F%F%H%H 1 1!s5zz22$#8D%00000 % 	(CJJ((sz::::,,,, MJJ(	
 
 
 
 	
r#   4Callable[[Request], Coroutine[None, None, Response]]c                Z     t                                                      d fd}|S )zgDefines custom route handler that catches exceptions and formats
        in OpenAI style error responser'   r   r)   r   c                  K   	 t          j                    } 
|            d {V }t          t          j                    |z
  dz            }| |j        d<   |S # t          $ r}|d }~wt
          $ r}|                                  d {V }	 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
                    ||          \  }}	t          d|	i|          cY d }~S d }~ww xY w)	Ni  zopenai-processing-msr.   prompt)rC   rE   rC   )status_coder"   )timeperf_counterr3   headersr   rD   jsonr   r   r   rU   r   )r'   	start_secresponseelapsed_time_msunauthorizedexc	json_bodyrE   rZ   error_messageoriginal_route_handlerrR   s             r$   custom_route_handlerzARouteErrorHandler.get_route_handler.<locals>.custom_route_handler   s     ' -//	!7!7!@!@@@@@@@"%t'8':':Y'F$&N"O"O>M;O !78  # # #""   "),,..000000	 !Y.. 8DD)DD  "Y..6CCCC  6BB	BB       DDD  ..St.DD!#m, +        7sN   AA 
D&A((D5D.B?>D?CDC/D=DD)r'   r   r)   r   )superget_route_handler)rR   rg   rf   	__class__s   ` @r$   ri   z#RouteErrorHandler.get_route_handler   sI     "'!:!:!<!<(	 (	 (	 (	 (	 (	 (	T $#r#   )N)rC   rD   rE   rF   r)   r*   )r)   rV   )r   r   r   r    r   r&   r1   r=   rB   r!   rU   ri   __classcell__)rj   s   @r$   rA   rA   f   s         HH 	H	
 	
":.	
 	
"2	     6 #
 #
 #
 #
 #
J2$ 2$ 2$ 2$ 2$ 2$ 2$ 2$ 2$ 2$r#   rA   )!
__future__r   rN   rP   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&   rA   r"   r#   r$   <module>rt      s   " " " " " " 



      & & & & & & & & & & D D D D D D D D D D D D D D D D ' ' ' ' ' '         
 + * * * * * $ $ $ $ $ $             I   @
 @
 @
 @
 @
 @
 @
 @
Fn$ n$ n$ n$ n$ n$ n$ n$ n$ n$r#   