
    ygq                    6   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ZJ G d de          ZK G d  de          ZL G d! d"          ZM G d# d$          ZN G d% d&          ZO G 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            !          e Zd ZedId            ZedJd            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 dKdE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 dLdHZdS )Mr0   returnCompletionsWithRawResponsec                     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
        )r4   selfs    a/var/www/html/nourish/venv/lib/python3.11/site-packages/openai/resources/beta/chat/completions.pywith_raw_responsezCompletions.with_raw_response+   s     *$///     CompletionsWithStreamingResponsec                     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<   r7   s    r9   with_streaming_responsez#Completions.with_streaming_response5   s     0555r;   N 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messages$Iterable[ChatCompletionMessageParam]modelUnion[str, ChatModel]rA   -Optional[ChatCompletionAudioParam] | NotGivenrB    type[ResponseFormatT] | NotGivenrC   Optional[float] | NotGivenrD   0completion_create_params.FunctionCall | NotGivenrE   6Iterable[completion_create_params.Function] | NotGivenrF   #Optional[Dict[str, int]] | NotGivenrG   Optional[bool] | NotGivenrH   Optional[int] | NotGivenrI   rJ   Optional[Metadata] | NotGivenrK   3Optional[List[Literal['text', 'audio']]] | NotGivenrL   rM   bool | NotGivenrN   9Optional[ChatCompletionPredictionContentParam] | NotGivenrO   rP   $Optional[ReasoningEffort] | NotGivenrQ   rR   /Optional[Literal['auto', 'default']] | NotGivenrS   0Union[Optional[str], List[str], None] | NotGivenrT   rU   5Optional[ChatCompletionStreamOptionsParam] | NotGivenrV   rW   .ChatCompletionToolChoiceOptionParam | NotGivenrX   ,Iterable[ChatCompletionToolParam] | NotGivenrY   rZ   r[   str | NotGivenr\   4completion_create_params.WebSearchOptions | NotGivenr]   Headers | Noner^   Query | Noner_   Body | Noner`   'float | httpx.Timeout | None | NotGiven%ParsedChatCompletion[ResponseFormatT]c       "           t                     ddi|pi }dfd}#|                     dt          i d	|d
|d|d|d|d|d|d|	d|
d|d|d|d|d|d|d|d|t                    ||||d|||||||dt          j                  t          || |!|"|#          t          t          t          t                            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raw_completionr'   r3   r}   c                (    t          |           S N)rB   chat_completioninput_tools_parse_chat_completionr   rB   rX   s    r9   parserz!Completions.parse.<locals>.parser   #    ) / .!   r;   /chat/completionsra   rc   rA   rC   rD   rE   rF   rG   rH   rI   rJ   rK   rL   rM   rN   rO   rP   F)rB   rQ   rR   rS   rT   streamrU   rV   rW   rX   rY   rZ   r[   r\   r]   r^   r_   r`   post_parserbodyoptionscast_tor   r   r'   r3   r}   )_validate_input_tools_postr   _type_to_response_formatr   CompletionCreateParamsr   r
   r   r)   r   r'   $r8   ra   rc   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^   r_   r`   r   s$       `                     `         r9   parsezCompletions.parse>   s   d 	e$$$ ()F
"

	 	 	 	 	 	 	 zz   U  U  ():	 
 $]     !*    ,-B  !*    !*    *+>  !*   '(8! " '(8# $ (@'P'P $0 "#&4#.#."$0" *<?     B )?E# #H )+'%"   2?CDnUUa  1
 1
 1	
r;   Jcompletion_create_params.ResponseFormat | type[ResponseFormatT] | NotGiven,ChatCompletionStreamManager[ResponseFormatT]c       "        `   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#|d$| d%|!d&|"}#t          |#||'          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.streamra   rc   rA   r   TrB   rC   rD   rE   rF   rG   rH   rI   rJ   rK   rL   rM   rN   rO   rP   rQ   rR   rT   rS   rU   rV   rW   rX   rY   rZ   r[   r\   r]   r^   r_   r`   rB   r   )r   _clientchatcompletionscreater   r#   $r8   ra   rc   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^   r_   r`   api_requests$                                       r9   r   zCompletions.stream   s"   ~ ()G
"

 =DL)0%=
 %=
 %=
X%=
 %%=
 %	%=

 4%=
 5_EEE%=
 0/%=
 (-%=
  i%=
 "z%=
 X%=
 #8"7%=
 "z%=
 X%=
 "z%=
  a!%=
" !4 3#%=
$ "z%%=
& .-'%=
( .-)%=
* +%=
, &-%=
. %/%=
0 1%=
2 *>3%=
4 $5%=
6 $7%=
8 %9%=
: &;%=
< %=%=
> ?%=
@  21A%=
B (-C%=
D $E%=
F "zG%=
H GI%=
L ++
 
 
 	
r;   )r3   r4   )r3   r<   Fra   rb   rc   rd   rA   re   rB   rf   rC   rg   rD   rh   rE   ri   rF   rj   rG   rk   rH   rl   rI   rl   rJ   rm   rK   rn   rL   rl   rM   ro   rN   rp   rO   rg   rP   rq   rQ   rl   rR   rr   rS   rs   rT   rk   rU   rt   rV   rg   rW   ru   rX   rv   rY   rl   rZ   rg   r[   rw   r\   rx   r]   ry   r^   rz   r_   r{   r`   r|   r3   r}   )Fra   rb   rc   rd   rA   re   rB   r   rC   rg   rD   rh   rE   ri   rF   rj   rG   rk   rH   rl   rI   rl   rJ   rm   rK   rn   rL   rl   rM   ro   rN   rp   rO   rg   rP   rq   rQ   rl   rR   rr   rS   rs   rT   rk   rU   rt   rV   rg   rW   ru   rX   rv   rY   rl   rZ   rg   r[   rw   r\   rx   r]   ry   r^   rz   r_   r{   r`   r|   r3   r   	__name__
__module____qualname__r   r:   r?   r   r   r    r;   r9   r0   r0   *   sk       0 0 0 _0 6 6 6 _6 @I<E8AJSLU:C.7:C/82;JS&//8PY7@AJ)2HQAJ+4PY2;FO>G1:,5(S\ )-$("&;DMQ
 Q
 Q
 Q
 Q
 Q
p @Ifo8AJSLU:C.7:C/82;JS&//8PY7@AJ)2HQAJ+4PY2;FO>G1:,5(S\ )-$("&;DMm
 m
 m
 m
 m
 m
 m
 m
r;   c            !          e Zd ZedId            ZedJd            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 dKdE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 dLdHZdS )Mr1   r3   AsyncCompletionsWithRawResponsec                     t          |           S r6   )r   r7   s    r9   r:   z"AsyncCompletions.with_raw_responseB  s     /t444r;   %AsyncCompletionsWithStreamingResponsec                     t          |           S r>   )r   r7   s    r9   r?   z(AsyncCompletions.with_streaming_responseL  s     5T:::r;   Nr@   ra   rb   rc   rd   rA   re   rB   rf   rC   rg   rD   rh   rE   ri   rF   rj   rG   rk   rH   rl   rI   rJ   rm   rK   rn   rL   rM   ro   rN   rp   rO   rP   rq   rQ   rR   rr   rS   rs   rT   rU   rt   rV   rW   ru   rX   rv   rY   rZ   r[   rw   r\   rx   r]   ry   r^   rz   r_   r{   r`   r|   r}   c       "          K   t                     ddi|pi }dfd}#|                     dt          i d	|d
|d|d|d|d|d|d|	d|
d|d|d|d|d|d|d|d|t                    ||||d|||||||dt          j                   d{V t          || |!|"|#          t          t          t          t                            t                    d           d{V 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   r'   r3   r}   c                (    t          |           S r   r   r   s    r9   r   z&AsyncCompletions.parse.<locals>.parser  r   r;   r   ra   rc   rA   rC   rD   rE   rF   rG   rH   rI   rJ   rK   rL   rM   rN   rO   rP   F)rB   rQ   rR   rT   rS   r   rU   rV   rW   rX   rY   rZ   r[   r\   Nr   r   r   )r   r   r   r   r   r   r   r
   r   r)   r   r'   r   s$       `                     `         r9   r   zAsyncCompletions.parseU  s#     d 	e$$$ ()F
"

	 	 	 	 	 	 	 ZZ,  U  U  ():	 
 $]     !*    ,-B  !*    !*    *+>  !*   '(8! " '(8# $ (@'P'P $0" #&4#.#."$0" *<?     B )?E# # # # # # # #H )+'%"   2?CDnUUa   1
 1
 1
 1
 1
 1
 1
 1
 1	
r;   r   1AsyncChatCompletionStreamManager[ResponseFormatT]c       "        t   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#| d$|!d%|"d&|}#t          |#||'          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   ra   rc   rA   r   TrB   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_   r`   r\   r   r   )r   r   r   r   r   r   r$   r   s$                                       r9   r   zAsyncCompletions.stream  s,   | 	e$$$ ()G
"

 ;dl'3: $
 $
 $
X$
%$
 %$
 4	$

 5_EEE$
 0/$
 (-$
  i$
 "z$
 X$
 #8"7$
 "z$
 X$
 "z$
 a$
  !4 3!$
" "z#$
$ .-%$
& .-'$
( )$
* &+$
, -$
. %/$
0 *>1$
2 $3$
4 $5$
6 %7$
8 &9$
: %;$
< =$
> (-?$
@ $A$
B "zC$
D GE$
F  21G$
J 0+
 
 
 	
r;   )r3   r   )r3   r   r   )Fra   rb   rc   rd   rA   re   rB   r   rC   rg   rD   rh   rE   ri   rF   rj   rG   rk   rH   rl   rI   rl   rJ   rm   rK   rn   rL   rl   rM   ro   rN   rp   rO   rg   rP   rq   rQ   rl   rR   rr   rS   rs   rT   rk   rU   rt   rV   rg   rW   ru   rX   rv   rY   rl   rZ   rg   r[   rw   r\   rx   r]   ry   r^   rz   r_   r{   r`   r|   r3   r   r   r   r;   r9   r1   r1   A  sk       5 5 5 _5 ; ; ; _; @I<E8AJSLU:C.7:C/82;JS&//8PY7@AJ)2HQAJ+4PY2;FO>G1:,5(S\ )-$("&;DMQ
 Q
 Q
 Q
 Q
 Q
p @Ifo8AJSLU:C.7:C/82;JS&//8PY7@AJ)2HQAJ+4PY2;FO>G1:,5(S\ )-$("&;DMn
 n
 n
 n
 n
 n
 n
 n
r;   c                      e Zd ZddZdS )r4   r   r0   r3   Nonec                P    || _         t          j        |j                  | _        d S N)_completionsr   to_raw_response_wrapperr   r8   r   s     r9   __init__z#CompletionsWithRawResponse.__init__Z  s(    '%=
 



r;   Nr   r0   r3   r   r   r   r   r   r   r;   r9   r4   r4   Y  (        
 
 
 
 
 
r;   r4   c                      e Zd ZddZdS )r   r   r1   r3   r   c                P    || _         t          j        |j                  | _        d S r   )r   r   async_to_raw_response_wrapperr   r   s     r9   r   z(AsyncCompletionsWithRawResponse.__init__c  s(    '%C
 



r;   Nr   r1   r3   r   r   r   r;   r9   r   r   b  r   r;   r   c                      e Zd ZddZdS )r<   r   r0   r3   r   c                F    || _         t          |j                  | _        d S r   )r   r   r   r   s     r9   r   z)CompletionsWithStreamingResponse.__init__l  s%    '1
 



r;   Nr   r   r   r;   r9   r<   r<   k  r   r;   r<   c                      e Zd ZddZdS )r   r   r1   r3   r   c                F    || _         t          |j                  | _        d S r   )r   r   r   r   s     r9   r   z.AsyncCompletionsWithStreamingResponse.__init__u  s%    '7
 



r;   Nr   r   r   r;   r9   r   r   t  r   r;   r   )Q
__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.shared_paramsr%   r&   types.chat.chat_completionr'    types.chat.chat_completion_chunkr(   !types.chat.parsed_chat_completionr)   %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_prediction_content_paramr.   3types.chat.chat_completion_tool_choice_option_paramr/   __all__r0   r1   r4   r   r<   r   r   r;   r9   <module>r      s   # " " " " " D D D D D D D D D D D D D D D D D D       % % % % % %  ! ! ! ! ! ! @ @ @ @ @ @ @ @ @ @ @ @ @ @ = = = = = = = = ' ' ' ' ' ' ; ; ; ; ; ; ; ; Z Z Z Z Z Z Z Z ! ! ! ! ! ! 3 3 3 3 3 3 1 1 1 1 1 1            + * * * * * ` ` ` ` ` ` ` ` = = = = = = = = 9 9 9 9 9 9 D D D D D D F F F F F F M M M M M M O O O O O O S S S S S S ` ` ` ` ` ` h h h h h h g g g g g g,
-T
 T
 T
 T
 T
/ T
 T
 T
nU
 U
 U
 U
 U
' U
 U
 U
p
 
 
 
 
 
 
 

 
 
 
 
 
 
 

 
 
 
 
 
 
 

 
 
 
 
 
 
 
 
 
r;   