
    ^h]                        S SK r S SKrS SKJr  S SKrS SKJrJrJrJ	r	J
r
JrJrJrJr  S SKrS SKJrJr  S SKJr  S SK7  S SKJr  S SKJr  \" 5       rSqS	 rS
\4S jrS\S\\   4S jr S\S\\   4S jr!S\\   S\\   S\"4S jr#\RI                  S\" \5      /S/S9\RI                  S\" \5      /S/S9\RI                  S\" \5      /S/S9\	" S5      S\	" SS9\" S5      \" \5      4S\S\S\S\\   S
\S \S!\%4S" jj5       5       5       r&\RO                  S#\" \5      /S/S9\RO                  S$\" \5      /S/S9\RO                  S%\" \5      /S/S9S\" \5      4S\S\S&\S\\   S!\%4
S' jj5       5       5       r(\RO                  S(\" \5      /S/S9\RO                  S)\" \5      /S/S9\RO                  S*\" \5      /S/S9S\" \5      4S\S\S&\S\\   S!\%4
S+ jj5       5       5       r)\RU                  S(\" \5      /S/S9\RU                  S)\" \5      /S/S9\RU                  S*\" \5      /S/S9S\" \5      4S\S\S&\S\\   S!\%4
S, jj5       5       5       r+\RO                  S\" \5      /S/S9\RO                  S\" \5      /S/S9\RO                  S\" \5      /S/S9\" \5      SS4S\S\S!\%S\\   S\\   4
S- jj5       5       5       r,g).    N)Optional)		APIRouterDependsFileFormHTTPExceptionRequestResponse
UploadFilestatus)CreateFileRequestget_secret_str)verbose_proxy_logger)*)user_api_key_auth)Routerc                 :   U c  g [        U [        5      (       d  [        S5      eU  Ho  n[        U[        5      (       d  M  UR	                  5        HA  u  p#[        U[
        5      (       d  M  UR                  S5      (       d  M4  [        U5      X'   MC     Mq     U qg )Nz3invalid files config, expected a list is not a listzos.environ/)	
isinstancelist
ValueErrordictitemsstr
startswithr   files_config)configelementkeyvalues       n/home/james-whalen/.local/lib/python3.13/site-packages/litellm/proxy/openai_files_endpoints/files_endpoints.pyset_files_configr!   %   s|    ~fd##NOOgt$$%mmo
eS))e.>.>}.M.M#1%#8GL .  L    custom_llm_providerc                     U S:X  a  g [         c  [        S5      e[          H  nUR                  S5      U :X  d  M  Us  $    g )N	vertex_aiz9files_config is not set, set it on your config.yaml file.r#   )r   r   get)r#   settings     r    get_files_provider_configr(   6   sH     k)TUU;;,-1DDN   r"   file_content_bytesreturnc                      U R                  S5      nUR                  5       S   R                  5       n[        R	                  U5      nU$ ! [        R
                  [        4 a     g f = f)Nzutf-8r   )decode
splitlinesstripjsonloadsJSONDecodeErrorUnicodeDecodeError)r)   file_content
first_linejson_objects       r    get_first_json_objectr6   D   sg    	)009!,,.q1779
 jj,  "45 s   AA A('A(r5   c                 ^    U R                  S0 5      =(       d    0 nUR                  S5      nU$ )Nbodymodel)r&   )r5   r8   r9   s      r    get_model_from_json_objr:   Q   s*    ??62&,"DHHWELr"   r9   
llm_routerc                 F    U b  Uc  gUR                  5       nSnX;   a  SnU$ )z<
Returns True if the model is in the llm_router model names
FT)get_model_names)r9   r;   model_names
is_in_lists       r    is_known_modelr@   X   s5     }
*,,.KJ
r"   z/{provider}/v1/filesfiles)dependenciestagsz	/v1/filesz/files.openai)defaultrequestfastapi_responsepurposeproviderfileuser_api_key_dictc                   #    SSK JnJnJn	Jn
JnJnJn  0 n Ub  UnUR                  5       I Sh  vN nSU0nU" UU UUUUS9I Sh  vN nUR                  XR                  4nSnSn[        R                  SL a#  [        US9nU(       a  [        US	9n[        UU
S
9n[!        S%SU0UD6n[        R                  SL a6  U(       a/  Ub,  U
c  [#        SSS0S9eU
R$                  " S%SU0UD6I Sh  vN nOQ['        US9nUb  UR)                  U5        UR+                  SS5        [        R$                  " S%0 UDSU0D6I Sh  vN n[,        R.                  " UR1                  UR3                  SS5      SS95        [5        US0 5      =(       d    0 nUR3                  SS5      =(       d    SnUR3                  SS5      =(       d    SnUR3                  SS5      =(       d    SnUR6                  R)                  U	" UUUUU[5        USS5      S95        U$  GN GN GN) N! [8         Ga(  nUR;                  UUUS9I Sh  vN    [<        R>                  " SRA                  [C        U5      5      5        [<        RD                  " [F        RH                  " 5       5        [K        U["        5      (       aY  [M        [5        US[C        URN                  5      5      [5        US S!5      [5        US"S!5      [5        US#[P        RR                  5      S$9e[C        U5       n[M        [5        USU5      [5        US S!5      [5        US"S!5      [5        US#S5      S$9eSnAff = f7f)&a  
Upload a file that can be used across - Assistants API, Batch API 
This is the equivalent of POST https://api.openai.com/v1/files

Supports Identical Params as: https://platform.openai.com/docs/api-reference/files/create

Example Curl
```
curl http://localhost:4000/v1/files         -H "Authorization: Bearer sk-1234"         -F purpose="batch"         -F file="@mydata.jsonl"

```
r   )add_litellm_data_to_requestgeneral_settingsget_custom_headersr;   proxy_configproxy_logging_objversionNrH   datarF   rN   rK   rR   rP   FT)r)   )r5   )r9   r;   rJ     errorz9LLM Router not initialized. Ensure models added to proxy.)status_codedetailr9   )r#   r#   litellm_call_id successrY   r   _hidden_paramsmodel_id	cache_keyapi_baseallowed_model_regionrK   r^   r_   r`   rR   model_regionrK   original_exceptionrequest_dataz@litellm.proxy.proxy_server.create_file(): Exception occured - {}messagetypeNoneparamrW   rg   rh   rj   code )*litellm.proxy.proxy_serverrM   rN   rO   r;   rP   rQ   rR   readfilenamecontent_typelitellm'enable_loadbalancing_on_batch_endpointsr6   r:   r@   r   r   acreate_filer(   updatepopasynciocreate_taskupdate_request_statusr&   getattrheaders	Exceptionpost_call_failure_hookr   rV   formatr   debug	traceback
format_excr   ProxyExceptionrX   r   HTTP_400_BAD_REQUEST)rF   rG   rH   rI   r#   rJ   rK   rM   rN   rO   r;   rP   rQ   rR   rT   r3   	file_datarouter_modelis_router_modeljson_obj_create_file_requestresponsellm_provider_confighidden_paramsr^   r_   r`   e	error_msgs                                r    create_filer   g   s    N   Dp"* "YY[( 7# 1-/%
 
 ]]L2C2CD	 '+::dB,MH68L"0&:#  1HiH4H ;;tC(!# #!\  (44 "&: H
 #<$7# #.$++,?@ $$%:DA$11r4Hr^qrrH 	33 $):B ?	 4 	
  *:B?E2 $$Z6<"!%%k48>B	 $$Z6<"  ''"3!#!$%68NPRS		
 e )
L s4  66/ATX 7 
 	
 	
 	""NUUA	

 	""9#7#7#9:a'' 9c!((m<Q/a&1Qv/J/JK	  q6(I 9i8Q/a&1Qs3	 's~   MH HH HB&H .H/AH  HCH MH H H H M'M
;H><DM

MMz+/{provider}/v1/files/{file_id:path}/contentz /v1/files/{file_id:path}/contentz/files/{file_id:path}/contentfile_idc                 :  #    SSK JnJnJnJnJn	Jn
  0 n U" UU UUU
US9I Sh  vN nUc  Sn[        R                  " SX2S.UD6I Sh  vN n[        R                  " U	R                  UR                  SS5      S	S
95        [        US0 5      =(       d    0 nUR                  SS5      =(       d    SnUR                  SS5      =(       d    SnUR                  SS5      =(       d    SnUR                  R                  U" UUUUU
[        USS5      S95        [        USS5      nUc  [!        SU 35      e[#        UR$                  UR&                  UR                  S9$  GN; GN! [(         Ga(  nU	R+                  UUUS9I Sh  vN    [,        R.                  " SR1                  [3        U5      5      5        [,        R4                  " [6        R8                  " 5       5        [;        U[<        5      (       aY  [?        [        US[3        UR@                  5      5      [        USS5      [        USS5      [        US[B        RD                  5      S9e[3        U5       n[?        [        USU5      [        USS5      [        USS5      [        USS5      S9eSnAff = f7f)a  
Returns information about a specific file. that can be used across - Assistants API, Batch API 
This is the equivalent of GET https://api.openai.com/v1/files/{file_id}/content

Supports Identical Params as: https://platform.openai.com/docs/api-reference/files/retrieve-contents

Example Curl
```
curl http://localhost:4000/v1/files/file-abc123/content         -H "Authorization: Bearer sk-1234"

```
r   rM   rN   rO   rP   rQ   rR   rS   NrD   r#   r   rY   rZ   r[   r\   r]   r^   r_   r`   ra   rb   r   z3Invalid response - response.response is None - got )contentrW   r{   rd   zJlitellm.proxy.proxy_server.retrieve_file_content(): Exception occured - {}rg   rh   ri   rj   rW   rk   rU   rm   )#rn   rM   rN   rO   rP   rQ   rR   rr   afile_contentrw   rx   ry   r&   rz   r{   ru   r   r
   r   rW   r|   r}   r   rV   r~   r   r   r   r   r   r   r   rX   r   r   )rF   rG   r   rI   rK   rM   rN   rO   rP   rQ   rR   rT   r   r   r^   r_   r`   httpx_responser   r   s                       r    get_file_contentr     s    F  DM 1-/%
 
 H .. 
 (
=A
 

 	33 $):B ?	 4 	
  *:B?E2 $$Z6<"!%%k48>B	 $$Z6<"  ''"3!#!$%68NPRS		
 4;8ZQU3V!EhZP  "**&22"**
 	
Y

N  66/ATX 7 
 	
 	
 	""X__A	

 	""9#7#7#9:a'' 9c!((m<Q/a&1Qv/J/JK	  q6(I 9i8Q/a&1Qs3	 'sX   JE% E$E% 	E"
DE% JE% "E% %J0JFDJJJz#/{provider}/v1/files/{file_id:path}z/v1/files/{file_id:path}z/files/{file_id:path}c                   #    SSK JnJnJnJnJn	Jn
  0 n U" UU UUU
US9I Sh  vN nUc  Sn[        R                  " SX2S.UD6I Sh  vN n[        R                  " U	R                  UR                  SS5      S	S
95        [        US0 5      =(       d    0 nUR                  SS5      =(       d    SnUR                  SS5      =(       d    SnUR                  SS5      =(       d    SnUR                  R                  U" UUUUU
[        USS5      S95        U$  N N! [          Ga(  nU	R#                  UUUS9I Sh  vN    [$        R&                  " SR)                  [+        U5      5      5        [$        R,                  " [.        R0                  " 5       5        [3        U[4        5      (       aY  [7        [        US[+        UR8                  5      5      [        USS5      [        USS5      [        US[:        R<                  5      S9e[+        U5       n[7        [        USU5      [        USS5      [        USS5      [        USS5      S9eSnAff = f7f)ax  
Returns information about a specific file. that can be used across - Assistants API, Batch API 
This is the equivalent of GET https://api.openai.com/v1/files/{file_id}

Supports Identical Params as: https://platform.openai.com/docs/api-reference/files/retrieve

Example Curl
```
curl http://localhost:4000/v1/files/file-abc123         -H "Authorization: Bearer sk-1234"

```
r   r   rS   NrD   r   rY   rZ   r[   r\   r]   r^   r_   r`   ra   rb   rd   Blitellm.proxy.proxy_server.retrieve_file(): Exception occured - {}rg   rh   ri   rj   rW   rk   rU   rm   )rn   rM   rN   rO   rP   rQ   rR   rr   afile_retrieverw   rx   ry   r&   rz   r{   ru   r|   r}   r   rV   r~   r   r   r   r   r   r   r   rX   r   r   rF   rG   r   rI   rK   rM   rN   rO   rP   rQ   rR   rT   r   r   r^   r_   r`   r   r   s                      r    get_filer     sc    F  DC 1-/%
 
 H // 
 (
=A
 

 	33 $):B ?	 4 	
  *:B?E2 $$Z6<"!%%k48>B	 $$Z6<"  ''"3!#!$%68NPRS		
 M

:  66/ATX 7 
 	
 	
 	""PWWA	

 	""9#7#7#9:a'' 9c!((m<Q/a&1Qv/J/JK	  q6(I 9i8Q/a&1Qs3	 'X   ID D$D 	D
CD ID D I)I=E >DIIIc                   #    SSK JnJnJnJnJn	Jn
  0 n U" UU UUU
US9I Sh  vN nUc  Sn[        R                  " SX2S.UD6I Sh  vN n[        R                  " U	R                  UR                  SS5      S	S
95        [        US0 5      =(       d    0 nUR                  SS5      =(       d    SnUR                  SS5      =(       d    SnUR                  SS5      =(       d    SnUR                  R                  U" UUUUU
[        USS5      S95        U$  N N! [          Ga(  nU	R#                  UUUS9I Sh  vN    [$        R&                  " SR)                  [+        U5      5      5        [$        R,                  " [.        R0                  " 5       5        [3        U[4        5      (       aY  [7        [        US[+        UR8                  5      5      [        USS5      [        USS5      [        US[:        R<                  5      S9e[+        U5       n[7        [        USU5      [        USS5      [        USS5      [        USS5      S9eSnAff = f7f)az  
Deletes a specified file. that can be used across - Assistants API, Batch API 
This is the equivalent of DELETE https://api.openai.com/v1/files/{file_id}

Supports Identical Params as: https://platform.openai.com/docs/api-reference/files/delete

Example Curl
```
curl http://localhost:4000/v1/files/file-abc123     -X DELETE     -H "Authorization: Bearer $OPENAI_API_KEY"

```
r   r   rS   NrD   r   rY   rZ   r[   r\   r]   r^   r_   r`   ra   rb   rd   r   rg   rh   ri   rj   rW   rk   rU   rm   )rn   rM   rN   rO   rP   rQ   rR   rr   afile_deleterw   rx   ry   r&   rz   r{   ru   r|   r}   r   rV   r~   r   r   r   r   r   r   r   rX   r   r   r   s                      r    delete_filer     sc    H  DC 1-/%
 
 H -- 
 (
=A
 

 	33 $):B ?	 4 	
  *:B?E2 $$Z6<"!%%k48>B	 $$Z6<"  ''"3!#!$%68NPRS		
 M

:  66/ATX 7 
 	
 	
 	""PWWA	

 	""9#7#7#9:a'' 9c!((m<Q/a&1Qv/J/JK	  q6(I 9i8Q/a&1Qs3	 'r   c                   #    SSK JnJnJnJnJn	Jn
  0 n U" UU UUU
US9I Sh  vN nUc  Sn[        R                  " SX4S.UD6I Sh  vN n[        R                  " U	R                  UR                  SS5      S	S
95        [        US0 5      =(       d    0 nUR                  SS5      =(       d    SnUR                  SS5      =(       d    SnUR                  SS5      =(       d    SnUR                  R                  U" UUUUU
[        USS5      S95        U$  N N! [          Ga(  nU	R#                  UUUS9I Sh  vN    [$        R&                  " SR)                  [+        U5      5      5        [$        R,                  " [.        R0                  " 5       5        [3        U[4        5      (       aY  [7        [        US[+        UR8                  5      5      [        USS5      [        USS5      [        US[:        R<                  5      S9e[+        U5       n[7        [        USU5      [        USS5      [        USS5      [        USS5      S9eSnAff = f7f)a^  
Returns information about a specific file. that can be used across - Assistants API, Batch API 
This is the equivalent of GET https://api.openai.com/v1/files/

Supports Identical Params as: https://platform.openai.com/docs/api-reference/files/list

Example Curl
```
curl http://localhost:4000/v1/files        -H "Authorization: Bearer sk-1234"

```
r   r   rS   NrD   )r#   rH   rY   rZ   r[   r\   r]   r^   r_   r`   ra   rb   rd   z?litellm.proxy.proxy_server.list_files(): Exception occured - {}rg   rh   ri   rj   rW   rk   rU   rm   )rn   rM   rN   rO   rP   rQ   rR   rr   
afile_listrw   rx   ry   r&   rz   r{   ru   r|   r}   r   rV   r~   r   r   r   r   r   r   r   rX   r   r   )rF   rG   rK   rI   rH   rM   rN   rO   rP   rQ   rR   rT   r   r   r^   r_   r`   r   r   s                      r    
list_filesr   p  sc    F  DC 1-/%
 
 H ++ 
 (
=A
 

 	33 $):B ?	 4 	
  *:B?E2 $$Z6<"!%%k48>B	 $$Z6<"  ''"3!#!$%68NPRS		
 M

:  66/ATX 7 
 	
 	
 	""MTTA	

 	""9#7#7#9:a'' 9c!((m<Q/a&1Qv/J/JK	  q6(I 9i8Q/a&1Qs3	 'r   )-rw   r   typingr   httpxfastapir   r   r   r   r   r	   r
   r   r   rr   r   r   litellm._loggingr   litellm.proxy._types$litellm.proxy.auth.user_api_key_authr   litellm.routerr   routerr   r!   r   r(   bytesr   r6   r:   boolr@   postUserAPIKeyAuthr   r&   r   r   deleter   r   rm   r"   r    <module>r      s      
 
 
  5 1 " B !	"
e 
 
 (3- (3- Xf5E $  +,-
  
 +,-
  
 +,-
   9"#H5Cy(/0A(BSSS S sm	S
 S S &SSl 1+,-
  
 &+,-
  
 #+,-
   #(/0A(Bkkk k sm	k
 &kk\ )+,-
  
 +,-
  
 +,-
   #(/0A(Baaa a sm	a
 &aaH )+,-
  
 +,-
  
 +,-
   #(/0A(Bbbb b sm	b
 &bbJ +,-
  
 +,-
  
 +,-
   )00A(B"!aaa &a sm	a
 c]aar"   