
    gN.                         d dl Z d dlmZmZmZmZ d dlmZmZ d dl	Z	d dl
mZ d dlmZ d dlmZ d dlmZmZ d dlmZmZmZ d d	lmZ d
dlmZ ddlmZ  G d de      Zy)    N)CallableListOptionalUnion)AsyncOpenAIOpenAI)Logging)CustomStreamWrapper)BaseLLM)AllMessageValuesOpenAITextCompletionUserMessage)LlmProvidersModelResponseTextCompletionResponse)ProviderConfigManager   )OpenAIError   )OpenAITextCompletionConfigc                   R    e Zd Z e       Zd fdZd Z	 	 	 	 	 	 	 	 ddededede	e
e   e
e   f   deded	ed
edee   dee   dedee   dee   fdZ	 	 ddededededededededee   fdZ	 	 	 	 ddededededededee   fdZ	 	 	 ddedededededededee   fdZ xZS )OpenAITextCompletionc                 "    t         |           y )N)super__init__)self	__class__s    [/var/www/openai/venv/lib/python3.12/site-packages/litellm/llms/openai/completion/handler.pyr   zOpenAITextCompletion.__init__   s        c                 "    ddi}|rd| |d<   |S )Nzcontent-typezapplication/jsonzBearer Authorization )r   api_keyheaderss      r   validate_environmentz)OpenAITextCompletion.validate_environment   s*    .
 )0	':GO$r   model_responser"   modelmessagestimeoutcustom_llm_providerlogging_objoptional_paramsprint_verboseapi_baseacompletionorganizationr#   c                    	 || j                  |      }||t        dd      t        j                  |t	        |            }|j                  ||||      }|j                  dd      }|j                  ||||
|d	
       |du rJ|j                  dd      r| j                  ||
|||||||||      S | j                  |
||||||||||      S |j                  dd      r| j                  ||
|||||||||      S | t        ||
t        j                  |||      }n|} |j                  j                   j"                  di |}|j%                         }|j'                         }|j)                  ||||
d       t+        di |S # t,        $ ra}t/        |dd      }t/        |dd       }t/        |dt1        |            }t/        |dd       }||rt/        |dd       }t        |||      d }~ww xY w)N)r"   i  zMissing model or messages)status_codemessage)r&   provider)r&   r'   r+   r#   max_retriesr   )r#   r-   complete_input_dict)inputr"   additional_argsTstreamF)r*   r-   r"   datar#   r%   r&   r(   r4   clientr/   )r-   r9   r#   r%   r"   r*   r&   r(   r4   r/   r:   r"   base_urlhttp_clientr(   r4   r/   r#   r-   r"   original_responser7   r1     r#   textresponser1   r2   r#   r!   )r$   r   r   #get_provider_text_completion_configr   !transform_text_completion_requestpoppre_callgetasync_streamingr.   	streamingr   litellmclient_sessioncompletionswith_raw_responsecreateparse
model_dump	post_callr   	Exceptiongetattrstr)r   r%   r"   r&   r'   r(   r)   r*   r+   r,   r-   r.   litellm_params	logger_fnr:   r/   r#   provider_configr9   r4   openai_clientraw_responserC   response_jsoner1   error_headers
error_texterror_responses                                r   
completionzOpenAITextCompletion.completion    s   &c	33G3D} 0!c;VWW 4WW%&9:O
 #DD! /	 E D ((=!4K  & (+/! !  d""&&x7//$/!) '! ''5# '$/%%1 0    ++XDRYjx  CJ  Xc  kp  zA  OZ  iu  ~D+  E  E $$Xu5~~ +%###1# +!!- &   >$* '!)$+$:$: '$/%1%M %+MQ}88JJQQYTXY'--/ ( 3 3 5 %%#&3#*$,% &  .>>> 		!!]C8K#Ay$7M FCF3J$Q
D9N$ '	4 H'] 		s,   B5F 8F -F BF 	G8AG33G8r9   r4   c                 ^  K   	 | t        ||t        j                  ||	|
      }n|} |j                  j                  j
                  d
i | d {   }|j                         }|j                         }|j                  ||||d       t        d
i |}t        j                  |      |j                  _        |S 7 l# t        $ ra}t        |dd      }t        |dd       }t        |dt!        |            }t        |dd       }||rt        |dd       }t#        |||	      d }~ww xY ww)Nr;   r>   r?   r1   rA   r#   rB   rC   rD   r!   )r   rL   aclient_sessionrN   rO   rP   rQ   rR   rS   r   jsondumps_hidden_paramsr@   rT   rU   rV   r   )r   r*   r-   r9   r#   r%   r"   r&   r(   r4   r/   r:   openai_aclientr[   rC   r\   response_objr]   r1   r^   r_   r`   s                         r   r.   z OpenAITextCompletion.acompletion   sP    )	~!,#% ' 7 7# +!-" "(!T!;!;!M!M!T!T "" L $))+H$//1M !!"*& (! "  2BMBL<@JJ}<UL''9%&  		!!]C8K#Ay$7M FCF3J$Q
D9N$ '	4 H'] 		s=   D-AC  B>A+C  =D->C   	D*	AD%%D**D-c           
   #     K   |
 t        ||t        j                  ||	|      }n|
}	  |j                  j                  j
                  di |}|j                         }t        ||d||j                  d	d       
      }	 |D ]  }|  y # t        $ ra}t        |dd      }t        |dd       }t        |dt        |            }t        |dd       }||rt        |dd       }t        |||      d }~ww xY w# t        $ ra}t        |dd      }t        |dd       }t        |dt        |            }t        |dd       }||rt        |dd       }t        |||      d }~ww xY ww)Nr;   r1   rA   r#   rB   rC   rD   text-completion-openaistream_optionscompletion_streamr&   r)   r*   rk   r!   )r   rL   rM   rN   rO   rP   rQ   rT   rU   rV   r   r
   rI   )r   r*   r"   r9   r#   r%   r&   r(   r-   r4   r:   r/   rZ   r[   rC   r]   r1   r^   r_   r`   streamwrapperchunks                         r   rK   zOpenAITextCompletion.streaming   s     >"!#22')M #M	M=44FFMMUPTUL#))+H ,& 8#88$4d;
	& ''  		!!]C8K#Ay$7M FCF3J$Q
D9N$ '	4 H'] 		*  		!!]C8K#Ay$7M FCF3J$Q
D9N$ '	4 H'] 		sN   %E%6B  E%?C8 
E%	C5AC00C55E%8	E"AEE""E%c           
       K   |
 t        ||	t        j                  |||      }n|
} |j                  j                  j
                  di | d {   }|j                         }t        ||d||j                  dd             }	 |2 3 d {   }| 7 F7 6 y # t        $ ra}t        |dd      }t        |dd       }t        |dt        |            }t        |d	d       }||rt        |dd       }t        |||
      d }~ww xY ww)Nr;   rj   rk   rl   r1   rA   r#   rB   rC   rD   r!   )r   rL   rc   rN   rO   rP   rQ   r
   rI   rT   rU   rV   r   )r   r*   r"   r9   r#   r%   r&   r(   r4   r-   r:   r/   rZ   r[   rC   rn   transformed_chunkr]   r1   r^   r_   r`   s                         r   rJ   z$OpenAITextCompletion.async_streaming  s(     >'!#33')M #MO]66HHOOWRVWW%%'+& 8#88$4d;
	+8 (''' X(= 		!!]C8K#Ay$7M FCF3J$Q
D9N$ '	4 H'] 		sa   AD
B4D
B BBBB D
BB D
	D&ADDD
)returnN)NNFNNNNN)NN)NNNN)NNN)__name__
__module____qualname__r   $openai_text_completion_global_configr   r$   r   rV   r   r   r   r   floatLiteLLMLoggingObjdictr   r   boolra   intr.   rK   rJ   __classcell__)r   s   @r   r   r      s   +E+G($ -1"&!&*"&#v%v v 	v
 -.5T0UUVv v !v 'v v  )v 3-v v  sm!v" $#vF '+7 7 	7
 7 &7 7 7 7 7 sm7D #'< < 	<
 < &< < < 3-<P #'0 0 	0
 0 &0 0 0 0 3-0r   r   )rd   typingr   r   r   r   openair   r   rL   *litellm.litellm_core_utils.litellm_loggingr	   rx   ,litellm.litellm_core_utils.streaming_handlerr
   litellm.llms.baser   litellm.types.llms.openair   r   litellm.types.utilsr   r   r   litellm.utilsr   common_utilsr   transformationr   r   r!   r   r   <module>r      s=     2 2 &  S L % W S S / & 6m7 mr   