
    j                     P   d Z ddlZddlZddlmZmZ ddlmZmZm	Z	m
Z
mZ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mZmZ  G d
 de      Z G d de      Zde	e   de
e   fdZ	 ddede
e   defdZ 	 dde
eee   ef      de
e   de
e   fdZ!de	e   de	e   fdZ"y)z)
Utility functions for base LLM classes.
    N)ABCabstractmethod)AnyDictListOptionalTypeUnion)_parsing	_pydantic)	BaseModel)verbose_logger)AllMessageValuesChatCompletionToolCallChunk)MessageProviderSpecificModelInfoTokenCountResponsec                       e Zd Ze	 	 ddedeeeeef         deeeeef         deeeef      dedee	   fd       Z
e	 dd	ee   defd
       Zy)BaseTokenCounterNmodel_to_usemessagescontents
deploymentrequest_modelreturnc                    K   y wN )selfr   r   r   r   r   s         y/Users/manta/Documents/Projects/TheRoad-I1/backend/.venv/lib/python3.12/site-packages/litellm/llms/base_llm/base_utils.pycount_tokenszBaseTokenCounter.count_tokens   s      	s   custom_llm_providerc                      y)zr
        Returns True if we should the this API for token counting for the selected `custom_llm_provider`
        Fr   )r   r"   s     r    should_use_token_counting_apiz.BaseTokenCounter.should_use_token_counting_api   s         )N r   )__name__
__module____qualname__r   strr   r   r   r   r   r!   boolr$   r   r%   r    r   r      s     04 4S#X/0 4S#X/0	
 T#s(^,  
$	%   .2%c] 
 r%   r   c                   B   e Zd Zdedee   fdZe	 ddee   dee   dee   fd       Z	e
eddee   dee   fd              Ze
e	 ddee   dee   fd	              Ze	 	 dd
ededee   dededee   dee   defd       Ze
ededee   fd              Zdee   fdZy)BaseLLMModelInfomodelr   c                      y)zE
        Default values all models of this provider support.
        Nr   )r   r.   s     r    get_provider_infoz"BaseLLMModelInfo.get_provider_info*   s     r%   Napi_keyapi_basec                     g S )zF
        Returns a list of models supported by this provider.
        r   )r   r1   r2   s      r    
get_modelszBaseLLMModelInfo.get_models3   s	     	r%   c                      y r   r   )r1   s    r    get_api_keyzBaseLLMModelInfo.get_api_key<   s     	r%   c                      y r   r   )r2   s    r    get_api_basezBaseLLMModelInfo.get_api_baseA   s    
 	r%   headersr   optional_paramslitellm_paramsc                      y r   r   )r   r9   r.   r   r:   r;   r1   r2   s           r    validate_environmentz%BaseLLMModelInfo.validate_environmentH   s     	r%   c                      y)a2  
        Returns the base model name from the given model name.

        Some providers like bedrock - can receive model=`invoke/anthropic.claude-3-opus-20240229-v1:0` or `converse/anthropic.claude-3-opus-20240229-v1:0`
            This function will return `anthropic.claude-3-opus-20240229-v1:0`
        Nr   r.   s    r    get_base_modelzBaseLLMModelInfo.get_base_modelU   s     	r%   c                      y)z
        Factory method to create a token counter for this provider.
        
        Returns:
            Optional TokenCounterInterface implementation for this provider,
            or None if token counting is not supported.
        Nr   )r   s    r    get_token_counterz"BaseLLMModelInfo.get_token_counter`   s     r%   )NNr   )r'   r(   r)   r*   r   r   r0   r   r   r4   staticmethodr6   r8   dictr   r=   r@   r   rB   r   r%   r    r-   r-   )   s{    
+	, GK}7?}	c  Xc] hsm    "&3-	#  
  "&"&

 
 '(	

 
 
 #
 3-
 

 
 c hsm   8,<#= r%   r-   
tool_callsr   c                 x   | d   d   j                  d      }	 |zt        j                  |      }t        |t              r4|j                  d      x}!t        t        j                  |            }|S t        t        j                  |            }|S 	 y# t        j                  $ r t        |      cY S w xY w)z
    In JSON mode, Anthropic API returns JSON schema as a tool call, we need to convert it to a message to follow the OpenAI format

    r   function	argumentsNvalues)content)getjsonloads
isinstancerD   r   dumpsJSONDecodeError)rE   json_mode_content_strargsrI   _messages        r    !_convert_tool_response_to_messagerT   k   s     ,6a=+D+H+H+U6 ,::34D$%TXXh5G+G6*T"4::f+=> #4::d+;< -   64556s   AB 4 B B98B9response_formatref_templatec                     @| j                  d      dk(  r,t        j                  |       }|d   d   }fd} ||       |S | S )Ntypejson_schemaschemac                 P   | g fg}t               }|r|j                         \  }}t        |      }||v r&|j                  |       t	        |t
              ryd|v r.|d   }|j                  d      d   }j                  |      |d<   |j                         D ]3  \  }}	t	        |	t
        t        f      s|j                  |	||gz   f       5 nQt	        |t              rAt        |      D ]3  \  }
}t	        |t
        t        f      s|j                  |||
gz   f       5 |ry y )Nz$ref/r?   )setpopidaddrN   rD   splitformatitemslistappend	enumerate)rZ   stackvisitedobjpathobj_idref_path
model_namekviitemrV   s               r    update_refsz4_dict_to_response_format_helper.<locals>.update_refs   s   b\NEeG!IIK	TCW$F#c4(}#&v;%-^^C%8%<
&2&9&9
&9&KF #		1%a$6!LL!TQCZ9 !,  T*#,S>4%dT4L9!LL$s
);< $2' r%   )rK   copydeepcopy)rU   rV   modified_formatrZ   rs   s    `   r    _dict_to_response_format_helperrw      sU     O$7$7$?=$P--8 /9	=6 	Fr%   c                    | yt        | t              rt        | |      S t        j                  j                  |       st        d|        || j                  |      }nt        j                  |       }d|| j                  dddS )z
    Re-implementation of openai's 'type_to_response_format_param' function

    Used for converting pydantic object to api schema.
    Nz#Unsupported response_format type - )rV   rY   T)rZ   namestrict)rX   rY   )rN   rD   rw   r   _completionsis_basemodel_type	TypeErrormodel_json_schemar   to_strict_json_schemar'   )rU   rV   rZ   s      r    type_to_response_format_paramr      s     /4(.MM
   22?C=o=NOPP 222M00A #,,
 r%   r   c                     g }| D ]H  }|d   dk(  r-t        j                  d       |j                  d|d   d       8|j                  |       J |S )zO
    Translate `developer` role to `system` role for non-OpenAI providers.
    role	developerzCTranslating developer role to system role for non-OpenAI providers.systemrJ   )r   rJ   )r   debugrf   )r   new_messagesms      r    !map_developer_role_to_system_roler      s_     ,.LV9#  U a	l KL"  r%   r   )#__doc__rt   rL   abcr   r   typingr   r   r   r   r	   r
   
openai.libr   r   pydanticr   litellm._loggingr   litellm.types.llms.openair   r   litellm.types.utilsr   r   r   r   r-   rT   rD   r*   rw   r   r   r   r%   r    <module>r      s      # 9 9 *  + S V Vs .?s ?D01g6 :>&&)1#&	&V #'!eDOT$9:;!3-! d^!H#$	
r%   