
    ik.                         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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   )BaseOpenAILLM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    w/Users/manta/Documents/Projects/TheRoad-I1/.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)   r5   clientr0   )r.   r:   r$   r&   r#   r+   r'   r)   r5   r0   r;   r#   base_urlhttp_clientr)   r5   r0   r$   r.   r#   original_responser8   r2     r$   textresponser2   r3   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;   r0   r$   provider_configr:   r5   openai_clientraw_responserD   response_jsoner2   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r:   r5   c                 f  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@   r2   rB   r$   rC   rD   rE   r"   )r   r   _get_async_http_clientrO   rP   rQ   rR   rS   rT   r   jsondumps_hidden_paramsrA   rU   rV   rW   r   )r   r+   r.   r:   r$   r&   r#   r'   r)   r5   r0   r;   openai_aclientr\   rD   r]   response_objr^   r2   r_   r`   ra   s                         r   r/   z OpenAITextCompletion.acompletion   sS    )	~!,#% - D D F# +!-" "(!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=   D1AC CA+C D1C 	D.AD))D..D1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<   r2   rB   r$   rC   rD   rE   text-completion-openaistream_optionscompletion_streamr'   r*   r+   rl   r"   )r   rM   rN   rO   rP   rQ   rR   rU   rV   rW   r   r
   rJ   )r   r+   r#   r:   r$   r&   r'   r)   r.   r5   r;   r0   r[   r\   rD   r^   r2   r_   r`   ra   streamwrapperchunks                         r   rL   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<   rk   rl   rm   r2   rB   r$   rC   rD   rE   r"   )r   rM   aclient_sessionrO   rP   rQ   rR   r
   rJ   rU   rV   rW   r   )r   r+   r#   r:   r$   r&   r'   r)   r5   r.   r;   r0   r[   r\   rD   ro   transformed_chunkr^   r2   r_   r`   ra   s                         r   rK   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   rW   r   r   r   r   floatLiteLLMLoggingObjdictr   r   boolrb   intr/   rL   rK   __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-;N #'0 0 	0
 0 &0 0 0 0 3-0r   r   ) re   typingr   r   r   r   openair   r   rM   *litellm.litellm_core_utils.litellm_loggingr	   rz   ,litellm.litellm_core_utils.streaming_handlerr
   litellm.llms.baser   litellm.types.llms.openair   r   litellm.types.utilsr   r   r   litellm.utilsr   common_utilsr   r   transformationr   r   r"   r   r   <module>r      s=     2 2 &  S L % W S S / 5 6l7 lr   