o
    畢hj                     @  s  d dl mZ d dlmZmZmZmZmZmZm	Z	 d dl
mZ 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 dd	lmZmZ dd
lmZ ddlmZmZ ddl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+m,Z-m.Z/ ddl0m1Z1 ddl2m3Z3m4Z4 ddl5m6Z6 ddl7m8Z8 ddl9m:Z: ddl;m<Z< ddl=m>Z> ddl?m@Z@ ddlAmBZB ddlCmDZD ddlEmFZF ddgZGG dd deZHG dd deZIG d d! d!ZJG d"d# d#ZKG d$d% d%ZLG d&d' d'ZMdS )(    )annotations)DictListTypeUnionIterableOptionalcast)partial)LiteralN   )_legacy_response)	NOT_GIVENBodyQueryHeadersNotGiven)maybe_transformasync_maybe_transform)cached_property)SyncAPIResourceAsyncAPIResource)to_streamed_response_wrapper"async_to_streamed_response_wrapper)Stream)completion_create_params)make_request_options)ResponseFormatTvalidate_input_toolsparse_chat_completiontype_to_response_format_param)	ChatModel)ChatCompletionStreamManager AsyncChatCompletionStreamManager)ChatCompletion)ChatCompletionChunk)ParsedChatCompletion)ChatCompletionModality)ChatCompletionToolParam)ChatCompletionAudioParam)ChatCompletionMessageParam) ChatCompletionStreamOptionsParam)#ChatCompletionToolChoiceOptionParamCompletionsAsyncCompletionsc                   @     e Zd ZedGddZedHddZeeeeeeeeeeeeeeeeeeeeeeeeeddded	dIdAdBZeeeeeeeeeeeeeeeeeeeeeeeeeddded	dJdEdFZdS )Kr-   returnCompletionsWithRawResponsec                 C     t | S a  
        This property can be used as a prefix for any HTTP method call to return the
        the raw response object instead of the parsed content.

        For more information, see https://www.github.com/openai/openai-python#accessing-raw-response-data-eg-headers
        )r1   self r6   ^/var/www/html/zoom/venv/lib/python3.10/site-packages/openai/resources/beta/chat/completions.pywith_raw_response*      zCompletions.with_raw_response CompletionsWithStreamingResponsec                 C  r2   z
        An alternative to `.with_raw_response` that doesn't eagerly read the response body.

        For more information, see https://www.github.com/openai/openai-python#with_streaming_response
        )r:   r4   r6   r6   r7   with_streaming_response4      z#Completions.with_streaming_responseNaudioresponse_formatfrequency_penaltyfunction_call	functions
logit_biaslogprobsmax_completion_tokens
max_tokensmetadata
modalitiesnparallel_tool_callspresence_penaltyseedservice_tierstopstorestream_optionstemperaturetool_choicetoolstop_logprobstop_puserextra_headersextra_query
extra_bodytimeoutmessages$Iterable[ChatCompletionMessageParam]modelUnion[str, ChatModel]r?   -Optional[ChatCompletionAudioParam] | NotGivenr@    type[ResponseFormatT] | NotGivenrA   Optional[float] | NotGivenrB   0completion_create_params.FunctionCall | NotGivenrC   6Iterable[completion_create_params.Function] | NotGivenrD   #Optional[Dict[str, int]] | NotGivenrE   Optional[bool] | NotGivenrF   Optional[int] | NotGivenrG   rH   #Optional[Dict[str, str]] | NotGivenrI   1Optional[List[ChatCompletionModality]] | NotGivenrJ   rK   bool | NotGivenrL   rM   rN   /Optional[Literal['auto', 'default']] | NotGivenrO   *Union[Optional[str], List[str]] | NotGivenrP   rQ   5Optional[ChatCompletionStreamOptionsParam] | NotGivenrR   rS   .ChatCompletionToolChoiceOptionParam | NotGivenrT   ,Iterable[ChatCompletionToolParam] | NotGivenrU   rV   rW   str | NotGivenrX   Headers | NonerY   Query | NonerZ   Body | Noner[   'float | httpx.Timeout | None | NotGiven%ParsedChatCompletion[ResponseFormatT]c          !        s   t  ddi|p
i }d  fdd} | jd	ti d
|d|d|d|d|d|d|d|	d|
d|d|d|d|d|d|dt d||||d||||||dtjt||||| dttt	t
  tddS )!a  Wrapper over the `client.chat.completions.create()` method that provides richer integrations with Python specific types
        & returns a `ParsedChatCompletion` object, which is a subclass of the standard `ChatCompletion` class.

        You can pass a pydantic model to this method and it will automatically convert the model
        into a JSON schema, send it to the API and parse the response content back into the given model.

        This method will also automatically parse `function` tool calls if:
        - You use the `openai.pydantic_function_tool()` helper method
        - You mark your tool schema with `"strict": True`

        Example usage:
        ```py
        from pydantic import BaseModel
        from openai import OpenAI


        class Step(BaseModel):
            explanation: str
            output: str


        class MathResponse(BaseModel):
            steps: List[Step]
            final_answer: str


        client = OpenAI()
        completion = client.beta.chat.completions.parse(
            model="gpt-4o-2024-08-06",
            messages=[
                {"role": "system", "content": "You are a helpful math tutor."},
                {"role": "user", "content": "solve 8x + 31 = 2"},
            ],
            response_format=MathResponse,
        )

        message = completion.choices[0].message
        if message.parsed:
            print(message.parsed.steps)
            print("answer: ", message.parsed.final_answer)
        ```
        X-Stainless-Helper-Methodbeta.chat.completions.parseraw_completionr$   r0   ru   c                      t  | dS N)r@   chat_completioninput_tools_parse_chat_completionrx   r@   rT   r6   r7   parser   
   z!Completions.parse.<locals>.parser/chat/completionsr\   r^   r?   rA   rB   rC   rD   rE   rF   rG   rH   rI   rJ   rK   rL   r@   rM   F)rN   rO   rP   streamrQ   rR   rS   rT   rU   rV   rW   rX   rY   rZ   r[   post_parserbodyoptionscast_tor   Nrx   r$   r0   ru   )_validate_input_tools_postr   _type_to_response_formatr   CompletionCreateParamsr   r	   r   r&   r   r$   !r5   r\   r^   r?   r@   rA   rB   rC   rD   rE   rF   rG   rH   rI   rJ   rK   rL   rM   rN   rO   rP   rQ   rR   rS   rT   rU   rV   rW   rX   rY   rZ   r[   r   r6   r   r7   parse=   s   O	
!	zCompletions.parseJcompletion_create_params.ResponseFormat | type[ResponseFormatT] | NotGiven,ChatCompletionStreamManager[ResponseFormatT]c          !      C  s   ddi|pi }t | jjjjfi d|d|d|dddt|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#|} t| ||d$S )%a  Wrapper over the `client.chat.completions.create(stream=True)` method that provides a more granular event API
        and automatic accumulation of each delta.

        This also supports all of the parsing utilities that `.parse()` does.

        Unlike `.create(stream=True)`, the `.stream()` method requires usage within a context manager to prevent accidental leakage of the response:

        ```py
        with client.beta.chat.completions.stream(
            model="gpt-4o-2024-08-06",
            messages=[...],
        ) as stream:
            for event in stream:
                if event.type == "content.delta":
                    print(event.delta, flush=True, end="")
        ```

        When the context manager is entered, a `ChatCompletionStream` instance is returned which, like `.create(stream=True)` is an iterator. The full list of events that are yielded by the iterator are outlined in [these docs](https://github.com/openai/openai-python/blob/main/helpers.md#chat-completions-events).

        When the context manager exits, the response will be closed, however the `stream` instance is still available outside
        the context manager.
        rv   beta.chat.completions.streamr\   r^   r?   r   Tr@   rA   rB   rC   rD   rE   rF   rG   rH   rI   rJ   rK   rL   rM   rN   rP   rO   rQ   rR   rS   rT   rU   rV   rW   rX   rY   rZ   r[   r@   r|   )r
   _clientchatcompletionscreater   r"   !r5   r\   r^   r?   r@   rA   rB   rC   rD   rE   rF   rG   rH   rI   rJ   rK   rL   rM   rN   rO   rP   rQ   rR   rS   rT   rU   rV   rW   rX   rY   rZ   r[   api_requestr6   r6   r7   r      s   <
	
 !#zCompletions.stream)r0   r1   )r0   r:   @r\   r]   r^   r_   r?   r`   r@   ra   rA   rb   rB   rc   rC   rd   rD   re   rE   rf   rF   rg   rG   rg   rH   rh   rI   ri   rJ   rg   rK   rj   rL   rb   rM   rg   rN   rk   rO   rl   rP   rf   rQ   rm   rR   rb   rS   rn   rT   ro   rU   rg   rV   rb   rW   rp   rX   rq   rY   rr   rZ   rs   r[   rt   r0   ru   )@r\   r]   r^   r_   r?   r`   r@   r   rA   rb   rB   rc   rC   rd   rD   re   rE   rf   rF   rg   rG   rg   rH   rh   rI   ri   rJ   rg   rK   rj   rL   rb   rM   rg   rN   rk   rO   rl   rP   rf   rQ   rm   rR   rb   rS   rn   rT   ro   rU   rg   rV   rb   rW   rp   rX   rq   rY   rr   rZ   rs   r[   rt   r0   r   	__name__
__module____qualname__r   r8   r<   r   r   r   r6   r6   r6   r7   r-   )       	 c                   @  r/   )Kr.   r0   AsyncCompletionsWithRawResponsec                 C  r2   r3   )r   r4   r6   r6   r7   r8   5  r9   z"AsyncCompletions.with_raw_response%AsyncCompletionsWithStreamingResponsec                 C  r2   r;   )r   r4   r6   r6   r7   r<   ?  r=   z(AsyncCompletions.with_streaming_responseNr>   r\   r]   r^   r_   r?   r`   r@   ra   rA   rb   rB   rc   rC   rd   rD   re   rE   rf   rF   rg   rG   rH   rh   rI   ri   rJ   rK   rj   rL   rM   rN   rk   rO   rl   rP   rQ   rm   rR   rS   rn   rT   ro   rU   rV   rW   rp   rX   rq   rY   rr   rZ   rs   r[   rt   ru   c          !        s   t  ddi|pi }d  fdd} | jd	ti d
|d|d|d|d|d|d|d|	d|
d|d|d|d|d|d|dt d||||d||||||dtjI dH t||||| dttt	t
  tddI dH S )!a  Wrapper over the `client.chat.completions.create()` method that provides richer integrations with Python specific types
        & returns a `ParsedChatCompletion` object, which is a subclass of the standard `ChatCompletion` class.

        You can pass a pydantic model to this method and it will automatically convert the model
        into a JSON schema, send it to the API and parse the response content back into the given model.

        This method will also automatically parse `function` tool calls if:
        - You use the `openai.pydantic_function_tool()` helper method
        - You mark your tool schema with `"strict": True`

        Example usage:
        ```py
        from pydantic import BaseModel
        from openai import AsyncOpenAI


        class Step(BaseModel):
            explanation: str
            output: str


        class MathResponse(BaseModel):
            steps: List[Step]
            final_answer: str


        client = AsyncOpenAI()
        completion = await client.beta.chat.completions.parse(
            model="gpt-4o-2024-08-06",
            messages=[
                {"role": "system", "content": "You are a helpful math tutor."},
                {"role": "user", "content": "solve 8x + 31 = 2"},
            ],
            response_format=MathResponse,
        )

        message = completion.choices[0].message
        if message.parsed:
            print(message.parsed.steps)
            print("answer: ", message.parsed.final_answer)
        ```
        rv   rw   rx   r$   r0   ru   c                   ry   rz   r}   r   r   r6   r7   r     r   z&AsyncCompletions.parse.<locals>.parserr   r\   r^   r?   rA   rB   rC   rD   rE   rF   rG   rH   rI   rJ   rK   rL   r@   rM   F)rN   rP   rO   r   rQ   rR   rS   rT   rU   rV   rW   Nr   r   r   )r   r   r   r   r   r   r   r	   r   r&   r   r$   r   r6   r   r7   r   H  s   O	
!	zAsyncCompletions.parser   1AsyncChatCompletionStreamManager[ResponseFormatT]c          !      C  s   t | ddi|p
i }| jjjjd&i d|d|d|dddt|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#|} t| ||d$S )'a  Wrapper over the `client.chat.completions.create(stream=True)` method that provides a more granular event API
        and automatic accumulation of each delta.

        This also supports all of the parsing utilities that `.parse()` does.

        Unlike `.create(stream=True)`, the `.stream()` method requires usage within a context manager to prevent accidental leakage of the response:

        ```py
        async with client.beta.chat.completions.stream(
            model="gpt-4o-2024-08-06",
            messages=[...],
        ) as stream:
            async for event in stream:
                if event.type == "content.delta":
                    print(event.delta, flush=True, end="")
        ```

        When the context manager is entered, an `AsyncChatCompletionStream` instance is returned which, like `.create(stream=True)` is an async iterator. The full list of events that are yielded by the iterator are outlined in [these docs](https://github.com/openai/openai-python/blob/main/helpers.md#chat-completions-events).

        When the context manager exits, the response will be closed, however the `stream` instance is still available outside
        the context manager.
        rv   r   r\   r^   r?   r   Tr@   rA   rB   rC   rD   rE   rF   rG   rH   rI   rJ   rK   rL   rM   rN   rO   rP   rQ   rR   rS   rT   rU   rV   rW   rX   rY   rZ   r[   r   Nr6   )r   r   r   r   r   r   r#   r   r6   r6   r7   r     s   ;	
 "zAsyncCompletions.stream)r0   r   )r0   r   r   )@r\   r]   r^   r_   r?   r`   r@   r   rA   rb   rB   rc   rC   rd   rD   re   rE   rf   rF   rg   rG   rg   rH   rh   rI   ri   rJ   rg   rK   rj   rL   rb   rM   rg   rN   rk   rO   rl   rP   rf   rQ   rm   rR   rb   rS   rn   rT   ro   rU   rg   rV   rb   rW   rp   rX   rq   rY   rr   rZ   rs   r[   rt   r0   r   r   r6   r6   r6   r7   r.   4  r   c                   @     e Zd ZdddZdS )	r1   r   r-   r0   Nonec                 C     || _ t|j| _d S N)_completionsr   to_raw_response_wrapperr   r5   r   r6   r6   r7   __init__A     
z#CompletionsWithRawResponse.__init__Nr   r-   r0   r   r   r   r   r   r6   r6   r6   r7   r1   @      r1   c                   @  r   )	r   r   r.   r0   r   c                 C  r   r   )r   r   async_to_raw_response_wrapperr   r   r6   r6   r7   r   J  r   z(AsyncCompletionsWithRawResponse.__init__Nr   r.   r0   r   r   r6   r6   r6   r7   r   I  r   r   c                   @  r   )	r:   r   r-   r0   r   c                 C     || _ t|j| _d S r   )r   r   r   r   r6   r6   r7   r   S     
z)CompletionsWithStreamingResponse.__init__Nr   r   r6   r6   r6   r7   r:   R  r   r:   c                   @  r   )	r   r   r.   r0   r   c                 C  r   r   )r   r   r   r   r6   r6   r7   r   \  r   z.AsyncCompletionsWithStreamingResponse.__init__Nr   r   r6   r6   r6   r7   r   [  r   r   )N
__future__r   typingr   r   r   r   r   r   r	   	functoolsr
   typing_extensionsr   httpx r   _typesr   r   r   r   r   _utilsr   r   _compatr   	_resourcer   r   	_responser   r   
_streamingr   
types.chatr   _base_clientr   lib._parsingr   r   r   r   r~   r    r   types.chat_modelr!   lib.streaming.chatr"   r#   types.chat.chat_completionr$    types.chat.chat_completion_chunkr%   !types.chat.parsed_chat_completionr&   #types.chat.chat_completion_modalityr'   %types.chat.chat_completion_tool_paramr(   &types.chat.chat_completion_audio_paramr)   (types.chat.chat_completion_message_paramr*   /types.chat.chat_completion_stream_options_paramr+   3types.chat.chat_completion_tool_choice_option_paramr,   __all__r-   r.   r1   r   r:   r   r6   r6   r6   r7   <module>   sJ   $    			