a
    fo                     @  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( ddl)m*Z*m+Z+m,Z, ddl-m.Z. ddl/m0Z0m1Z1 ddl2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9 ddl:m;Z;m<Z< ddl=m>Z>m?Z?m@Z@ ddlAmBZBmCZC ddlDmEZE ddlFmGZH ddlImJZK G dd dee3 ZLG dd dee3 ZMG dd dee3 ZNG dd  d ee3 ZOG d!d" d"ee3 ZPG d#d$ d$ZQd%d&d'd(d)ZRd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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rD   rE   _raw_streamresponse	_response
__stream__	_iteratorChatCompletionStreamState_stateselfrC   rD   rE    rS   _/var/www/ai-form-bot/venv/lib/python3.9/site-packages/openai/lib/streaming/chat/_completions.py__init__8   s    
zChatCompletionStream.__init__*ChatCompletionStreamEvent[ResponseFormatT]rF   c                 C  s
   | j  S N)rN   __next__rR   rS   rS   rT   rY   D   s    zChatCompletionStream.__next__z4Iterator[ChatCompletionStreamEvent[ResponseFormatT]]c                 c  s   | j D ]
}|V  qd S rX   rN   rR   itemrS   rS   rT   __iter__G   s    
zChatCompletionStream.__iter__r   c                 C  s   | S rX   rS   rZ   rS   rS   rT   	__enter__K   s    zChatCompletionStream.__enter__type[BaseException] | NoneBaseException | NoneTracebackType | Noneexc_typeexcexc_tbrF   c                 C  s   |    d S rX   closerR   rd   re   rf   rS   rS   rT   __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)rL   rh   rZ   rS   rS   rT   rh   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_donerP   get_final_completionrZ   rS   rS   rT   rp   ^   s    z)ChatCompletionStream.get_final_completionc                 C  s   t |  | S )*Blocks until the stream has been consumed.)r&   rZ   rS   rS   rT   ro   i   s    zChatCompletionStream.until_doner   c                 C  s   | j jS rX   rP   current_completion_snapshotrZ   rS   rS   rT   rs   n   s    z0ChatCompletionStream.current_completion_snapshotc                 c  s,   | j D ] }| j|}|D ]
}|V  qqd S rX   rJ   rP   handle_chunkrR   Z	sse_eventevents_to_fireeventrS   rS   rT   rM   r   s    
zChatCompletionStream.__stream__N)__name__
__module____qualname____doc__rU   rY   r^   r_   rj   rh   rp   ro   propertyrs   rM   rS   rS   rS   rT   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?   r@   rA   api_requestrD   rE   rF   c                C  s   d | _ || _|| _|| _d S rX   )$_ChatCompletionStreamManager__stream)_ChatCompletionStreamManager__api_request-_ChatCompletionStreamManager__response_format)_ChatCompletionStreamManager__input_toolsrR   r   rD   rE   rS   rS   rT   rU      s    z$ChatCompletionStreamManager.__init__z%ChatCompletionStream[ResponseFormatT]rW   c                 C  s"   |   }t|| j| jd| _| jS N)rC   rD   rE   )r   r=   r   r   r   rR   rC   rS   rS   rT   r_      s    z%ChatCompletionStreamManager.__enter__r`   ra   rb   rc   c                 C  s   | j d ur| j   d S rX   )r   rh   ri   rS   rS   rT   rj      s    
z$ChatCompletionStreamManager.__exit__N)ry   rz   r{   r|   rU   r_   rj   rS   rS   rS   rT   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?   r@   rA   rB   c                C  s*   || _ |j| _|  | _t||d| _d S rG   rI   rQ   rS   rS   rT   rU      s    
z"AsyncChatCompletionStream.__init__rV   rW   c                   s   | j  I d H S rX   )rN   	__anext__rZ   rS   rS   rT   r      s    z#AsyncChatCompletionStream.__anext__z9AsyncIterator[ChatCompletionStreamEvent[ResponseFormatT]]c                 C s    | j 2 z3 d H W }|V  q6 d S rX   r[   r\   rS   rS   rT   	__aiter__   s    z#AsyncChatCompletionStream.__aiter__r   c                   s   | S rX   rS   rZ   rS   rS   rT   
__aenter__   s    z$AsyncChatCompletionStream.__aenter__r`   ra   rb   rc   c                   s   |   I d H  d S rX   rg   ri   rS   rS   rT   	__aexit__   s    z#AsyncChatCompletionStream.__aexit__c                   s   | j  I dH  dS rk   )rL   acloserZ   rS   rS   rT   rh      s    zAsyncChatCompletionStream.closerl   c                   s   |   I dH  | j S )rm   Nrn   rZ   rS   rS   rT   rp      s    z.AsyncChatCompletionStream.get_final_completionc                   s   t | I dH  | S )rq   N)r'   rZ   rS   rS   rT   ro      s    z$AsyncChatCompletionStream.until_doner   c                 C  s   | j jS rX   rr   rZ   rS   rS   rT   rs      s    z5AsyncChatCompletionStream.current_completion_snapshotc                 C s6   | j 2 z(3 d H W }| j|}|D ]
}|V  q"q6 d S rX   rt   rv   rS   rS   rT   rM      s    z$AsyncChatCompletionStream.__stream__N)ry   rz   r{   r|   rU   r   r   r   r   rh   rp   ro   r}   rs   rM   rS   rS   rS   rT   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?   r@   rA   r   c                C  s   d | _ || _|| _|| _d S rX   ))_AsyncChatCompletionStreamManager__stream._AsyncChatCompletionStreamManager__api_request2_AsyncChatCompletionStreamManager__response_format._AsyncChatCompletionStreamManager__input_toolsr   rS   rS   rT   rU      s    z)AsyncChatCompletionStreamManager.__init__z*AsyncChatCompletionStream[ResponseFormatT]rW   c                   s&   | j I d H }t|| j| jd| _| jS r   )r   r   r   r   r   r   rS   rS   rT   r     s    z+AsyncChatCompletionStreamManager.__aenter__r`   ra   rb   rc   c                   s   | j d ur| j  I d H  d S rX   )r   rh   ri   rS   rS   rT   r     s    
z*AsyncChatCompletionStreamManager.__aexit__N)ry   rz   r{   r|   rU   r   r   rS   rS   rS   rT   r      s   r   c                   @  s   e Zd Z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 )rO   r@   r?   rA   )rE   rD   rF   c                C  sF   d | _ g | _t|r"dd |D ng | _|| _t|r<|nt| _d S )Nc                 S  s   g | ]}|qS rS   rS   ).0ZtoolrS   rS   rT   
<listcomp>+      z6ChatCompletionStreamState.__init__.<locals>.<listcomp>)	7_ChatCompletionStreamState__current_completion_snapshot/_ChatCompletionStreamState__choice_event_statesr%   _input_tools_response_formatinspectisclassr#   _rich_response_format)rR   rE   rD   rS   rS   rT   rU   "  s
    z"ChatCompletionStreamState.__init__rl   rW   c                 C  s   t | j| j| jdS )N)Zchat_completionrD   rE   )r/   rs   r   r   rZ   rS   rS   rT   rp   /  s
    z.ChatCompletionStreamState.get_final_completionr   c                 C  s   | j d usJ | j S rX   )r   rZ   rS   rS   rT   rs   6  s    z5ChatCompletionStreamState.current_completion_snapshotr5   0list[ChatCompletionStreamEvent[ResponseFormatT]]chunkrF   c                 C  s   |  || _| j|| jdS )zOAccumulate a new chunk into the snapshot and returns a list of events to yield.)r   completion_snapshot)_accumulate_chunkr   _build_events)rR   r   rS   rS   rT   ru   ;  s
    z&ChatCompletionStreamState.handle_chunkChoiceChunkChoiceEventState)choicerF   c                 C  sB   z| j |j W S  ty<   t| jd}| j | | Y S 0 d S )N)rE   )r   index
IndexErrorr   r   append)rR   r   choice_staterS   rS   rT   _get_choice_stateD  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ddd t|jjpdg 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y4   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rjt |jdkr|t |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r:t#| j|
jj$d}|rH|%di %drH|
jj&rHt t!|
jj&ddd|
j_ntrt|
 q|j'd ur|j'd u r|t(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_argumentsTrS   )r   idx_rS   rS   rT   
<dictcomp>e  s   z?ChatCompletionStreamState._accumulate_chunk.<locals>.<dictcomp>)parsed
tool_calls)excludetype_valuer   deltaZexclude_unsetr   messagerH   lengthZ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(   	enumerater   to_dicttyper   r   r   r   r   r   r   finish_reasonr-   r   r   r8   r9   r   r   r%   r   r   bytesr   r0   r   get	argumentslogprobsr)   r:   extendusagesystem_fingerprint)rR   r   r   r   choice_snapshotZprevious_tool_calls
tool_indexZ	prev_toolZnew_toolZtool_call_chunktool_call_snapshotZ
input_toolrS   rS   rT   r   L  s    







z+ChatCompletionStreamState._accumulate_chunk)r   r   rF   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   Zarguments_deltazlogprobs.content.delta)r   r   r   zlogprobs.refusal.delta)r   r   r   )choice_chunkr   rD   )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   )
rR   r   r   rw   r   r   r   r   Ztool_call_delta	tool_callrS   rS   rT   r     s    

	


		z'ChatCompletionStreamState._build_eventsN)ry   rz   r{   rU   rp   r}   rs   ru   r   r   r   rS   rS   rS   rT   rO   !  s   	}rO   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]rA   )rE   rF   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)rR   rE   rS   rS   rT   rU   &  s    zChoiceEventState.__init__r   r   r?   r   )r   r   rD   rF   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   rD   )rw   r   r   )	r   r   _content_done_eventsr   r   _add_tool_done_eventr   r   r   )rR   r   r   rD   rw   r   rS   rS   rT   r   0  s6    


z ChoiceEventState.get_done_events)r   rD   rF   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)rD   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   r1   r   r   r   r   r   r   r   r   )rR   r   rD   rw   r   rS   rS   rT   r   Y  sZ    

z%ChoiceEventState._content_done_eventsint)rw   r   r   rF   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   )rE   r   z"tool_calls.function.arguments.done)r   r   r   r   r   )r   addr   r   r   r2   r   r   r   r   r)   r   r   r   r   r   )rR   rw   r   r   r   r   rS   rS   rT   r     s,    


z%ChoiceEventState._add_tool_done_eventN)ry   rz   r{   rU   r   r   r   rS   rS   rS   rT   r   %  s   
)@r   r5   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   rS   rS   rT   r     s(    

r   )S
__future__r   r   typesr   typingr   r   r   r   r   r	   r
   r   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$   Z_utilsr%   r&   r'   _compatr(   Z_modelsr)   r*   Z_parsingr,   r-   r.   r/   r0   r1   r2   Z
_streamingr3   r4   Z
types.chatr5   r6   r7   _exceptionsr8   r9   Ztypes.chat.chat_completionr:   Z types.chat.chat_completion_chunkr;   r   Z#types.chat.completion_create_paramsr<   ZResponseFormatParamr=   r~   r   r   rO   r   r   rS   rS   rS   rT   <module>   s:   (8$	J/J/   