a
    ~gt                     @  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'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                   @  s   e Zd ZdZdddddddZd	d
ddZdd
ddZdd
ddZdddddddZdd
ddZ	dd
ddZ
dd
ddZed d
d!d"Zdd
d#d$Zd%S )&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
    zStream[ChatCompletionChunk]6type[ResponseFormatT] | ResponseFormatParam | NotGiven,Iterable[ChatCompletionToolParam] | NotGivenNone
raw_streamresponse_formatinput_toolsreturnc                C  s*   || _ |j| _|  | _t||d| _d S NrE   rF   _raw_streamresponse	_response
__stream__	_iteratorChatCompletionStreamState_stateselfrD   rE   rF    rT   k/var/www/html/emsaiapi.evdpl.com/venv/lib/python3.9/site-packages/openai/lib/streaming/chat/_completions.py__init__8   s    
zChatCompletionStream.__init__*ChatCompletionStreamEvent[ResponseFormatT]rG   c                 C  s
   | j  S N)rO   __next__rS   rT   rT   rU   rZ   D   s    zChatCompletionStream.__next__z4Iterator[ChatCompletionStreamEvent[ResponseFormatT]]c                 c  s   | j D ]
}|V  qd S rY   rO   rS   itemrT   rT   rU   __iter__G   s    
zChatCompletionStream.__iter__r   c                 C  s   | S rY   rT   r[   rT   rT   rU   	__enter__K   s    zChatCompletionStream.__enter__type[BaseException] | NoneBaseException | NoneTracebackType | Noneexc_typeexcexc_tbrG   c                 C  s   |    d S rY   closerS   re   rf   rg   rT   rT   rU   __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)rM   ri   r[   rT   rT   rU   ri   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_donerQ   get_final_completionr[   rT   rT   rU   rq   ^   s    z)ChatCompletionStream.get_final_completionc                 C  s   t |  | S )*Blocks until the stream has been consumed.)r'   r[   rT   rT   rU   rp   i   s    zChatCompletionStream.until_doner   c                 C  s   | j jS rY   rQ   current_completion_snapshotr[   rT   rT   rU   rt   n   s    z0ChatCompletionStream.current_completion_snapshotc                 c  s,   | j D ] }| j|}|D ]
}|V  qqd S rY   rK   rQ   handle_chunkrS   Z	sse_eventevents_to_fireeventrT   rT   rU   rN   r   s    
zChatCompletionStream.__stream__N)__name__
__module____qualname____doc__rV   rZ   r_   r`   rk   ri   rq   rp   propertyrt   rN   rT   rT   rT   rU   r>   /   s   r>   c                   @  sF   e Zd ZdZdddddddZd	d
ddZdddd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:
            ...
    ```
    z)Callable[[], Stream[ChatCompletionChunk]]r@   rA   rB   api_requestrE   rF   rG   c                C  s   d | _ || _|| _|| _d S rY   )$_ChatCompletionStreamManager__stream)_ChatCompletionStreamManager__api_request-_ChatCompletionStreamManager__response_format)_ChatCompletionStreamManager__input_toolsrS   r   rE   rF   rT   rT   rU   rV      s    z$ChatCompletionStreamManager.__init__z%ChatCompletionStream[ResponseFormatT]rX   c                 C  s"   |   }t|| j| jd| _| jS N)rD   rE   rF   )r   r>   r   r   r   rS   rD   rT   rT   rU   r`      s    z%ChatCompletionStreamManager.__enter__ra   rb   rc   rd   c                 C  s   | j d ur| j   d S rY   )r   ri   rj   rT   rT   rU   rk      s    
z$ChatCompletionStreamManager.__exit__N)rz   r{   r|   r}   rV   r`   rk   rT   rT   rT   rU   r   y   s   r   c                   @  s   e Zd ZdZdddddddZd	d
ddZdd
ddZdd
ddZdddddddZdd
ddZ	dd
ddZ
dd
ddZed d
d!d"Zdd
d#d$Zd%S )&AsyncChatCompletionStreamr?   z AsyncStream[ChatCompletionChunk]r@   rA   rB   rC   c                C  s*   || _ |j| _|  | _t||d| _d S rH   rJ   rR   rT   rT   rU   rV      s    
z"AsyncChatCompletionStream.__init__rW   rX   c                   s   | j  I d H S rY   )rO   	__anext__r[   rT   rT   rU   r      s    z#AsyncChatCompletionStream.__anext__z9AsyncIterator[ChatCompletionStreamEvent[ResponseFormatT]]c                 C s    | j 2 z3 d H W }|V  q6 d S rY   r\   r]   rT   rT   rU   	__aiter__   s    z#AsyncChatCompletionStream.__aiter__r   c                   s   | S rY   rT   r[   rT   rT   rU   
__aenter__   s    z$AsyncChatCompletionStream.__aenter__ra   rb   rc   rd   c                   s   |   I d H  d S rY   rh   rj   rT   rT   rU   	__aexit__   s    z#AsyncChatCompletionStream.__aexit__c                   s   | j  I dH  dS rl   )rM   acloser[   rT   rT   rU   ri      s    zAsyncChatCompletionStream.closerm   c                   s   |   I dH  | j S )rn   Nro   r[   rT   rT   rU   rq      s    z.AsyncChatCompletionStream.get_final_completionc                   s   t | I dH  | S )rr   N)r(   r[   rT   rT   rU   rp      s    z$AsyncChatCompletionStream.until_doner   c                 C  s   | j jS rY   rs   r[   rT   rT   rU   rt      s    z5AsyncChatCompletionStream.current_completion_snapshotc                 C s6   | j 2 z(3 d H W }| j|}|D ]
}|V  q"q6 d S rY   ru   rw   rT   rT   rU   rN      s    z$AsyncChatCompletionStream.__stream__N)rz   r{   r|   r}   rV   r   r   r   r   ri   rq   rp   r~   rt   rN   rT   rT   rT   rU   r      s   r   c                   @  sF   e Zd ZdZdddddddZd	d
ddZdddddddZdS ) 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:
            ...
    ```
    z+Awaitable[AsyncStream[ChatCompletionChunk]]r@   rA   rB   r   c                C  s   d | _ || _|| _|| _d S rY   ))_AsyncChatCompletionStreamManager__stream._AsyncChatCompletionStreamManager__api_request2_AsyncChatCompletionStreamManager__response_format._AsyncChatCompletionStreamManager__input_toolsr   rT   rT   rU   rV      s    z)AsyncChatCompletionStreamManager.__init__z*AsyncChatCompletionStream[ResponseFormatT]rX   c                   s&   | j I d H }t|| j| jd| _| jS r   )r   r   r   r   r   r   rT   rT   rU   r     s    z+AsyncChatCompletionStreamManager.__aenter__ra   rb   rc   rd   c                   s   | j d ur| j  I d H  d S rY   )r   ri   rj   rT   rT   rU   r     s    
z*AsyncChatCompletionStreamManager.__aexit__N)rz   r{   r|   r}   rV   r   r   rT   rT   rT   rU   r      s   r   c                   @  s   e Zd ZdZeedddddddZd	d
ddZedd
ddZdddddZ	dddddZ
dddddZddddddZd S )!rP   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())
    ```
    )rF   rE   rA   r@   rB   )rF   rE   rG   c                C  sF   d | _ g | _t|r"dd |D ng | _|| _t|r<|nt| _d S )Nc                 S  s   g | ]}|qS rT   rT   ).0toolrT   rT   rU   
<listcomp>?      z6ChatCompletionStreamState.__init__.<locals>.<listcomp>)	7_ChatCompletionStreamState__current_completion_snapshot/_ChatCompletionStreamState__choice_event_statesr&   _input_tools_response_formatinspectisclassr#   _rich_response_format)rS   rF   rE   rT   rT   rU   rV   6  s
    z"ChatCompletionStreamState.__init__rm   rX   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.
        )chat_completionrE   rF   )r0   rt   r   r   r[   rT   rT   rU   rq   C  s
    z.ChatCompletionStreamState.get_final_completionr   c                 C  s   | j d usJ | j S rY   )r   r[   rT   rT   rU   rt   O  s    z5ChatCompletionStreamState.current_completion_snapshotr6   z4Iterable[ChatCompletionStreamEvent[ResponseFormatT]]chunkrG   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)rS   r   rT   rT   rU   rv   T  s
    z&ChatCompletionStreamState.handle_chunkChoiceChunkChoiceEventState)choicerG   c                 C  sB   z| j |j W S  ty<   t| jd}| j | | Y S 0 d S )N)rF   )r   index
IndexErrorr   r   append)rS   r   choice_staterT   rT   rU   _get_choice_state]  s    z+ChatCompletionStreamState._get_choice_statec                 C  s  | j }|d u rt|S |jD ]}z|j|j }|jjp:g }ttttt	tdt
|jttddd t|jjphg D ddtd|j d|_t|D ]J\}}|jjpg | }|jdkr|jdksJ |jj|j_qtrt| qW nP ty:   tttti |j
dd	hd
d|j id}|j| Y n0 |jr|j|_t| j| jdr|jdkrtt|d|jdkrt |jjr|jjst| j rt!t"|jjddd|j_#|jjpg D ]}	|jjpg |	j }
|
jdkrDt$| j|
jj%d}|rR|&di &drR|
jj'rRt!t"|
jj'ddd|
j_ntrt|
 q|j(d ur|j(d u rt)t*|j(j|j(jd|_(q|j(jr|j(jd u rg |j(_|j(j+|j(j |j(jr|j(jd u rg |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_argumentsTrT   )r   idx_rT   rT   rU   
<dictcomp>  s   z?ChatCompletionStreamState._accumulate_chunk.<locals>.<dictcomp>)parsed
tool_calls)excludetype_valuer   deltaexclude_unsetr   messagerI   length)
completioncontent_filterzutf-8)Zpartial_mode)rF   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)rS   r   r   r   choice_snapshotZprevious_tool_calls
tool_indexZ	prev_toolZnew_toolZtool_call_chunktool_call_snapshot
input_toolrT   rT   rU   r   e  s     








z+ChatCompletionStreamState._accumulate_chunk0list[ChatCompletionStreamEvent[ResponseFormatT]])r   r   rG   c          
      C  s  g }| ttd||d |jD ]}| |}|j|j }|jjd urx|jjd urx| tt	d|jj|jj|jj
d |jjd ur|jjd ur| ttd|jj|jjd |jjr@|jj}|d usJ |jjD ]j}||j }	|	jdkr2|jd usJ | ttd|	jj|j|	jj|	jj|jjp(d	d
 qtrt|	 q|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   arguments_deltazlogprobs.content.delta)r   r   r   zlogprobs.refusal.delta)r   r   r   )choice_chunkr   rE   )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   )
rS   r   r   rx   r   r   r   r   tool_call_delta	tool_callrT   rT   rU   r     s    

	


		z'ChatCompletionStreamState._build_eventsN)rz   r{   r|   r}   r#   rV   rq   r~   rt   rv   r   r   r   rT   rT   rT   rU   rP   !  s   	 rP   c                   @  sV   e Zd ZdddddZdddd	d
ddZddd	dddZd	ddddddZdS )r   zlist[ChatCompletionToolParam]rB   )rF   rG   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)rS   rF   rT   rT   rU   rV   E  s    zChoiceEventState.__init__r   r   r@   r   )r   r   rE   rG   c                C  s   g }|j rF|| j||d | jd urF| j| jvrF| j||| jd |jjpPg D ]H}| j|jkr|| j||d | jd ur| j||| jd |j| _qR|S )N)r   rE   )rx   r   r   )	r   r   _content_done_eventsr   r   _add_tool_done_eventr   r   r   )rS   r   r   rE   rx   r   rT   rT   rU   r   O  s6    


z ChoiceEventState.get_done_events)r   rE   rG   c                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r| jsd| _|ttd|j jd |jd ur|jjd ur| jsd| _|ttd|jjd	 |jd ur|jjd ur| jsd| _|ttd
|jjd |S )NT)rE   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   )rS   r   rE   rx   r   rT   rT   rU   r   x  sZ    

z%ChoiceEventState._content_done_eventsint)rx   r   r   rG   c             
   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 ntrt| d S )Nr   )rF   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   )rS   rx   r   r   r   r   rT   rT   rU   r     s,    


z%ChoiceEventState._add_tool_done_eventN)rz   r{   r|   rV   r   r   r   rT   rT   rT   rU   r   D  s   
)@r   r6   r   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   rT   rT   rU   r     s(    

r   )T
__future__r   r   typesr   typingr   r   r   r   r   r	   r
   r   typing_extensionsr   r   r   Zjiterr   _typesr   r   r   _eventsr   r   r   r   r   r   r   r   r   r   r   r   Z_deltasr!   r#   r$   r%   _utilsr&   r'   r(   _compatr)   _modelsr*   r+   _parsingr-   r.   r/   r0   r1   r2   r3   
_streamingr4   r5   
types.chatr6   r7   r8   _exceptionsr9   r:   types.chat.chat_completionr;    types.chat.chat_completion_chunkr<   r   Z#types.chat.completion_create_paramsr=   ResponseFormatParamr>   r   r   r   rP   r   r   rT   rT   rT   rU   <module>   s:   (8$	J/J/  % 