U
    ·ÜŸj¢q  ã                   @  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l5m6Z6m7Z7 ddl8m9Z9 ddl:m;Z; ddl<m=Z= ddl>m?Z? ddl@mAZA ddlBmCZC ddlDmEZE ddlFmGZG ddlHmIZI ddgZJG dd„ deƒZKG d d„ deƒZLG d!d"„ d"ƒZMG d#d$„ d$ƒZNG d%d&„ d&ƒZOG d'd(„ d(ƒZPd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)ÚMetadataÚReasoningEffort)ÚChatCompletion)ÚChatCompletionChunk)ÚParsedChatCompletion)ÚChatCompletionToolParam)ÚChatCompletionAudioParam)ÚChatCompletionMessageParam)Ú ChatCompletionStreamOptionsParam)Ú$ChatCompletionPredictionContentParam)Ú#ChatCompletionToolChoiceOptionParamÚCompletionsÚAsyncCompletionsc                %   @  s\  e Zd Zeddœdd„ƒZe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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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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d d!d"d#d)d%œ#d*d+„ZdS ),r/   ÚCompletionsWithRawResponse©Úreturnc                 C  s   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© r7   úJ/tmp/pip-unpacked-wheel-2qj3ofc_/openai/resources/beta/chat/completions.pyÚwith_raw_response+   s    zCompletions.with_raw_responseÚ CompletionsWithStreamingResponsec                 C  s   t | ƒS ©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:   r5   r7   r7   r8   Úwith_streaming_response5   s    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Ú
predictionÚpresence_penaltyÚreasoning_effortÚseedÚservice_tierÚstopÚstoreÚstream_optionsÚtemperatureÚtool_choiceÚtoolsÚtop_logprobsÚtop_pÚuserÚweb_search_optionsÚextra_headersÚextra_queryÚ
extra_bodyÚtimeoutú$Iterable[ChatCompletionMessageParam]úUnion[str, ChatModel]ú-Optional[ChatCompletionAudioParam] | NotGivenú type[ResponseFormatT] | NotGivenúOptional[float] | NotGivenú0completion_create_params.FunctionCall | NotGivenú6Iterable[completion_create_params.Function] | NotGivenú#Optional[Dict[str, int]] | NotGivenúOptional[bool] | NotGivenúOptional[int] | NotGivenúOptional[Metadata] | NotGivenú5Optional[List[Literal[('text', 'audio')]]] | NotGivenúbool | NotGivenú9Optional[ChatCompletionPredictionContentParam] | NotGivenú$Optional[ReasoningEffort] | NotGivenú1Optional[Literal[('auto', 'default')]] | NotGivenú0Union[Optional[str], List[str], None] | NotGivenú5Optional[ChatCompletionStreamOptionsParam] | NotGivenú.ChatCompletionToolChoiceOptionParam | NotGivenú,Iterable[ChatCompletionToolParam] | NotGivenústr | NotGivenú4completion_create_params.WebSearchOptions | NotGivenúHeaders | NoneúQuery | NoneúBody | Noneú'float | httpx.Timeout | None | NotGivenú%ParsedChatCompletion[ResponseFormatT]©#ÚmessagesÚmodelr>   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\   r]   r3   c       "   $   #     sª   t ˆƒ ddi|pi –}dddœ‡ ‡fdd„}#| jdt||||||||	|
||||||||tˆ ƒ||||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.parser&   rx   ©Úraw_completionr3   c                   s   t ˆ | ˆd�S ©N)r?   Zchat_completionÚinput_tools©Ú_parse_chat_completion©r   ©r?   rU   r7   r8   Úparser—   s
    ýz!Completions.parse.<locals>.parserú/chat/completionsF)rz   r{   r>   r@   rA   rB   rC   rD   rE   rF   rG   rH   rI   rJ   rK   rL   rM   r?   rN   rO   rP   rQ   ÚstreamrR   rS   rT   rU   rV   rW   rX   rY   ©rZ   r[   r\   r]   Zpost_parser©ÚbodyÚoptionsZcast_torˆ   )Ú_validate_input_toolsÚ_postr   Ú_type_to_response_formatr   ÚCompletionCreateParamsr   r	   r   r(   r   r&   ©$r6   rz   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\   r]   r†   r7   r…   r8   Úparse>   sl    R ÿþá!Þ$û	ÐzCompletions.parseúJcompletion_create_params.ResponseFormat | type[ResponseFormatT] | NotGivenz,ChatCompletionStreamManager[ResponseFormatT]c       "   $   &   C  sz   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.
        r|   úbeta.chat.completions.streamT)#rz   r{   r>   rˆ   r?   r@   rA   rB   rC   rD   rE   rF   rG   rH   rI   rJ   rK   rL   rM   rN   rO   rQ   rP   rR   rS   rT   rU   rV   rW   rX   rY   rZ   r[   r\   r]   ©r?   r�   )r
   Ú_clientÚchatÚcompletionsÚcreater�   r"   ©$r6   rz   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\   r]   Zapi_requestr7   r7   r8   rˆ   Ñ   s`    ? ÿþ
Ü&ýzCompletions.stream©	Ú__name__Ú
__module__Ú__qualname__r   r9   r<   r   r’   rˆ   r7   r7   r7   r8   r/   *   sŽ   	ÚV Úc                %   @  s\  e Zd Zeddœdd„ƒZe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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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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d d!d"d#d)d%œ#d*d+„ZdS ),r0   ÚAsyncCompletionsWithRawResponser2   c                 C  s   t | ƒS r4   )rŸ   r5   r7   r7   r8   r9   B  s    z"AsyncCompletions.with_raw_responseÚ%AsyncCompletionsWithStreamingResponsec                 C  s   t | ƒS r;   )r    r5   r7   r7   r8   r<   L  s    z(AsyncCompletions.with_streaming_responseNr=   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   c       "   $   #   ƒ  s¶   t ˆƒ ddi|pi –}dddœ‡ ‡fdd„}#| jdt||||||||	|
||||||||tˆ ƒ||||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)
        ```
        r|   r}   r&   rx   r~   c                   s   t ˆ | ˆd�S r€   r‚   r„   r…   r7   r8   r†   ®  s
    ýz&AsyncCompletions.parse.<locals>.parserr‡   F)rz   r{   r>   r@   rA   rB   rC   rD   rE   rF   rG   rH   rI   rJ   rK   rL   rM   r?   rN   rO   rQ   rP   rˆ   rR   rS   rT   rU   rV   rW   rX   rY   Nr‰   rŠ   )r�   rŽ   r   r�   r   r�   r   r	   r   r(   r   r&   r‘   r7   r…   r8   r’   U  sl    R ÿþá!Þ$û	ÐzAsyncCompletions.parser“   z1AsyncChatCompletionStreamManager[ResponseFormatT]c       "   $   %   C  s€   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.
        r|   r”   T)#rz   r{   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   rZ   r[   r\   r]   rY   r•   )r�   r–   r—   r˜   r™   r�   r#   rš   r7   r7   r8   rˆ   è  s`    > ÿþ
Ý%ýzAsyncCompletions.streamr›   r7   r7   r7   r8   r0   A  sŽ   	ÚV Úc                   @  s   e Zd Zdddœdd„ZdS )r1   r/   ÚNone©r˜   r3   c                 C  s   || _ t |j¡| _d S ©N)Ú_completionsr   Zto_raw_response_wrapperr’   ©r6   r˜   r7   r7   r8   Ú__init__Z  s    ÿz#CompletionsWithRawResponse.__init__N©rœ   r�   rž   r¦   r7   r7   r7   r8   r1   Y  s   r1   c                   @  s   e Zd Zdddœdd„ZdS )rŸ   r0   r¡   r¢   c                 C  s   || _ t |j¡| _d S r£   )r¤   r   Zasync_to_raw_response_wrapperr’   r¥   r7   r7   r8   r¦   c  s    ÿz(AsyncCompletionsWithRawResponse.__init__Nr§   r7   r7   r7   r8   rŸ   b  s   rŸ   c                   @  s   e Zd Zdddœdd„ZdS )r:   r/   r¡   r¢   c                 C  s   || _ t|jƒ| _d S r£   )r¤   r   r’   r¥   r7   r7   r8   r¦   l  s    ÿz)CompletionsWithStreamingResponse.__init__Nr§   r7   r7   r7   r8   r:   k  s   r:   c                   @  s   e Zd Zdddœdd„ZdS )r    r0   r¡   r¢   c                 C  s   || _ t|jƒ| _d S r£   )r¤   r   r’   r¥   r7   r7   r8   r¦   u  s    ÿz.AsyncCompletionsWithStreamingResponse.__init__Nr§   r7   r7   r7   r8   r    t  s   r    )QÚ
__future__r   Útypingr   r   r   r   r   r   r	   Ú	functoolsr
   Útyping_extensionsr   ZhttpxÚ r   Ú_typesr   r   r   r   r   Ú_utilsr   r   Ú_compatr   Z	_resourcer   r   Ú	_responser   r   Z
_streamingr   Z
types.chatr   Z_base_clientr   Zlib._parsingr   r   r�   r   rƒ   r    r�   Ztypes.chat_modelr!   Zlib.streaming.chatr"   r#   Ztypes.shared_paramsr$   r%   Ztypes.chat.chat_completionr&   Z types.chat.chat_completion_chunkr'   Z!types.chat.parsed_chat_completionr(   Z%types.chat.chat_completion_tool_paramr)   Z&types.chat.chat_completion_audio_paramr*   Z(types.chat.chat_completion_message_paramr+   Z/types.chat.chat_completion_stream_options_paramr,   Z3types.chat.chat_completion_prediction_content_paramr-   Z3types.chat.chat_completion_tool_choice_option_paramr.   Ú__all__r/   r0   r1   rŸ   r:   r    r7   r7   r7   r8   Ú<module>   sJ   $    			