o
    {$f>O                     @  s4  d dl mZ d dl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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 ddl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( ddl)m*Z* ddl+m,Z, ddl-m.Z. ddl/m0Z0 ddl1m2Z2 ddgZ3G dd deZ4G dd deZ5dS )    )annotations)DictListUnionIterableOptional)partial)LiteralN   )	NOT_GIVENBodyQueryHeadersNotGiven)SyncAPIResourceAsyncAPIResource)Stream)completion_create_params)ResponseFormatTvalidate_input_toolsparse_chat_completiontype_to_response_format_param)	ChatModel)ChatCompletionStreamManager AsyncChatCompletionStreamManager)ChatCompletionChunk)ParsedChatCompletion)ChatCompletionToolParam)ChatCompletionMessageParam) ChatCompletionStreamOptionsParam)#ChatCompletionToolChoiceOptionParamCompletionsAsyncCompletionsc                   @     e Zd Z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d9d3d4Z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d:d7d8ZdS );r!   Nresponse_formatfrequency_penaltyfunction_call	functions
logit_biaslogprobs
max_tokensnparallel_tool_callspresence_penaltyseedservice_tierstop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%    type[ResponseFormatT] | NotGivenr&   Optional[float] | NotGivenr'   0completion_create_params.FunctionCall | NotGivenr(   6Iterable[completion_create_params.Function] | NotGivenr)   #Optional[Dict[str, int]] | NotGivenr*   Optional[bool] | NotGivenr+   Optional[int] | NotGivenr,   r-   bool | NotGivenr.   r/   r0   /Optional[Literal['auto', 'default']] | NotGivenr1   *Union[Optional[str], List[str]] | NotGivenr2   5Optional[ChatCompletionStreamOptionsParam] | NotGivenr3   r4   .ChatCompletionToolChoiceOptionParam | NotGivenr5   ,Iterable[ChatCompletionToolParam] | NotGivenr6   r7   r8   str | NotGivenr9   Headers | Noner:   Query | Noner;   Body | Noner<   'float | httpx.Timeout | None | NotGivenreturn%ParsedChatCompletion[ResponseFormatT]c                C  s   t | ddi|p
i }| jjjjdi 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|}t|||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.parser=   r?   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r%   chat_completioninput_toolsN _validate_input_tools_clientchatcompletionscreate_type_to_response_format_parse_chat_completionselfr=   r?   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   raw_completionrZ   rZ   \/var/www/html/dev/env/lib/python3.10/site-packages/openai/resources/beta/chat/completions.pyparse"   s~   J	
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t|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.content, 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.
        rU   beta.chat.completions.streamr=   r?   streamTr%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r%   rY   )r   r]   r^   r_   r`   ra   r   rd   r=   r?   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   api_requestrZ   rZ   rf   rk      s   7
	
zCompletions.stream6r=   r>   r?   r@   r%   rA   r&   rB   r'   rC   r(   rD   r)   rE   r*   rF   r+   rG   r,   rG   r-   rH   r.   rB   r/   rG   r0   rI   r1   rJ   r2   rK   r3   rB   r4   rL   r5   rM   r6   rG   r7   rB   r8   rN   r9   rO   r:   rP   r;   rQ   r<   rR   rS   rT   )6r=   r>   r?   r@   r%   rh   r&   rB   r'   rC   r(   rD   r)   rE   r*   rF   r+   rG   r,   rG   r-   rH   r.   rB   r/   rG   r0   rI   r1   rJ   r2   rK   r3   rB   r4   rL   r5   rM   r6   rG   r7   rB   r8   rN   r9   rO   r:   rP   r;   rQ   r<   rR   rS   ri   __name__
__module____qualname__r   rg   rk   rZ   rZ   rZ   rf   r!   !   f    xc                   @  r#   );r"   Nr$   r=   r>   r?   r@   r%   rA   r&   rB   r'   rC   r(   rD   r)   rE   r*   rF   r+   rG   r,   r-   rH   r.   r/   r0   rI   r1   rJ   r2   rK   r3   r4   rL   r5   rM   r6   r7   r8   rN   r9   rO   r:   rP   r;   rQ   r<   rR   rS   rT   c                  s   t | ddi|pi }| jjjjdi 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|I dH }t|||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 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)
        ```
        rU   rV   r=   r?   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   NrW   rZ   r[   rc   rZ   rZ   rf   rg      s   J	

zAsyncCompletions.parserh   1AsyncChatCompletionStreamManager[ResponseFormatT]c                C  s   t | ddi|p
i }| jjjjd!i 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|}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.content, 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.
        rU   rj   r=   r?   rk   Tr%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   rl   NrZ   )r\   r]   r^   r_   r`   ra   r   rm   rZ   rZ   rf   rk   i  s   6	
zAsyncCompletions.streamro   )6r=   r>   r?   r@   r%   rh   r&   rB   r'   rC   r(   rD   r)   rE   r*   rF   r+   rG   r,   rG   r-   rH   r.   rB   r/   rG   r0   rI   r1   rJ   r2   rK   r3   rB   r4   rL   r5   rM   r6   rG   r7   rB   r8   rN   r9   rO   r:   rP   r;   rQ   r<   rR   rS   ru   rp   rZ   rZ   rZ   rf   r"      rt   )6
__future__r   typingr   r   r   r   r   	functoolsr   typing_extensionsr	   httpx_typesr   r   r   r   r   	_resourcer   r   
_streamingr   
types.chatr   lib._parsingr   r   r\   r   rb   r   ra   types.chat_modelr   lib.streaming.chatr   r    types.chat.chat_completion_chunkr   !types.chat.parsed_chat_completionr   %types.chat.chat_completion_tool_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"   rZ   rZ   rZ   rf   <module>   s,    U