o
    ͩZht                     @  s   d dl mZ d dlZd dlmZ d dlmZmZmZm	Z	m
Z
mZmZmZ d dl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mZmZmZmZmZ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+m,Z,m-Z- ddl.m/Z/ ddl0m1Z1m2Z2 ddl3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z: ddl;m<Z<m=Z= ddl>m?Z?m@Z@mAZA ddlBmCZCmDZD ddlEmFZF ddlGmHZI ddlJmKZL G dd dee4 ZMG dd dee4 ZNG dd dee4 ZOG dd  d ee4 ZPG d!d" d"ee4 ZQG d#d$ d$ZRd+d)d*ZSdS ),    )annotationsN)TracebackType)TYPE_CHECKINGAnyGenericCallableIterable	AwaitableAsyncIteratorcast)SelfIteratorassert_never)	from_json   )ParsedChoiceSnapshotParsedChatCompletionSnapshot#ParsedChatCompletionMessageSnapshot)
ChunkEventContentDoneEventRefusalDoneEventContentDeltaEventRefusalDeltaEventLogprobsContentDoneEventLogprobsRefusalDoneEventChatCompletionStreamEventLogprobsContentDeltaEventLogprobsRefusalDeltaEvent"FunctionToolCallArgumentsDoneEvent#FunctionToolCallArgumentsDeltaEvent   )accumulate_delta   )	NOT_GIVENIncExNotGiven)is_givenconsume_sync_iteratorconsume_async_iterator)
model_dump)buildconstruct_type   )ResponseFormatThas_parseable_inputmaybe_parse_contentparse_chat_completionget_input_tool_by_namesolve_response_format_tparse_function_tool_arguments)StreamAsyncStream)ChatCompletionChunkParsedChatCompletionChatCompletionToolParam)LengthFinishReasonErrorContentFilterFinishReasonError)ChoiceLogprobs)Choice)ResponseFormatc                   @  x   e Zd ZdZd*d
dZd+ddZd,ddZd-ddZd.ddZd/ddZ	d0d d!Z
d-d"d#Zed1d%d&Zd,d'd(Zd)S )2ChatCompletionStream+  Wrapper over the Chat Completions streaming API that adds helpful
    events such as `content.done`, supports automatically parsing
    responses & tool calls and accumulates a `ChatCompletion` object
    from each individual chunk.

    https://platform.openai.com/docs/api-reference/streaming
    
raw_streamStream[ChatCompletionChunk]response_format6type[ResponseFormatT] | ResponseFormatParam | NotGiveninput_tools,Iterable[ChatCompletionToolParam] | NotGivenreturnNonec                C  *   || _ |j| _|  | _t||d| _d S NrC   rE   _raw_streamresponse	_response
__stream__	_iteratorChatCompletionStreamState_stateselfrA   rC   rE    rV   ]/var/www/html/lang_env/lib/python3.10/site-packages/openai/lib/streaming/chat/_completions.py__init__8      
zChatCompletionStream.__init__*ChatCompletionStreamEvent[ResponseFormatT]c                 C  s
   | j  S N)rQ   __next__rU   rV   rV   rW   r\   D   s   
zChatCompletionStream.__next__4Iterator[ChatCompletionStreamEvent[ResponseFormatT]]c                 c  s    | j D ]}|V  qd S r[   rQ   rU   itemrV   rV   rW   __iter__G   s   
zChatCompletionStream.__iter__r   c                 C  s   | S r[   rV   r]   rV   rV   rW   	__enter__K   s   zChatCompletionStream.__enter__exc_typetype[BaseException] | NoneexcBaseException | Noneexc_tbTracebackType | Nonec                 C  s   |    d S r[   closerU   rd   rf   rh   rV   rV   rW   __exit__N   s   zChatCompletionStream.__exit__c                 C  s   | j   dS z
        Close the response and release the connection.

        Automatically called if the response body is read to completion.
        N)rO   rk   r]   rV   rV   rW   rk   V   s   zChatCompletionStream.close%ParsedChatCompletion[ResponseFormatT]c                 C  s   |    | j S )V  Waits until the stream has been read to completion and returns
        the accumulated `ParsedChatCompletion` object.

        If you passed a class type to `.stream()`, the `completion.choices[0].message.parsed`
        property will be the content deserialised into that class, if there was any content returned
        by the API.
        
until_donerS   get_final_completionr]   rV   rV   rW   rs   ^   s   
z)ChatCompletionStream.get_final_completionc                 C  s   t |  | S )*Blocks until the stream has been consumed.)r'   r]   rV   rV   rW   rr   i   s   zChatCompletionStream.until_doner   c                 C     | j jS r[   rS   current_completion_snapshotr]   rV   rV   rW   rw   n      z0ChatCompletionStream.current_completion_snapshotc                 c  s.    | j D ]}| j|}|D ]}|V  qqd S r[   rM   rS   handle_chunkrU   Z	sse_eventevents_to_fireeventrV   rV   rW   rP   r   s   
zChatCompletionStream.__stream__N)rA   rB   rC   rD   rE   rF   rG   rH   rG   rZ   )rG   r^   rG   r   rd   re   rf   rg   rh   ri   rG   rH   rG   rH   rG   ro   rG   r   )__name__
__module____qualname____doc__rX   r\   rb   rc   rm   rk   rs   rr   propertyrw   rP   rV   rV   rV   rW   r?   /       







r?   c                   @  .   e Zd ZdZdd
dZdddZdddZdS )ChatCompletionStreamManageraK  Context manager over a `ChatCompletionStream` that is returned by `.stream()`.

    This context manager ensures the response cannot be leaked if you don't read
    the stream to completion.

    Usage:
    ```py
    with client.beta.chat.completions.stream(...) as stream:
        for event in stream:
            ...
    ```
    api_request)Callable[[], Stream[ChatCompletionChunk]]rC   rD   rE   rF   rG   rH   c                C     d | _ || _|| _|| _d S r[   )$_ChatCompletionStreamManager__stream)_ChatCompletionStreamManager__api_request-_ChatCompletionStreamManager__response_format)_ChatCompletionStreamManager__input_toolsrU   r   rC   rE   rV   rV   rW   rX         
z$ChatCompletionStreamManager.__init__%ChatCompletionStream[ResponseFormatT]c                 C  s"   |   }t|| j| jd| _| jS N)rA   rC   rE   )r   r?   r   r   r   rU   rA   rV   rV   rW   rc      s   z%ChatCompletionStreamManager.__enter__rd   re   rf   rg   rh   ri   c                 C  s   | j d ur| j   d S d S r[   )r   rk   rl   rV   rV   rW   rm      s   
z$ChatCompletionStreamManager.__exit__N)r   r   rC   rD   rE   rF   rG   rH   )rG   r   r   )r   r   r   r   rX   rc   rm   rV   rV   rV   rW   r   y   
    

r   c                   @  r>   )2AsyncChatCompletionStreamr@   rA    AsyncStream[ChatCompletionChunk]rC   rD   rE   rF   rG   rH   c                C  rI   rJ   rL   rT   rV   rV   rW   rX      rY   z"AsyncChatCompletionStream.__init__rZ   c                   s   | j  I d H S r[   )rQ   	__anext__r]   rV   rV   rW   r      s   z#AsyncChatCompletionStream.__anext__9AsyncIterator[ChatCompletionStreamEvent[ResponseFormatT]]c                 C s"   | j 2 z	3 d H W }|V  q6 d S r[   r_   r`   rV   rV   rW   	__aiter__   s   z#AsyncChatCompletionStream.__aiter__r   c                   s   | S r[   rV   r]   rV   rV   rW   
__aenter__   s   z$AsyncChatCompletionStream.__aenter__rd   re   rf   rg   rh   ri   c                   s   |   I d H  d S r[   rj   rl   rV   rV   rW   	__aexit__   s   z#AsyncChatCompletionStream.__aexit__c                   s   | j  I dH  dS rn   )rO   acloser]   rV   rV   rW   rk      s   zAsyncChatCompletionStream.closero   c                   s   |   I dH  | j S )rp   Nrq   r]   rV   rV   rW   rs      s   
z.AsyncChatCompletionStream.get_final_completionc                   s   t | I dH  | S )rt   N)r(   r]   rV   rV   rW   rr      s   z$AsyncChatCompletionStream.until_doner   c                 C  ru   r[   rv   r]   rV   rV   rW   rw      rx   z5AsyncChatCompletionStream.current_completion_snapshotc                 C s8   | j 2 z3 d H W }| j|}|D ]}|V  qq6 d S r[   ry   r{   rV   rV   rW   rP      s   z$AsyncChatCompletionStream.__stream__N)rA   r   rC   rD   rE   rF   rG   rH   r~   )rG   r   r   r   r   r   r   )r   r   r   r   rX   r   r   r   r   rk   rs   rr   r   rw   rP   rV   rV   rV   rW   r      r   r   c                   @  r   ) AsyncChatCompletionStreamManageraV  Context manager over a `AsyncChatCompletionStream` that is returned by `.stream()`.

    This context manager ensures the response cannot be leaked if you don't read
    the stream to completion.

    Usage:
    ```py
    async with client.beta.chat.completions.stream(...) as stream:
        for event in stream:
            ...
    ```
    r   +Awaitable[AsyncStream[ChatCompletionChunk]]rC   rD   rE   rF   rG   rH   c                C  r   r[   ))_AsyncChatCompletionStreamManager__stream._AsyncChatCompletionStreamManager__api_request2_AsyncChatCompletionStreamManager__response_format._AsyncChatCompletionStreamManager__input_toolsr   rV   rV   rW   rX      r   z)AsyncChatCompletionStreamManager.__init__*AsyncChatCompletionStream[ResponseFormatT]c                   s(   | j I d H }t|| j| jd| _| jS r   )r   r   r   r   r   r   rV   rV   rW   r     s   z+AsyncChatCompletionStreamManager.__aenter__rd   re   rf   rg   rh   ri   c                   s$   | j d ur| j  I d H  d S d S r[   )r   rk   rl   rV   rV   rW   r     s   
z*AsyncChatCompletionStreamManager.__aexit__N)r   r   rC   rD   rE   rF   rG   rH   )rG   r   r   )r   r   r   r   rX   r   r   rV   rV   rV   rW   r      r   r   c                   @  sb   e Zd ZdZeedd"d	d
Zd#ddZed$ddZd%ddZ	d&ddZ
d'ddZd(dd Zd!S ))rR   aS  Helper class for manually accumulating `ChatCompletionChunk`s into a final `ChatCompletion` object.

    This is useful in cases where you can't always use the `.stream()` method, e.g.

    ```py
    from openai.lib.streaming.chat import ChatCompletionStreamState

    state = ChatCompletionStreamState()

    stream = client.chat.completions.create(..., stream=True)
    for chunk in response:
        state.handle_chunk(chunk)

        # can also access the accumulated `ChatCompletion` mid-stream
        state.current_completion_snapshot

    print(state.get_final_completion())
    ```
    )rE   rC   rE   rF   rC   rD   rG   rH   c                C  sL   d | _ g | _t|rdd |D ng | _|| _t|r!|| _d S t| _d S )Nc                 S  s   g | ]}|qS rV   rV   ).0ZtoolrV   rV   rW   
<listcomp>?  s    z6ChatCompletionStreamState.__init__.<locals>.<listcomp>)	7_ChatCompletionStreamState__current_completion_snapshot/_ChatCompletionStreamState__choice_event_statesr&   _input_tools_response_formatinspectisclassr#   _rich_response_format)rU   rE   rC   rV   rV   rW   rX   6  s
   z"ChatCompletionStreamState.__init__ro   c                 C  s   t | j| j| jdS )zParse the final completion object.

        Note this does not provide any guarantees that the stream has actually finished, you must
        only call this method when the stream is finished.
        )Zchat_completionrC   rE   )r0   rw   r   r   r]   rV   rV   rW   rs   C  s
   z.ChatCompletionStreamState.get_final_completionr   c                 C  s   | j d usJ | j S r[   )r   r]   rV   rV   rW   rw   O  s   z5ChatCompletionStreamState.current_completion_snapshotchunkr6   4Iterable[ChatCompletionStreamEvent[ResponseFormatT]]c                 C  s   |  || _| j|| jdS )zTAccumulate a new chunk into the snapshot and returns an iterable of events to yield.)r   completion_snapshot)_accumulate_chunkr   _build_events)rU   r   rV   rV   rW   rz   T  s
   z&ChatCompletionStreamState.handle_chunkchoiceChoiceChunkChoiceEventStatec                 C  s>   z| j |j W S  ty   t| jd}| j | | Y S w )N)rE   )r   index
IndexErrorr   r   append)rU   r   choice_staterV   rV   rW   _get_choice_state]  s   z+ChatCompletionStreamState._get_choice_statec                 C  s  | j }|d u rt|S |jD ]\}zd|j|j }|jjpg }ttttt	tdt
|jttddd t|jjp4g D ddtd|j d|_t|D ]%\}}|jjpWg | }|jdkrm|jdksfJ |jj|j_qNtrst| qNW n' ty   tttti |j
dd	hd
d|j id}|j| Y nw |jr|j|_t| j| jdr|jdkrt|d|jdkrt |jjr|jjst| j rt!t"|jjddd|j_#|jjpg D ]A}	|jjpg |	j }
|
jdkrt$| j|
jj%d}|r|&di &dr|
jj'rt!t"|
jj'ddd|
j_qtrt|
 q|j(d urk|j(d u r9t)t*|j(j|j(jd|_(q|j(jrR|j(jd u rIg |j(_|j(j+|j(j |j(jrk|j(jd u rbg |j(_|j(j+|j(j q|j,|_,|j-|_-|S )Nzdict[object, object]Tc                 S  s   i | ]\}}|d ddiiqS )functionparsed_argumentsTrV   )r   idx_rV   rV   rW   
<dictcomp>  s    z?ChatCompletionStreamState._accumulate_chunk.<locals>.<dictcomp>)parsed
tool_calls)excludetype_valuer   deltaZexclude_unsetr   messagerK   length)
completionZcontent_filterzutf-8)Zpartial_mode)rE   namestrict)contentrefusal).r   $_convert_initial_chunk_into_snapshotchoicesr   r   r   r   r   r+   r!   r)   r$   	enumerater   to_dicttyper   r   r   r   r   r   r   finish_reasonr.   r   r   r9   r:   r   r   r&   r   r   bytesr   r1   r   get	argumentslogprobsr*   r;   extendusagesystem_fingerprint)rU   r   r   r   choice_snapshotZprevious_tool_calls
tool_indexZ	prev_toolZnew_toolZtool_call_chunktool_call_snapshotZ
input_toolrV   rV   rW   r   e  s    











z+ChatCompletionStreamState._accumulate_chunkr   0list[ChatCompletionStreamEvent[ResponseFormatT]]c          
      C  s  g }| ttd||d |jD ]}| |}|j|j }|jjd ur;|jjd ur;| tt	d|jj|jj|jj
d |jjd urV|jjd urV| ttd|jj|jjd |jjr|jj}|d usdJ |jjD ]3}||j }	|	jdkr|jd us{J | ttd|	jj|j|	jj|	jj|jjpd	d
 qhtrt|	 qh|jd ur|jd ur|jjr|jjr| ttd|jj|jjd |jjr|jjr| ttd|jj|jjd ||j||| jd q|S )Nr   )r   r   snapshotzcontent.delta)r   r   r   r   zrefusal.delta)r   r   r   r   z#tool_calls.function.arguments.delta )r   r   r   r   r   Zarguments_deltazlogprobs.content.delta)r   r   r   zlogprobs.refusal.delta)r   r   r   )choice_chunkr   rC   )r   r*   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   get_done_eventsr   )
rU   r   r   r|   r   r   r   r   Ztool_call_delta	tool_callrV   rV   rW   r     s   


	


		z'ChatCompletionStreamState._build_eventsN)rE   rF   rC   rD   rG   rH   r   r   )r   r6   rG   r   )r   r   rG   r   r   r6   rG   r   )r   r6   r   r   rG   r   )r   r   r   r   r#   rX   rs   r   rw   rz   r   r   r   rV   rV   rV   rW   rR   !  s    


	
 rR   c                   @  s4   e Zd ZdddZdddZdddZdddZdS )r   rE   list[ChatCompletionToolParam]rG   rH   c                C  s0   || _ d| _d| _d| _d| _t | _d | _d S )NF)r   _content_done_refusal_done_logprobs_content_done_logprobs_refusal_doneset_done_tool_calls*_ChoiceEventState__current_tool_call_index)rU   rE   rV   rV   rW   rX   E  s   
zChoiceEventState.__init__r   r   r   r   rC   rD   r   c                C  s   g }|j r#|| j||d | jd ur#| j| jvr#| j||| jd |jjp(g D ]$}| j|jkrI|| j||d | jd urI| j||| jd |j| _q)|S )N)r   rC   )r|   r   r   )	r   r   _content_done_eventsr   r   _add_tool_done_eventr   r   r   )rU   r   r   rC   r|   r   rV   rV   rW   r   O  s2   


z ChoiceEventState.get_done_eventsc                C  s  g }|j jr.| js.d| _t||j d}||j _|ttdttt	t
| d|j j|d |j jd urF| jsFd| _|ttd|j jd |jd urc|jjd urc| jscd| _|ttd|jjd	 |jd ur|jjd ur| jsd| _|ttd
|jjd |S )NT)rC   r   z'type[ContentDoneEvent[ResponseFormatT]]zcontent.done)r   r   r   zrefusal.done)r   r   zlogprobs.content.done)r   r   zlogprobs.refusal.done)r   r   r   r/   r   r   r*   r   r   r   r2   r   r   r   r   r   r   r   r   )rU   r   rC   r|   r   rV   rV   rW   r   x  sR   

z%ChoiceEventState._content_done_eventsr|   r   intc             
   C  s   || j v rd S | j | |jjd usJ |jj| }|jdkr?t| j|jd}||j_|	t
td||jj|jj|d d S trGt| d S d S )Nr   )rE   r   z"tool_calls.function.arguments.done)r   r   r   r   r   )r   addr   r   r   r3   r   r   r   r   r*   r   r   r   r   r   )rU   r|   r   r   r   r   rV   rV   rW   r     s.   


z%ChoiceEventState._add_tool_done_eventN)rE   r   rG   rH   )r   r   r   r   rC   rD   rG   r   )r   r   rC   rD   rG   r   )r|   r   r   r   r   r   rG   rH   )r   r   r   rX   r   r   r   rV   rV   rV   rW   r   D  s
    



)@r   r   r6   rG   r   c                 C  sl   |   }td|d }| jD ]}i |jddhdd|j  i||j< qttttdd i|dd	id
S )Nzlist[object]r   Tr   r   r   r   objectzchat.completionr   )r   r   r   r)   r   r   r   r+   )r   datar   r   rV   rV   rW   r     s(   

r   r   )T
__future__r   r   typesr   typingr   r   r   r   r   r	   r
   r   Ztyping_extensionsr   r   r   Zjiterr   _typesr   r   r   Z_eventsr   r   r   r   r   r   r   r   r   r   r   r   Z_deltasr!   r#   r$   r%   _utilsr&   r'   r(   _compatr)   Z_modelsr*   r+   Z_parsingr-   r.   r/   r0   r1   r2   r3   Z
_streamingr4   r5   Z
types.chatr6   r7   r8   _exceptionsr9   r:   Ztypes.chat.chat_completionr;   Z types.chat.chat_completion_chunkr<   r   Z#types.chat.completion_create_paramsr=   ZResponseFormatParamr?   r   r   r   rR   r   r   rV   rV   rV   rW   <module>   s<    (8$	J/J/  % 