a
    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                   @  s   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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dd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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ddd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$Iterable[ChatCompletionMessageParam]Union[str, ChatModel] type[ResponseFormatT] | NotGivenOptional[float] | NotGiven0completion_create_params.FunctionCall | NotGiven6Iterable[completion_create_params.Function] | NotGiven#Optional[Dict[str, int]] | NotGivenOptional[bool] | NotGivenOptional[int] | NotGivenbool | NotGiven/Optional[Literal['auto', 'default']] | NotGiven*Union[Optional[str], List[str]] | NotGiven5Optional[ChatCompletionStreamOptionsParam] | NotGiven.ChatCompletionToolChoiceOptionParam | NotGiven,Iterable[ChatCompletionToolParam] | NotGivenstr | NotGivenHeaders | NoneQuery | NoneBody | None'float | httpx.Timeout | None | NotGiven%ParsedChatCompletion[ResponseFormatT]messagesmodelr$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   returnc                C  sn   t | ddi|pi }| jjjj||t|||||||	|
||||||||||||||||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.parserR   rS   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$   Zchat_completioninput_tools_validate_input_tools_clientchatcompletionscreate_type_to_response_format_parse_chat_completionselfrR   rS   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   Zraw_completion rd   _/var/www/ai-form-bot/venv/lib/python3.9/site-packages/openai/resources/beta/chat/completions.pyparse"   sL    J
zCompletions.parseJcompletion_create_params.ResponseFormat | type[ResponseFormatT] | NotGivenz,ChatCompletionStreamManager[ResponseFormatT]c                C  sj   ddi|pi }t | jjjj||dt|||||||	|
||||||||||||||||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.
        rU   beta.chat.completions.streamTrR   rS   streamr$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r$   rY   )r   r\   r]   r^   r_   r`   r   rc   rR   rS   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   Zapi_requestrd   rd   re   rj      sN    7
zCompletions.stream__name__
__module____qualname__r   rf   rj   rd   rd   rd   re   r!   !   sd   Fxc                   @  s   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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dd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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dddZdS )r"   Nr#   r<   r=   r>   r?   r@   rA   rB   rC   rD   rE   rF   rG   rH   rI   rJ   rK   rL   rM   rN   rO   rP   rQ   c                  st   t | ddi|pi }| jjjj||t|||||||	|
||||||||||||||||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   rW   NrX   rZ   rb   rd   rd   re   rf      sL    J
zAsyncCompletions.parserg   z1AsyncChatCompletionStreamManager[ResponseFormatT]c                C  sp   t | ddi|pi }| jjjj||dt|||||||	|
||||||||||||||||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.
        rU   rh   Tri   rk   )r[   r\   r]   r^   r_   r`   r   rl   rd   rd   re   rj   i  sN    6
zAsyncCompletions.streamrm   rd   rd   rd   re   r"      sd   Fx)6
__future__r   typingr   r   r   r   r   	functoolsr   typing_extensionsr	   Zhttpx_typesr   r   r   r   r   Z	_resourcer   r   Z
_streamingr   Z
types.chatr   Zlib._parsingr   r   r[   r   ra   r   r`   Ztypes.chat_modelr   Zlib.streaming.chatr   r   Z types.chat.chat_completion_chunkr   Z!types.chat.parsed_chat_completionr   Z%types.chat.chat_completion_tool_paramr   Z(types.chat.chat_completion_message_paramr   Z/types.chat.chat_completion_stream_options_paramr   Z3types.chat.chat_completion_tool_choice_option_paramr    __all__r!   r"   rd   rd   rd   re   <module>   s*    U