
    3fihP                        d Z ddlZddlmZ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 ddlmZmZ i d	d
dddd
ddddddddddddddddddddddddd dd!d"i d#d"d$d
d%dd&d
d'dd(dd)dd*dd+dd,d-d.d-d/d0d1d0d2d"d3d"d4d5d6d5i d7d8d9d8d:d5d;d5d<dd=dd>d"d?d"d@dAdBdAdCdDdEdDdFdGdHdGdIdJdKdJdLdMi dNdMdOdPdQdPdRdGdSdGdTdUdVdUdWddXddYd-dZd-d[d0d\d0d]d^d_d^d`dadbdai dcdddedddfd^dgd^dhdidjdidkdddldddmd^dndidod^dpdidqdddrdddsdJdtdMdudJi dvdMdwdPdxdPdydzd{d
d|d}d~dzdd
ddzdd
ddddJddddddzddzddzi dddddddd}dd}dd}ddzddzddzddddddddddddddddi ddddddddddddddddddddddddddddddddzddzi ddddddddddddddddddddPddPddPddddddddddi ddddPddPddPddʓddʓddʓddddddddddddddԓdd-ddԓddԓi ddٓddԓddddddddԓddddddddddddddԓdd-ddddԓddԓi ddddddddԓdddddddddddd"dd"dd-dd-ddddddddi dddd8ddddd ddddddddd8ddddddd	dd
ddd"ddddԓd"ddd8dddʐdZ G d de      Z	 dej(                  ddedededefdZ	 dej(                  ddededededef
dZ G d de      Zy(  z(Callback Handler that prints to std out.    N)Enumauto)AnyDictList)warn_deprecated)BaseCallbackHandler)	AIMessage)ChatGeneration	LLMResultgpt-5g{GzT?zgpt-5-cachedgMb ?zgpt-5-2025-08-07zgpt-5-2025-08-07-cachedzgpt-5-completiong{Gz?zgpt-5-2025-08-07-completionz
gpt-5-minigMb0?zgpt-5-mini-cachedg-C6>zgpt-5-mini-2025-08-07zgpt-5-mini-2025-08-07-cachedzgpt-5-mini-completiongMb`?z gpt-5-mini-2025-08-07-completionz
gpt-5-nanog-C6
?zgpt-5-nano-cachedgh㈵>zgpt-5-nano-2025-08-07zgpt-5-nano-2025-08-07-cachedzgpt-5-nano-completiong-C6:?z gpt-5-nano-2025-08-07-completionzgpt-5-chat-latestzgpt-5-chat-latest-cachedzgpt-5-chat-latest-2025-08-07z#gpt-5-chat-latest-2025-08-07-cachedzgpt-5-chat-latest-completionz'gpt-5-chat-latest-2025-08-07-completiongpt-4.1zgpt-4.1-2025-04-14zgpt-4.1-cachedgMb@?zgpt-4.1-2025-04-14-cachedzgpt-4.1-completiongMb?zgpt-4.1-2025-04-14-completionzgpt-4.1-minizgpt-4.1-mini-2025-04-14zgpt-4.1-mini-cachedg-C6?zgpt-4.1-mini-2025-04-14-cachedzgpt-4.1-mini-completiong-C6Z?z"gpt-4.1-mini-2025-04-14-completionzgpt-4.1-nanozgpt-4.1-nano-2025-04-14zgpt-4.1-nano-cachedzgpt-4.1-nano-2025-04-14-cachedzgpt-4.1-nano-completionz"gpt-4.1-nano-2025-04-14-completionzgpt-4.5-previewg333333?zgpt-4.5-preview-2025-02-27zgpt-4.5-preview-cachedg333333?z!gpt-4.5-preview-2025-02-27-cachedzgpt-4.5-preview-completiong333333?z%gpt-4.5-preview-2025-02-27-completiono1gQ?zo1-2024-12-17z	o1-cachedgQ~?zo1-2024-12-17-cachedzo1-completiongQ?zo1-2024-12-17-completionzo1-prozo1-pro-2025-03-19zo1-pro-completiong333333?zo1-pro-2025-03-19-completiono3zo3-2025-04-16z	o3-cachedzo3-2025-04-16-cachedzo3-completionzo3-2025-04-16-completionzo4-minig/nR?zo4-mini-2025-04-16zo4-mini-cachedg/n2?zo4-mini-2025-04-16-cachedzo4-mini-completiong/nr?zo4-mini-2025-04-16-completionzo3-minizo3-mini-2025-01-31zo3-mini-cachedg/nB?zo3-mini-2025-01-31-cachedzo3-mini-completionzo3-mini-2025-01-31-completionzo1-minizo1-mini-cachedzo1-mini-2024-09-12zo1-mini-2024-09-12-cachedzo1-mini-completionzo1-mini-2024-09-12-completionz
o1-previewzo1-preview-cachedzo1-preview-2024-09-12zo1-preview-2024-09-12-cachedzo1-preview-completionz o1-preview-2024-09-12-completiongpt-4og{Gzd?zgpt-4o-cachedgpt-4o-2024-05-13g{Gzt?zgpt-4o-2024-08-06zgpt-4o-2024-08-06-cachedzgpt-4o-2024-11-20zgpt-4o-2024-11-20-cachedzgpt-4o-completionzgpt-4o-2024-05-13-completionzgpt-4o-2024-08-06-completionzgpt-4o-2024-11-20-completionzgpt-4o-audio-previewzgpt-4o-audio-preview-2024-12-17zgpt-4o-audio-preview-2024-10-01zgpt-4o-audio-preview-completionz*gpt-4o-audio-preview-2024-12-17-completionz*gpt-4o-audio-preview-2024-10-01-completionzgpt-4o-realtime-previewz"gpt-4o-realtime-preview-2024-12-17z"gpt-4o-realtime-preview-2024-10-01zgpt-4o-realtime-preview-cachedz)gpt-4o-realtime-preview-2024-12-17-cachedz)gpt-4o-realtime-preview-2024-10-01-cachedz"gpt-4o-realtime-preview-completiong{Gz?z-gpt-4o-realtime-preview-2024-12-17-completionz-gpt-4o-realtime-preview-2024-10-01-completionzgpt-4o-miniga2U0*#?zgpt-4o-mini-cachedga2U0*?zgpt-4o-mini-2024-07-18zgpt-4o-mini-2024-07-18-cachedzgpt-4o-mini-completionga2U0*C?z!gpt-4o-mini-2024-07-18-completionzgpt-4o-mini-audio-previewz$gpt-4o-mini-audio-preview-2024-12-17z$gpt-4o-mini-audio-preview-completionz/gpt-4o-mini-audio-preview-2024-12-17-completionzgpt-4o-mini-realtime-previewz'gpt-4o-mini-realtime-preview-2024-12-17z#gpt-4o-mini-realtime-preview-cachedga2U0*3?z.gpt-4o-mini-realtime-preview-2024-12-17-cachedz'gpt-4o-mini-realtime-preview-completionga2U0*c?z2gpt-4o-mini-realtime-preview-2024-12-17-completionzgpt-4o-mini-search-previewz%gpt-4o-mini-search-preview-2025-03-11z%gpt-4o-mini-search-preview-completionz0gpt-4o-mini-search-preview-2025-03-11-completionzgpt-4o-search-previewz gpt-4o-search-preview-2025-03-11z gpt-4o-search-preview-completionz+gpt-4o-search-preview-2025-03-11-completionzcomputer-use-previewg~jth?zcomputer-use-preview-2025-03-11zcomputer-use-preview-completiong~jt?z*computer-use-preview-2025-03-11-completiongpt-4gQ?z
gpt-4-0314z
gpt-4-0613z	gpt-4-32kzgpt-4-32k-0314zgpt-4-32k-0613zgpt-4-vision-previewzgpt-4-1106-previewzgpt-4-0125-previewzgpt-4-turbo-previewzgpt-4-turbozgpt-4-turbo-2024-04-09zgpt-4-completionzgpt-4-0314-completionzgpt-4-0613-completionzgpt-4-32k-completiongQ?zgpt-4-32k-0314-completionzgpt-4-32k-0613-completionzgpt-4-vision-preview-completionzgpt-4-1106-preview-completionzgpt-4-0125-preview-completionzgpt-4-turbo-preview-completionzgpt-4-turbo-completionz!gpt-4-turbo-2024-04-09-completionzgpt-3.5-turbog~jtX?zgpt-3.5-turbo-0125zgpt-3.5-turbo-0301zgpt-3.5-turbo-0613zgpt-3.5-turbo-1106gMbP?zgpt-3.5-turbo-instructzgpt-3.5-turbo-16kzgpt-3.5-turbo-16k-0613zgpt-3.5-turbo-completionzgpt-3.5-turbo-0125-completionzgpt-3.5-turbo-0301-completionzgpt-3.5-turbo-0613-completionzgpt-3.5-turbo-1106-completionz!gpt-3.5-turbo-instruct-completionzgpt-3.5-turbo-16k-completiongMbp?z!gpt-3.5-turbo-16k-0613-completionzgpt-35-turbozgpt-35-turbo-0125zgpt-35-turbo-0301zgpt-35-turbo-0613zgpt-35-turbo-instructzgpt-35-turbo-16kzgpt-35-turbo-16k-0613zgpt-35-turbo-completionzgpt-35-turbo-0125-completionzgpt-35-turbo-0301-completionzgpt-35-turbo-0613-completionz gpt-35-turbo-instruct-completionzgpt-35-turbo-16k-completionz gpt-35-turbo-16k-0613-completionztext-ada-001adaztext-babbage-001babbageztext-curie-001curieztext-davinci-003ztext-davinci-002zcode-davinci-002zbabbage-002-finetunedzdavinci-002-finetunedzgpt-3.5-turbo-0613-finetunedzgpt-3.5-turbo-1106-finetunedzgpt-3.5-turbo-0125-finetunedz gpt-4o-mini-2024-07-18-finetunedz'gpt-4o-mini-2024-07-18-finetuned-cachedz babbage-002-finetuned-completionz davinci-002-finetuned-completionz'gpt-3.5-turbo-0613-finetuned-completiong~jtx?z'gpt-3.5-turbo-1106-finetuned-completionz'gpt-3.5-turbo-0125-finetuned-completionz+gpt-4o-mini-2024-07-18-finetuned-completionga2U0*S?zbabbage-002-azure-finetunedzdavinci-002-azure-finetunedz!gpt-35-turbo-0613-azure-finetuned)z&babbage-002-azure-finetuned-completionz&davinci-002-azure-finetuned-completionz,gpt-35-turbo-0613-azure-finetuned-completionzada-finetuned-legacyzbabbage-finetuned-legacyzcurie-finetuned-legacyzdavinci-finetuned-legacyc                   :    e Zd ZdZ e       Z e       Z e       Zy)	TokenTypezToken type enum.N)__name__
__module____qualname____doc__r   PROMPTPROMPT_CACHED
COMPLETION     g/var/www/auto_recruiter/arenv/lib/python3.12/site-packages/langchain_community/callbacks/openai_info.pyr   r   4  s    VFFMJr!   r   
token_type
model_nameis_completionr$   returnc                T   |rt        ddd       t        j                  }| j                         } d| v r| j	                  d      d   dz   } d| v r| j	                  d	      d   d
z   } d| v r| j	                  d	      d   dz   } |t        j                  k(  r| j                  d      sn| j                  d      s]| j                  d      sL| j                  d      s;| j                  d      s*| j                  d      s| j                  d      sd| v r	d| vr| dz   S |t        j                  k(  r|| j                  d      sU| j                  d      sD| j                  d      s3| j                  d      s"| j                  d      s| j                  d      r| j                  d      s| dz   S | S )a  
    Standardize the model name to a format that can be used in the OpenAI API.

    Args:
        model_name: Model name to standardize.
        is_completion: Whether the model is used for completion or not.
            Defaults to False. Deprecated in favor of ``token_type``.
        token_type: Token type. Defaults to ``TokenType.PROMPT``.

    Returns:
        Standardized model name.

    0.3.13zis_completion is deprecated. Use token_type instead. Example:

from langchain_community.callbacks.openai_info import TokenType

standardize_model_name('gpt-4o', token_type=TokenType.COMPLETION)
1.0sincemessageremovalz.ft-r   z-azure-finetunedz:ft-:z-finetuned-legacyzft:   z
-finetunedr   r   zgpt-3.5zgpt-35zo1-zo3-zo4-	finetunedlegacyz-completionr   r   r   r   o4r   z-cached)r   r   r   lowersplit
startswithr   )r%   r&   r$   s      r"   standardize_model_namer7   <  s   & V 	
 ))
!!#J%%f-a03EE
%%c*1-0CC

%%c*1-<
Y)))g&  )  +  *  '  '  ':%(**DM))i---!!'*$$X.$$Y/$$T*$$T*$$T*&&':;I%%r!   
num_tokensc                    |rt        ddd       t        j                  }t        | |      } | t        vr3t        d|  ddj                  t        j                               z         t        |    |d	z  z  S )
a  
    Get the cost in USD for a given model and number of tokens.

    Args:
        model_name: Name of the model
        num_tokens: Number of tokens.
        is_completion: Whether the model is used for completion or not.
            Defaults to False. Deprecated in favor of ``token_type``.
        token_type: Token type. Defaults to ``TokenType.PROMPT``.

    Returns:
        Cost in USD.
    r)   zis_completion is deprecated. Use token_type instead. Example:

from langchain_community.callbacks.openai_info import TokenType

get_openai_token_cost_for_model('gpt-4o', 10, token_type=TokenType.COMPLETION)
r*   r+   r#   zUnknown model: z=. Please provide a valid OpenAI model name.Known models are: z, i  )r   r   r   r7   MODEL_COST_PER_1K_TOKENS
ValueErrorjoinkeys)r%   r8   r&   r$   s       r"   get_openai_token_cost_for_modelr>   }  s    ( c 	
 ))
'
zJJ11j\ *! !#'99-E-J-J-L#MN
 	
 $J/:3DEEr!   c                       e Zd ZU dZdZeed<   dZeed<   dZeed<   dZ	eed<   dZ
eed<   dZeed<   d	Zeed
<   d fdZdefdZedefd       Zdeeef   dee   deddfdZdededdfdZdededdfdZddZdedd fdZ xZS )OpenAICallbackHandlerz)Callback Handler that tracks OpenAI info.r   total_tokensprompt_tokensprompt_tokens_cachedcompletion_tokensreasoning_tokenssuccessful_requestsg        
total_costr'   Nc                 T    t         |           t        j                         | _        y )N)super__init__	threadingLock_lock)self	__class__s    r"   rJ   zOpenAICallbackHandler.__init__  s    ^^%
r!   c                     d| j                    d| j                   d| j                   d| j                   d| j                   d| j
                   d| j                   S )NzTokens Used: z
	Prompt Tokens: z
		Prompt Tokens Cached: z
	Completion Tokens: z
		Reasoning Tokens: z
Successful Requests: z
Total Cost (USD): $)rA   rB   rC   rD   rE   rF   rG   rN   s    r"   __repr__zOpenAICallbackHandler.__repr__  s    D--. /  $ 2 23 4))-)B)B(C D$$($:$:#; <%%)%:%:$; <$$($<$<#= >""&//!24	
r!   c                      y)z;Whether to call verbose callbacks even if verbose is False.Tr    rQ   s    r"   always_verbosez$OpenAICallbackHandler.always_verbose  s     r!   
serializedpromptskwargsc                      y)zPrint out the prompts.Nr    )rN   rU   rV   rW   s       r"   on_llm_startz"OpenAICallbackHandler.on_llm_start  s     	r!   tokenc                      y)zPrint out the token.Nr    )rN   rZ   rW   s      r"   on_llm_new_tokenz&OpenAICallbackHandler.on_llm_new_token  s    r!   responsec                    	 |j                   d   d   }t        |t              r;	 |j                  }t        |t
              r|j                  }|j                  }nd}d}nd}d}d}d}|rd|d   i}	|d   }
|d   }|xs i j                  d      x}rt        |      }n4|j                  d}n%t        |j                  j                  dd            }d|j                  d	i       v r|d	   d   }d
|j                  di       v r|d   d
   }n|j                  yd|j                  vr+| j                  5  | xj                  dz  c_        ddd       y|j                  d   }	|	j                  dd      }
|	j                  dd      }t        |j                  j                  dd            }|t        v r_||z
  }t        ||t         j"                        }t        ||t         j$                        }||z   }t        ||
t         j&                        }nd}d}| j                  5  | xj(                  ||z   z  c_        | xj*                  |	j                  dd      z  c_        | xj,                  |z  c_        | xj.                  |z  c_        | xj0                  |
z  c_        | xj2                  |z  c_        | xj                  dz  c_        ddd       y# t        $ r d}Y w xY w# t        $ r d}d}Y w xY w# 1 sw Y   yxY w# 1 sw Y   yxY w)zCollect token usage.r   NrA   output_tokensinput_tokensr%    
cache_readinput_token_details	reasoningoutput_token_detailstoken_usager0   rD   rB   r#   )generations
IndexError
isinstancer   r-   r
   usage_metadataresponse_metadataAttributeErrorgetr7   
llm_outputrM   rF   r:   r>   r   r   r   r   rG   rA   rB   rC   rD   rE   )rN   r]   rW   
generationr-   rj   rk   rC   rE   rf   rD   rB   response_model_namer%   uncached_prompt_tokensuncached_prompt_costcached_prompt_costprompt_costcompletion_costs                      r"   
on_llm_endz OpenAICallbackHandler.on_llm_end  sK   	!--a03J j.1
)$,,gy1%,%;%;N(/(A(A%%)N(,%
 "N $ )>.+IJK . ?*>:M'8'>B&C&CL&QQ"Q34GH
$$,
3''++L"=
 ~112GLL'56K'L ($ n001GLL#12H#I+#V ""*H$7$77ZZ 2,,1,2 #--m<K +0CQ G'OOOQ?M/##''b9J 11%25I%I"#B2y?O?O$  "A0Y=T=T" /1CCK=-):N:NO  OK ZZ 	*OO{_<<O!CC-/%%)==%""&77"!!%55!$$)$	* 	*S  	J	 " )!%$(!)B26	* 	*s;   K 9K! 1K5B'LKK!K21K25K>L
c                     | S )z&Return a copy of the callback handler.r    rQ   s    r"   __copy__zOpenAICallbackHandler.__copy__%      r!   memoc                     | S )z+Return a deep copy of the callback handler.r    )rN   rz   s     r"   __deepcopy__z"OpenAICallbackHandler.__deepcopy__)  ry   r!   )r'   N)r'   r@   )r   r   r   r   rA   int__annotations__rB   rC   rD   rE   rF   rG   floatrJ   strrR   propertyboolrT   r   r   r   rY   r\   r   rv   rx   r|   __classcell__)rO   s   @r"   r@   r@     s    3L#M3 !#!sc  J&	
# 	
   sCx.379HK	c S T U*9 U* U* U*n )@ r!   r@   )F)r   rK   enumr   r   typingr   r   r   langchain_core._apir   langchain_core.callbacksr	   langchain_core.messagesr
   langchain_core.outputsr   r   r:   r   r   r   r   r7   r}   r   r>   r@   r    r!   r"   <module>r      s   .   " " / 8 - <eWe He 	e
 xe e "4e 'e e We #He Ue  '!e$ '%e& 'e( W)e* #H+e. V/e0 '1e4 5e6 7e8 #G9e: *8;e> #D?e@ .tAeD uEeF %GeH fIeJ  KeN %OeP $UQeT FUeV vWeX 6YeZ %f[e^ v_e` )&aed Feef vgeh 8iej %hken voep )&qet uuev !%wex fyez ({e~ !$e@ ,TAeD 	%EeF UGeH IeJ FKeN TOeP QeT dUeV WeZ [e\ #C]e` 	%aeb Uced eef Fgej Ukel mep vqer &set huev  wez &{e| $V}e@ vAeB &CeD gEeF  GeJ &KeL $VMeP vQeR gSeT &UeV  WeZ &[e\ $V]e` %aeb ced Ueef #Fgej Tkel 'mep fqer Wset uev wex yez {e| }e@ AeB #ECeD #DEeF #DGeJ FKeL &vMeN &vOeR &tSeT 1$UeV 1$WeZ u[e\ )%]e^ )%_e` %faeb 0ced 0eeh )$iej 4Tkel 4Tmep 7qer (set guev $Xwez f{e| (}e@  AeB +GCeF +FGeH 6vIeL #FMeN .vOeP *6QeR 5fSeV .vWeX 9&Ye\ !']e^ ,W_eb ,Vced 7eeh Viej 'ken 'oep 24qet Euev &uwez &u{e| 1%}e@ TAeB $CeD $EeF GeH dIeJ dKeL DMeN $OeP $QeR 4SeT 4UeV dWeZ [e\ T]e^ T_e` Daeb  ced  eef &tgeh $Tiej $Tkel %dmen doep (qex Vyez &{e| &}e~ &e@ %AeB fCeD EeF eGeN OeP $VQeR $USeT $UUeV $UWeX (YeZ #E[e\ (]e` Faeb ced eef geh Viej kel Umep uqer #Fset #Euev #Ewex 'yez "5{e| '}e@ FAeB 
6CeD EeF vGeH eIeJ UKeL MeN OeP QeT VUeV UWeX #EYeZ #E[e\ #E]e^ '_e` .waed 'eef 'geh .uiej .ukel .umen 26oer "6set "5uev (wez /5.349" &# $I	e P	   > &,,	>>> 	>
 	>H  %F
 &,,%F%F%F %F
 %F %FPF/ Fr!   