
    i|                        S SK r S SKrS SKrS SKrS SKJrJr  S SKJr  S SK	J
r
JrJrJrJrJrJrJrJrJrJrJrJrJrJrJrJrJrJrJrJrJr  S SK J!r!J"r"  S SK#J$r$J%r%J&r&J'r'J(r(J)r)J*r*J+r+J,r,J-r-J.r.J/r/J0r0J1r1J2r2  S SK3J4s  J5r6  S SK7J8r8J9r9J:r:J;r;  S SK<J=r=J>r>J?r?J@r@JArAJBrBJCrCJDrDJErEJFrFJGrGJHrHJIrIJJrJ  S S	KKJLrLJMrM  S S
KNJOrO  S SKPJQrQJRrR  S SKSJTrT  S SKUJVrVJWrW  S SKXJYrY  S SKZJ[r[  S SK\J]r]  S SK^J_r_J`r`JaraJbrbJcrcJdrdJere  \6R                  " \g5      rh\$(       a  S SKiJjrj  SrkS\'S\%4   S\'S\%4   4S jrl\1 " S S\,5      5       rmS\>SS4S jrn " S S\T5      rog)    N)AsyncOpenAIAzureOpenAI)OpenAI)ResponseResponseStreamEventResponseCompletedEventResponseCreatedEvent$ResponseFileSearchCallCompletedEvent'ResponseFunctionCallArgumentsDeltaEvent&ResponseFunctionCallArgumentsDoneEventResponseInProgressEventResponseOutputItemAddedEvent&ResponseOutputTextAnnotationAddedEventResponseTextDeltaEvent#ResponseWebSearchCallCompletedEventResponseOutputItemResponseOutputMessageResponseFileSearchToolCallResponseFunctionToolCallResponseFunctionWebSearchResponseComputerToolCallResponseReasoningItemResponseCodeInterpreterToolCall%ResponseImageGenCallPartialImageEventResponseOutputItemDoneEvent)ImageGenerationCallMcpCall)TYPE_CHECKINGAnyAsyncGeneratorCallableDict	GeneratorListOptionalProtocolSequenceTupleTypeUnionruntime_checkablecast)achat_to_completion_decorator$astream_chat_to_completion_decoratorchat_to_completion_decorator#stream_chat_to_completion_decorator)ChatMessageChatResponseChatResponseAsyncGenChatResponseGenCompletionResponseCompletionResponseAsyncGenCompletionResponseGenLLMMetadataMessageRoleContentBlock	TextBlock
ImageBlockThinkingBlockToolCallBlock)FieldPrivateAttr)DEFAULT_TEMPERATURE)llm_chat_callbackllm_completion_callback)FunctionCallingLLM)ToolSelectionModel)parse_partial_json)PromptTemplate)FlexibleModel)	O1_MODELScreate_retry_decoratoris_function_calling_modelopenai_modelname_to_contextsizeresolve_openai_credentialsresolve_tool_choiceto_openai_message_dicts)BaseToolzgpt-4o-minif.returnc                 n   ^  [         R                  " T 5      S[        S[        S[        4U 4S jj5       nU$ )NargskwargsrS   c                    > [        U SS5      nUS::  a  T" U /UQ70 UD6$ [        USSSSS9nU" T5      " U /UQ70 UD6$ )Nmax_retriesr   T<         )rX   random_exponentialstop_after_delay_secondsmin_secondsmax_seconds)getattrrK   )selfrU   rV   rX   retryrR   s        [/home/james-whalen/.local/lib/python3.13/site-packages/llama_index/llms/openai/responses.pywrapper$llm_retry_decorator.<locals>.wrapperm   sb    dM15!T+D+F++&##%'
 Qx.t.v..    )	functoolswrapsr   )rR   rd   s   ` rc   llm_retry_decoratorri   l   s:    __Q/S /C /C / / Nrf   c                   0    \ rS rSrSrS\S\\   4S jrSr	g)	Tokenizer   zBTokenizers support an encode function that returns a list of ints.textrS   c                     g N )ra   rm   s     rc   encodeTokenizer.encode   s    rf   rp   N)
__name__
__module____qualname____firstlineno____doc__strr$   intrq   __static_attributes__rp   rf   rc   rk   rk      s    L3 49 rf   rk   responsec                 Z   U R                   R                   Vs/ s H  n[        U[        5      (       d  M  UPM     nn[	        U5      S:  aU  U R                   R                   Vs/ s H  n[        U[        5      (       a  M  UPM     snUS   /-   U R                   l        g g s  snf s  snf )NrZ   r   )messageblocks
isinstancer>   len)r{   block
tool_callss      rc   force_single_tool_callr      s    #++222%j6V2   : "))00#
0e]3 0#
 ]O	# #
s   B#B#%B(B(c            9       
  ^  \ rS rSr% Sr\" \SS9r\\	S'   \" \
SSSS	9r\\	S
'   \" SSSSS	9r\\	S'   \" SSS9r\\   \	S'   \" SSS9r\\\\4      \	S'   \" SSS9r\\\      \	S'   \" SSS9r\\   \	S'   \" SSS9r\\	S'   \" SSS9r\\	S'   \" SSS9r\\\      \	S'   \" S S!S9r\\	S"'   \" SS#S9r\\   \	S$'   \" SS%S9r\\\\4      \	S&'   \" \S'S(9r\\\4   \	S)'   \" S*S+SS,9r \\	S-'   \" S.S/SS,9r!\\	S0'   \" SS1S9r"\\	S2'   \" SS3S9r#\\\\4      \	S4'   \" SS5S9r$\\   \	S6'   \" S7S89r%\\	S9'   \" S:S89r&\\	S;'   \" SS<S9r'\\   \	S='   \(" 5       r)\*\	S>'   \(" 5       r+\,\	S?'   \(" 5       r-\\.R^                     \	S@'   \(" 5       r0\\.Rb                     \	SA'   \(" 5       r2\\   \	SB'   \\
SSSSSSSS SSSSSS*S.SSSSSSSSS4S\S
\S\\   S\\\\4      S\\\      S\\   S\S\S\\\      S"\S$\\   SC\\   S&\\\\4      S2\S)\\\\4      S-\S0\S6\\   S9\\   S;\\   S4\\\\4      SD\\.R^                     SE\\.Rb                     SF\\*   SG\\,   S=\\   SH\SIS48U 4SJ jjjr3\4SI\4SK j5       r5\6SI\74SL j5       r8\6SI\\9   4SM j5       r:SI\4SN jr;SI\4SO jr<S}SP\SI\\\4   4SQ jjr=SH\SI\\\4   4SR jr>\?" 5       SS\@\A   SH\SI\B4ST j5       rC\?" 5       SS\@\A   SH\SI\D4SU j5       rE\F" 5        S}SV\SW\SH\SI\G4SX jj5       rH\F" 5        S}SV\SW\SH\SI\I4SY jj5       rJ\KSZ\\L   SI\B4S[ j5       rM\NSS\@\A   SH\SI\B4S\ j5       rO\K S~S]\PS^\\   S)\\\4   S_\\Q   S\SC\\   SI\R\\S   \\   \\\4   \\Q   \\   \4   4S` jj5       rT\NSS\@\A   SH\SI\D4Sa j5       rU\?" 5       SS\@\A   SH\SI\B4Sb j5       rV\?" 5       SS\@\A   SH\SI\W4Sc j5       rX\F" 5        S}SV\SW\SH\SI\G4Sd jj5       rY\F" 5        S}SV\SW\SH\SI\Z4Se jj5       r[\NSS\@\A   SH\SI\B4Sf j5       r\\NSS\@\A   SH\SI\W4Sg j5       r]       SSh\@Si   Sj\\^\\A4      Sk\\\A      Sl\Sm\Sn\\^\\4      So\S2\\   SH\SI\\\4   4Sp jjr_ SSqSrSs\SH\SI\\`   4St jjra\bR                   S~Su\d\e   SV\fSv\\\\4      Sw\SI\e4
U 4Sx jjj5       rg\bR                   S~Su\d\e   SV\fSv\\\\4      Sw\SI\e4
U 4Sy jjj5       rh\bR                   S~Su\d\e   SV\fSv\\\\4      Sw\SI\i\^\e\j4   SS4   4
U 4Sz jjj5       rk\bR                   S~Su\d\e   SV\fSv\\\\4      Sw\SI\l\^\e\j4   S4   4
U 4S{ jjj5       rmS|rnU =ro$ )OpenAIResponses   a  
OpenAI Responses LLM.

Args:
    model: name of the OpenAI model to use.
    temperature: a float from 0 to 1 controlling randomness in generation; higher will lead to more creative, less deterministic responses.
    max_output_tokens: the maximum number of tokens to generate.
    reasoning_options: Optional dictionary to configure reasoning for O1 models.
                Corresponds to the 'reasoning' parameter in the OpenAI API.
                Example: {"effort": "low", "summary": "concise"}
    include: Additional output data to include in the model response.
    instructions: Instructions for the model to follow.
    track_previous_responses: Whether to track previous responses. If true, the LLM class will statefully track previous responses.
    store: Whether to store previous responses in OpenAI's storage.
    built_in_tools: The built-in tools to use for the model to augment responses.
    truncation: Whether to auto-truncate the input if it exceeds the model's context window.
    user: An optional identifier to help track the user's requests for abuse.
    strict: Whether to enforce strict validation of the structured output.
    additional_kwargs: Add additional parameters to OpenAI request body.
    max_retries: How many times to retry the API call if it fails.
    timeout: How long to wait, in seconds, for an API call before failing.
    api_key: Your OpenAI api key
    api_base: The base URL of the API to call
    api_version: the version of the API to call
    default_headers: override the default headers for API requests.
    http_client: pass in your own httpx.Client instance.
    async_http_client: pass in your own httpx.AsyncClient instance.

Examples:
    `pip install llama-index-llms-openai`

    ```python
    from llama_index.llms.openai import OpenAIResponses

    llm = OpenAIResponses(model="gpt-4o-mini", api_key="sk-...")

    response = llm.complete("Hi, write a short story")
    print(response.text)
    ```
zThe OpenAI model to use.)defaultdescriptionmodelz)The temperature to use during generation.g        g       @)r   r   geletemperature      ?z)The top-p value to use during generation.top_pz)The maximum number of tokens to generate.r   )r   gtmax_output_tokensNzjOptional dictionary to configure reasoning for O1 models. Example: {'effort': 'low', 'summary': 'concise'}reasoning_optionsz8Additional output data to include in the model response.includez%Instructions for the model to follow.instructionsFzeWhether to track previous responses. If true, the LLM class will statefully track previous responses.track_previous_responsesz8Whether to store previous responses in OpenAI's storage.storez=The built-in tools to use for the model to augment responses.built_in_toolsdisabledzLWhether to auto-truncate the input if it exceeds the model's context window.
truncationzCAn optional identifier to help track the user's requests for abuse.userz$Metadata to include in the API call.call_metadataz7Additional kwargs for the OpenAI API at inference time.)default_factoryr   additional_kwargs   z"The maximum number of API retries.)r   r   r   rX   g      N@z*The timeout, in seconds, for API requests.timeoutz>Whether to enforce strict validation of the structured output.strictz%The default headers for API requests.default_headerszThe OpenAI API key.api_keyzThe base URL for OpenAI API.)r   api_basezThe API version for OpenAI API.api_versionz*The context window override for the model.context_window_client_aclient_http_client_async_http_client_previous_response_idprevious_response_idhttp_clientasync_http_clientopenai_clientasync_openai_clientrV   rS   c                   > U=(       d    0 n[        UUUS9u  nnnU[        ;   a  Sn[        TU ]  " S0 SU_SU_SU_SU_SU_SU_S	U_S
U_SU	_SU
_SU_SU_SU_SU_SU_SU_SU_SU_SU_SU_SU_UD6  Xl        U R
                  (       a  SU l        UU l        UU l        U=(       d    [        S0 U R                  5       D6U l        U=(       d    [        S0 U R                  SS9D6U l        g )N)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rX   r   r   r   r   r   r   r   r   T)is_asyncrp   )rN   rJ   super__init__r   r   r   r   r   
SyncOpenAI_get_credential_kwargsr   r   r   )ra   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rX   r   r   r   r   r   r   r   r   r   r   rV   	__class__s                               rc   r   OpenAIResponses.__init__  s   < .3)C#*
&; IK 	
	
#	
 0	
 0		

 	
 &	
 &>	
 	
 *	
 "	
 	
 0	
 $	
 	
 $	
  !	
" #	
$ ,%	
& ('	
( )	
* *-	
2 &:" ((DJ'"3$S
(ST5P5P5R(S+ 
{ 0
))4)80
rf   c                     g)Nopenai_responses_llmrp   )clss    rc   
class_nameOpenAIResponses.class_nameg  s    %rf   c           	          [        U R                  =(       d    [        U R                  5       5      U R                  =(       d    SS[        U R                  5       S9U R                  S9$ )NT)r   )r   
num_outputis_chat_modelrL   
model_name)r8   r   rM   _get_model_namer   rL   r   ra   s    rc   metadataOpenAIResponses.metadatak  s]    .. G.t/C/C/EF--3&?**,' zz	
 		
rf   c                 J    [         R                  " U R                  5       5      $ )z
Get a tokenizer for this model, or None if a tokenizing method is unknown.

OpenAI can do this using the tiktoken package, subclasses may not have
this convenience.
)tiktokenencoding_for_modelr   r   s    rc   
_tokenizerOpenAIResponses._tokenizerx  s     **4+?+?+ABBrf   c                     U R                   nSU;   a  UR                  S5      S   nU$ UR                  S5      (       a  UR                  S5      S   nU$ )Nzft-:r   zft:rZ   )r   split
startswith)ra   r   s     rc   r   OpenAIResponses._get_model_name  s^    ZZ
J#))#.q1J  ""5))#))#.q1Jrf   c                 6    [        U R                  [        5      $ ro   )r   r   r   r   s    rc   _is_azure_client OpenAIResponses._is_azure_client  s    $,,44rf   r   c                     U R                   U R                  U R                  U R                  U R                  U(       a  U R
                  S.$ U R                  S.$ )N)r   base_urlrX   r   r   r   )r   r   rX   r   r   r   r   )ra   r   s     rc   r   &OpenAIResponses._get_credential_kwargs  sU    ||++||#336>422
 	
 EIDUDU
 	
rf   c                     U R                   =(       d    / nU R                  U R                  U R                  U R                  U R
                  U R                  U R                  U R                  / UQUR                  S/ 5      QU R                  U R                  U R                  S.nU R                  [        ;   a  U R                  b  U R                  US'   UR                  U R                   5        U=(       d    0 nUR                  U5        U$ )Ntools)r   r   r   r   r   r   r   r   r   r   r   r   	reasoning)r   r   r   r   r   r   r   r   r   popr   r   r   rJ   r   updater   )ra   rV   initial_toolsmodel_kwargss       rc   _get_model_kwargs!OpenAIResponses._get_model_kwargs  s    ++1rZZ|| --!%!7!7**$($>$>ZZ++?}?vzz'2'>?ZZ//II
 ::"t'='='I(,(>(>L% 	D2232F#rf   messagesc                 (    U R                   " U40 UD6$ ro   )_chatra   r   rV   s      rc   chatOpenAIResponses.chat  s    zz(-f--rf   c                 (    U R                   " U40 UD6$ ro   )_stream_chatr   s      rc   stream_chatOpenAIResponses.stream_chat  s       4V44rf   prompt	formattedc                 >    [        U R                  5      nU" U40 UD6$ ro   )r/   r   )ra   r   r   rV   complete_fns        rc   completeOpenAIResponses.complete  s"     34::>6,V,,rf   c                 >    [        U R                  5      nU" U40 UD6$ ro   )r0   r   )ra   r   r   rV   stream_complete_fns        rc   stream_completeOpenAIResponses.stream_complete  s&     AARARS!&3F33rf   outputc           
         [        [        R                  / S9nS/ 0n/ nU  GH  n[        U[        5      (       a  UR
                   Hx  n[        US5      (       a"  UR                  [        UR                  S95        [        US5      (       a  UR                  US'   [        US5      (       d  Mi  UR                  US'   Mz     UR                  R                  U5        M  [        U[        5      (       ar  UR                  S:w  a_  US   R                  U5        UR                   b;  ["        R$                  " UR                   5      nUR                  ['        US95        GM?  GMB  GME  [        U[(        5      (       a  US   R                  U5        GMq  [        U[*        5      (       a  US   R                  U5        GM  [        U[,        5      (       a  US   R                  U5        GM  [        U[.        5      (       aE  UR                  R                  [1        UR2                  UR4                  UR6                  S	95        GM#  [        U[8        5      (       a  US   R                  U5        GMO  [        U[:        5      (       a  US   R                  U5        GM{  [        U[<        5      (       d  GM  S nUR
                  (       a5  S
R?                  UR
                   Vs/ s H  oR                  PM     sn5      nUR@                  (       ax  U(       a<  US
S
R?                  UR@                   Vs/ s H  oR                  PM     sn5      -   -  nO5S
R?                  UR@                   Vs/ s H  oR                  PM     sn5      nUR                  R                  [C        UURE                  SS1S9S95        GM     [G        XS9$ s  snf s  snf s  snf )Nroler~   built_in_tool_callsrm   rm   annotationsrefusalfailed)image)	tool_nametool_call_idtool_kwargs
contentsummaryexcluder   additional_information)r}   r   )$r1   r9   	ASSISTANTr   r   r   hasattrappendr;   rm   r   r   r~   extendr   statusresultbase64	b64decoder<   r   r   r   r   r>   namecall_id	argumentsr   r   r   joinr  r=   
model_dumpr2   )	r   r}   r   r~   itempartimage_bytesr   is	            rc   _parse_response_output&OpenAIResponses._parse_response_output  s   ;#8#8D2B7%'D$ 566 LLDtV,,iTYY&?@t]33;?;K;K)-8tY//7;||))4 ) %%f-D"566;;(*%&;<CCDI{{.&,&6&6t{{&Cj{&CD / +
 D"ABB!"78??ED'**!"78??ED"<==!"78??ED":;;%%!"&))%)\\$(NN D";<<!"78??ED":;;!"78??ED"788)-<<"ii(FA(FGG<<4$))T\\4R\VV\4R*S#SS"&))T\\,J\VV\,J"K%%! '/3%.	$: 0? 0[ l GQQ )G 5S,Js    O
/O
)O
c                 b   U R                   " S0 UD6n[        UU R                  SS9nU R                  R                  R
                  " SUSS.UD6nU R                  (       a  UR                  U l        [        R                  UR                  5      nXVl        UR                  UR                  S'   [        UR                  R                   S5      (       aY  UR"                  R$                   H?  n['        U[(        5      (       d  M  UR                  R                   R*                  Ul        MA     U$ )NTr   is_responses_apiFinputstreamusagereasoning_tokensrp   )r   rP   r   r   	responsescreater   idr   r   r  r   rawr  r   r  output_tokens_detailsr}   r~   r   r=   r   
num_tokens)ra   r   rV   kwargs_dictmessage_dictsr{   chat_responser   s           rc   r   OpenAIResponses._chat
  s    ,,6v6/**!
 "\\33:: 

 
 (()1D&'>>xO$3;>>''08>>779KLL&..55e]33 <<MM $ 6 rf   eventr   current_tool_callc           	      $	   SnUn/ n[        U [        5      (       d  [        U [        5      (       a  U(       a  U R                  R                  nGO+[        U [
        5      (       a-  [        U R                  [        5      (       a  U R                  nGO[        U [        5      (       a&  U R                  nUR                  [        US95        GO[        U [        5      (       aW  U R                  (       aD  UR                  [        [        R                   " U R                  5      SU R"                   3S95        GOB[        U [$        5      (       a$  Ub  U=R&                  U R                  -  sl        GO	[        U [(        5      (       aW  UbR  U R&                  Ul        SUl        UR                  [-        UR.                  UR&                  UR0                  S95        SnGO[        U [2        5      (       a3  UR5                  S/ 5      n	U	R                  U R6                  5        XS'   GOU[        U [8        5      (       a  UR                  U 5        GO-[        U [:        5      (       a  UR                  U 5        GO[        U [<        5      (       GaV  [        U R                  [>        5      (       Ga5  Sn
U R                  R@                  (       a?  S	RC                  U R                  R@                   Vs/ s H  oRD                  PM     sn5      n
U R                  RF                  (       a  U
(       aF  U
S	S	RC                  U R                  RF                   Vs/ s H  oRD                  PM     sn5      -   -  n
O?S	RC                  U R                  RF                   Vs/ s H  oRD                  PM     sn5      n
UR                  [I        U
U R                  RK                  S
S1S9S95        O[        U [L        5      (       a  [O        U S5      (       a4  [O        U R                  S5      (       a  U R                  RP                  US'   [R        RU                  U R                  RV                  5      nURX                  RZ                  nUUUUUU4$ s  snf s  snf s  snf )a  
Process a ResponseStreamEvent and update the state accordingly.

Args:
    event: The response stream event to process
    content: Current accumulated content string
    tool_calls: List of completed tool calls
    built_in_tool_calls: List of built-in tool calls
    additional_kwargs: Additional keyword arguments to include in ChatResponse
    current_tool_call: The currently in-progress tool call, if any
    track_previous_responses: Whether to track previous response IDs
    previous_response_id: Previous response ID if tracking

Returns:
    A tuple containing the updated state:
    (content, tool_calls, built_in_tool_calls, additional_kwargs, current_tool_call, updated_previous_response_id, delta)
 r   id_)r   detailN	completed)r   r   r   r   r   r   r  r  r  r{   r  ).r   r	   r   r{   r#  r   r  r   r   deltar  r;   r   partial_image_b64r<   r  r  partial_image_indexr   r  r   r
  r>   r  r  r   get
annotationr
   r   r   r   r   r  rm   r  r=   r  r   r  r  r   r  r   r}   r~   )r+  r   r   r,  r   r   r2  updated_previous_response_idr~   r   r   r  resps                rc   process_response_event&OpenAIResponses.process_response_event(  s   B ';$%'e122j*7
 7
 (/4~~/@/@,;<<%**&>??$)JJ!566KKEMM)/0DEE&&$..u/F/FG!$U%>%>$?@ FGG ,!++u{{:+EFF ,.3oo!++6!(!"3"8"8$5$?$?%6%>%> %)!EFF+//rBKu//0/:m,CDD&&u-BCC&&u-:;;%**&;<<)-::%%"ii9K9K(L9KA9K(LMG::%%4$))-2ZZ-?-?@-?VV-?@+ $  #'))UZZ=O=O,P=OVV=O,P"Q! '/4zz/D/D%.	$: 0E 0 566uj))genng.N.N-2^^-A-A!'*"99%..:O:OPD\\((F (
 	
/ )M A -Qs   ,RRRc                 `   ^ ^^ [        UT R                  SS9mS[        4UUU 4S jjnU" 5       $ )NTr  rS   c            
   3     >#    / n S/ 0nS nT	R                   nT	R                  R                  R                  " STSS.T	R                  " S0 TD6D6 H  n[
        R                  UU UUT	R                  US9u  nn nnnnT	R                  (       a  UT	R                   :w  a  UT	l         U (       a  XS'   [        [        [        R                  US9UUUS9v   M     g 7fNr   Tr  )r+  r   r   r,  r   r   r   )r}   r2  r$  r   rp   )r   r   r!  r"  r   r   r9  r   r2   r1   r9   r  )
r   r   r,  local_previous_response_idr+  r~   r2  rV   r(  ra   s
          rc   gen)OpenAIResponses._stream_chat.<locals>.gen  s    "$!6 ;DH)-)C)C&//66 # ((262 $::(;&7&7-1-J-J)C ; '%%. 112d6P6PP1KD.&?R&;< #'(22%  &7 As   CC)rP   r   r4   ra   r   rV   r?  r(  s   ` ` @rc   r   OpenAIResponses._stream_chat  s7     0**!
.	_ .	 .	` urf   c                 D   #    U R                   " U40 UD6I S h  vN $  N7fro   )_achatr   s      rc   achatOpenAIResponses.achat  s!      [[4V4444     c                 D   #    U R                   " U40 UD6I S h  vN $  N7fro   )_astream_chatr   s      rc   astream_chatOpenAIResponses.astream_chat  s#      '';F;;;;rG  c                 Z   #    [        U R                  5      nU" U40 UD6I S h  vN $  N7fro   )r-   rD  )ra   r   r   rV   acomplete_fns        rc   	acompleteOpenAIResponses.acomplete  s+      5T[[A!&3F3333   "+)+c                 Z   #    [        U R                  5      nU" U40 UD6I S h  vN $  N7fro   )r.   rI  )ra   r   r   rV   astream_complete_fns        rc   astream_complete OpenAIResponses.astream_complete  s/      C4CUCUV(:6::::rP  c           	      ~  #    [        UU R                  SS9nU R                  R                  R                  " SUSS.U R
                  " S0 UD6D6I S h  vN nU R                  (       a  UR                  U l        [        R                  UR                  5      nXEl        UR                  UR                  S'   U$  Nf7f)NTr  Fr  r  rp   )rP   r   r   r!  r"  r   r   r#  r   r   r  r   r$  r  r   )ra   r   rV   r(  r{   r)  s         rc   rD  OpenAIResponses._achat  s      0**!
 $(==#:#:#A#A $
$
 $$.v.$
 
 (()1D&'>>xO$3;>>''0
s   AB=B;A'B=c                 h   ^ ^^#    [        UT R                  SS9mS[        4UUU 4S jjnU" 5       $ 7f)NTr  rS   c            
       >#    / n S/ 0nS nT
R                   nT
R                  R                  R                  " ST	SS.T
R                  " S0 TD6D6I S h  vN nU  S h  vN n[
        R                  UU UUT
R                  US9u  nn nnnnT
R                  (       a  UT
R                   :w  a  UT
l         U (       a  XS'   [        [        [        R                  US9UUUS97v   M   N N
 g 7fr=  )r   r   r!  r"  r   r   r9  r   r2   r1   r9   r  )r   r   r,  r>  response_streamr+  r~   r2  rV   r(  ra   s           rc   r?  *OpenAIResponses._astream_chat.<locals>.gen'  s#    "$!6 ;DH)-)C)C&$(MM$;$;$B$B %#% ((262% O  / $e $::(;&7&7-1-J-J)C ; '%%. 112d6P6PP1KD.&?R&;< #'(22%  &7 E$s7   AC-C'C-C+C) C+#BC-)C++C-)rP   r   r3   rA  s   ` ` @rc   rI  OpenAIResponses._astream_chat  s;      0**!
0	/ 0	 0	d us   -2r   rQ   user_msgchat_historyallow_parallel_tool_callstool_requiredtool_choiceverbosec	                    U V
s/ s H#  n
SS0U
R                   R                  SS9S   EPM%     nn
Ub  UnOU R                  nU(       a  U H  nSUS'   SUS   S	'   M     [        U[        5      (       a  [        [        R                  US
9nU=(       d    / nU(       a  UR                  U5        UU=(       d    SU(       a  [        Xe5      OSUS.U	E$ s  sn
f )Predict and call the tool.typefunctionT)skip_length_checkNr   F
parametersadditionalProperties)r   r   )r   r   r`  parallel_tool_calls)
r   to_openai_toolr   r   rx   r1   r9   USERr  rO   )ra   r   r\  r]  r^  r_  r`  ra  r   rV   tool
tool_specs	tool_specr   s                 rc   _prepare_chat_with_tools(OpenAIResponses._prepare_chat_with_tools[  s    ( 

  
--...FzR  	 
 F[[F'	&*	(#BG	,'(>? ( h$$"(8(8(KH%2OOH% !'4 /{J#<
 
 	
3
s   *Cr{   r2   error_on_no_tool_callc           	         UR                   R                   Vs/ s H  n[        U[        5      (       d  M  UPM     nn[	        U5      S:  a!  U(       a  [        S[	        U5       S35      e/ $ / nU H^  n [        [        [        UR                  5      5      nUR                  [        UR                  =(       d    SUR                  US95        M`     U$ s  snf ! [         a    0 n NPf = f)rc  rZ   z)Expected at least one tool call, but got z tool calls.r.  )tool_idr   r   )r}   r~   r   r>   r   
ValueErrorrG   r,   rx   r   	Exceptionr  rE   r   r   )	ra   r{   rq  rV   r   r   tool_selections	tool_callargument_dicts	            rc   get_tool_calls_from_response,OpenAIResponses.get_tool_calls_from_response  s     "))00+
0%/ 0 	 +
 z?Q$ ?J?PP\]  	#I# 24Y=R=R3S T ""%228b'11 - $ =+
&  # "#s   CC6#CC*)C*
output_cls
llm_kwargsprompt_argsc                 `   > U=(       d    0 nSU;  a  SOUS   US'   [         TU ]  " X4SU0UD6$ )Structured predict.r`  requiredr|  )r   structured_predictra   r{  r   r|  r}  r   s        rc   r  "OpenAIResponses.structured_predict  sS      %2
 (z9Jz-?X 	=!
 w)
+5
9D
 	
rf   c                 |   >#    U=(       d    0 nSU;  a  SOUS   US'   [         TU ]  " X4SU0UD6I Sh  vN $  N7f)r  r`  r  r|  N)r   astructured_predictr  s        rc   r  #OpenAIResponses.astructured_predict  s`       %2
 (z9Jz-?X 	=!
 W0
+5
9D
 
 	
 
   2<:<c                 `   > U=(       d    0 nSU;  a  SOUS   US'   [         TU ]  " X4SU0UD6$ )Stream structured predict.r`  r  r|  )r   stream_structured_predictr  s        rc   r  )OpenAIResponses.stream_structured_predict  sS      %2
 (z9Jz-?X 	=!
 w0
+5
9D
 	
rf   c                 |   >#    U=(       d    0 nSU;  a  SOUS   US'   [         TU ]  " X4SU0UD6I Sh  vN $  N7f)r  r`  r  r|  N)r   astream_structured_predictr  s        rc   r  *OpenAIResponses.astream_structured_predict  s`       %2
 (z9Jz-?X 	=!
 W7
+5
9D
 
 	
 
r  )r   r   r   r   r   r   )Fro   )NNTFNFN)T)prs   rt   ru   rv   rw   r?   DEFAULT_OPENAI_MODELr   rx   __annotations__rA   r   floatr   r   r%   ry   r   r"   r   r   r$   r   r   boolr   r   dictr   r   r   r   rX   r   r   r   r   r   r   r   r@   r   r   r   r   r   httpxClientr   AsyncClientr   r   classmethodr   propertyr8   r   rk   r   r   r   r   r   rB   r'   r1   r2   r   r4   r   rC   r5   r   r7   r   staticmethodr   r  ri   r   r   r   r(   r:   r9  r   rE  r3   rJ  rN  r6   rS  rD  rI  r*   ro  rE   ry  
dispatcherspanr)   rF   rH   r  r  r#   rI   r  r    r  rz   __classcell__)r   s   @rc   r   r      s   'R $2LE3  #?	K  ?	E5  (-?(x}  38 A3xS#X/  $)N$GXd3i   #(;#L(3-  &+{&d  NE4  ,1S,NHT$Z(  bJ   YD(3-  /4:/M8DcN+  ).M)tCH~  8K 
 @GU 
 TFD  16"I1OXd38n-  #4=RSGXc]S&DEHcE)JKKK$)@%NHSM 
 &-GZ''MHk)+6=L(5<<(86Am!2!23C+6=8C=8 *0+/6:'+&*)./3$".2266:!%"&%)48.29=.259(,7N
N
 N
 $C=	N

 $DcN3N
 $s)$N
 smN
 #'N
 N
 !d,N
 N
 smN
 'smN
  S#X/N
 N
  $DcN3!N
" #N
$ %N
& #'N
( 3-)N
* c]+N
, "$sCx.1-N
. ell+/N
0 $E$5$561N
2  
+3N
4 &k25N
6 !7N
8 9N
: 
;N
 N
` &3 & & 

+ 

 

 CHY/ C C 5$ 5
t 
S#X 
# $sCx. 8 .Xk2 .c .l . . 5 -59<5	5 5
 -2--&*->A-	- - -244&*4>A4	4 4 :Rt,>'? :RL :R :Rx h{3 s |  :  /3|
"|
!#Y|
  S>|
 $$<=	|

 #'|
 'sm|
 
\S	S#X)*	
|
 |
| 9 -99<9	9 9x 5;'5 5 
	5 5 <;'< < 
	< < -244&*4>A4	4 4 -2;;&*;>A;	#; ;  -9<	 0 ; -;9<;	; ;@ 7;48*.#26!%0

#0
 5k!1230
 tK01	0

 $(0
 0
 eCI./0
 0
 0
 0
 
c3h0
j '+% %  $% 	%
 
m	%N __
 04	
K
 
 T#s(^,	

 
 

 
& __
 04	
K
 
 T#s(^,	

 
 

 
& __
 04	
K
 
 T#s(^,	

 
 
5-.d:	;
 
& __
 04	
K
 
 T#s(^,	

 
 
e]23T9	:
 
rf   r   )prg   r  r   r  openair   r   r   r   openai.types.responsesr   r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   +openai.types.responses.response_output_itemr   r   typingr   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,    llama_index.core.instrumentationcoreinstrumentation
instrument(llama_index.core.base.llms.generic_utilsr-   r.   r/   r0    llama_index.core.base.llms.typesr1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>    llama_index.core.bridge.pydanticr?   r@   llama_index.core.constantsrA   llama_index.core.llms.callbacksrB   rC   &llama_index.core.llms.function_callingrD   llama_index.core.llms.llmrE   rF   llama_index.core.llms.utilsrG   llama_index.core.promptsrH   llama_index.core.program.utilsrI   llama_index.llms.openai.utilsrJ   rK   rL   rM   rN   rO   rP   get_dispatcherrs   r  llama_index.core.tools.typesrQ   r  ri   rk   r   r   rp   rf   rc   <module>r     s*       + '     0 U    $ 6 5      F : : 3 8   &&x0
5$ 8CH- (382D &   	\ 	d 	o
( o
rf   