
    gu_                        d dl Z d dlZd dlmZ d dlZd dlmZmZmZm	Z	m
Z
mZmZmZmZ d dlZd dlmZmZ d dlmZ d dl d dlmZ d dlmZ d d	lmZ  e       Zdad
 ZdefdZde dee!   fdZ"de!dee   fdZ#dee   dee   de$fdZ%ejM                  d ee      gdg      ejM                  d ee      gdg      ejM                  d ee      gdg       e	d      d e	d       ed       ee      fdededed ee   ded!ed"e'fd#                     Z(ejS                  d$ ee      gdg      ejS                  d% ee      gdg      ejS                  d& ee      gdg      d ee      fdeded'ed ee   d"e'f
d(                     Z*ejS                  d) ee      gdg      ejS                  d* ee      gdg      ejS                  d+ ee      gdg      d ee      fdeded'ed ee   d"e'f
d,                     Z+ejY                  d) ee      gdg      ejY                  d* ee      gdg      ejY                  d+ ee      gdg      d ee      fdeded'ed ee   d"e'f
d-                     Z-ejS                  d ee      gdg      ejS                  d ee      gdg      ejS                  d ee      gdg       ee      ddfdeded"e'd ee   dee   f
d.                     Z.y)/    N)Optional)		APIRouterDependsFileFormHTTPExceptionRequestResponse
UploadFilestatus)CreateFileRequestget_secret_str)verbose_proxy_logger)*)user_api_key_auth))get_custom_llm_provider_from_request_body)Routerc                    | y t        | t              st        d      | D ]\  }t        |t              s|j	                         D ]6  \  }}t        |t
              s|j                  d      s)t        |      ||<   8 ^ | ay )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       i/var/www/openai/venv/lib/python3.12/site-packages/litellm/proxy/openai_files_endpoints/files_endpoints.pyset_files_configr"   (   st    ~fd#NOOgt$%mmo
UeS)e.>.>}.M#1%#8GCL .  L    custom_llm_providerc                 x    | dk(  ry t         t        d      t         D ]  }|j                  d      | k(  s|c S  y )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)   9   sH     k)TUU;;,-1DDN   r#   file_content_bytesreturnc                     	 | j                  d      }|j                         d   j                         }t        j	                  |      }|S # t        j
                  t        f$ r Y y w xY w)Nzutf-8r   )decode
splitlinesstripjsonloadsJSONDecodeErrorUnicodeDecodeError)r*   file_content
first_linejson_objects       r!   get_first_json_objectr7   G   sg    	)009!,,.q1779
 jj,  "45 s   AA A'&A'r6   c                 T    | j                  di       xs i }|j                  d      }|S )Nbodymodel)r'   )r6   r9   r:   s      r!   get_model_from_json_objr;   T   s*    ??62&,"DHHWELr#   r:   
llm_routerc                 @    | |y|j                         }d}| |v rd}|S )zD
    Returns True if the model is in the llm_router model names
    FT)get_model_names)r:   r<   model_names
is_in_lists       r!   is_known_modelrA   [   s7     }
*,,.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                   K   ddl m}m}m}	m}
m}m}m} i }	 |j                          d{   }|xs t        |        d{   xs d}d|i} ||| ||||       d{   }|j                  ||j                  f}d}d}t        j                  d	u r't        |
      }|rt        |      }t!        ||
      }t#        d'd|i|}t        j                  d	u r2|r0|.|
t%        dddi       |
j&                  d'd|i| d{   }nRt)        |      }||j+                  |       |j-                  dd       t        j&                  d'i |d|i d{   }t/        j0                  |j3                  |j5                  dd      d             t7        |di       xs i }|j5                  dd      xs d}|j5                  dd      xs d}|j5                  dd      xs d}|j8                  j+                   |	|||||t7        |dd                   |S 7 7 7 7 7 # t:        $ r)}|j=                  |||       d{  7   t?        j@                  d jC                  tE        |                   t?        jF                  tI        jJ                                tM        |t$              r\tO        t7        |d!tE        |jP                              t7        |d"d#      t7        |d$d#      t7        |d%tR        jT                        &      tE        |       }tO        t7        |d!|      t7        |d"d#      t7        |d$d#      t7        |d%d      &      d}~ww xY ww)(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versionNrG   rE   rI   datarG   rO   rL   rS   rQ   FT)r*   )r6   )r:   r<   rK     errorz9LLM Router not initialized. Ensure models added to proxy.)status_codedetailr:   )r$   r$   litellm_call_id successr[   r   _hidden_paramsmodel_id	cache_keyapi_baseallowed_model_regionrL   r`   ra   rb   rS   model_regionrL   original_exceptionrequest_dataz@litellm.proxy.proxy_server.create_file(): Exception occured - {}messagetypeNoneparamrY   ri   rj   rl   code )+litellm.proxy.proxy_serverrN   rO   rP   r<   rQ   rR   rS   readr   filenamecontent_typelitellm'enable_loadbalancing_on_batch_endpointsr7   r;   rA   r   r   acreate_filer)   updatepopasynciocreate_taskupdate_request_statusr'   getattrheaders	Exceptionpost_call_failure_hookr   rX   formatr   debug	traceback
format_excr   ProxyExceptionrZ   r   HTTP_400_BAD_REQUEST)rG   rH   rI   rJ   r$   rK   rL   rN   rO   rP   r<   rQ   rR   rS   rV   r4   	file_datarouter_modelis_router_modeljson_obj_create_file_requestresponsellm_provider_confighidden_paramsr`   ra   rb   e	error_msgs                                r!   create_filer   j   s    N   Ds "YY[( >wOO 	 7# 1-/%
 
 ]]L$2C2CD	 '+::dB,MH68L"0&:#  1HiH4H ;;tC(!# #!\  5Z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		
 o ) P
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H* !H""B%H* H%AH* H(C H* M H* H* "H* %H* (H* *
M4MI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                 Z  K   ddl m}m}m}m}m}	m}
 i }	  ||| |||
|       d{   }|xs t        |        d{   xs d}t        j                  d||d| d{   }t        j                  |	j                  |j                  dd	      d
             t        |di       xs i }|j                  dd      xs d	}|j                  dd      xs d	}|j                  dd      xs d	}|j                  j!                   ||||||
t        |dd	                   t        |dd      }|t#        d|       t%        |j&                  |j(                  |j                        S 7 E7 07 # t*        $ r)}|	j-                  |||       d{  7   t/        j0                  dj3                  t5        |                   t/        j6                  t9        j:                                t=        |t>              r\tA        t        |dt5        |jB                              t        |dd      t        |dd      t        |dtD        jF                              t5        |       }tA        t        |d|      t        |dd      t        |dd      t        |dd            d}~ww xY ww)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   rN   rO   rP   rQ   rR   rS   rU   NrT   rE   r$   r   r[   r\   r]   r^   r_   r`   ra   rb   rc   rd   r   z3Invalid response - response.response is None - got )contentrY   r}   rf   zJlitellm.proxy.proxy_server.retrieve_file_content(): Exception occured - {}ri   rj   rk   rl   rY   rm   rW   ro   )$rp   rN   rO   rP   rQ   rR   rS   r   rt   afile_contentry   rz   r{   r'   r|   r}   rw   r   r
   r   rY   r~   r   r   rX   r   r   r   r   r   r   r   r   rZ   r   r   )rG   rH   r   rJ   rL   rN   rO   rP   rQ   rR   rS   rV   r$   r   r   r`   ra   rb   httpx_responser   r   s                        r!   get_file_contentr     s    F  DP 1-/%
 
  >wOO 	
 !.. 
 3W
HL
 

 	33 $):B ?	 4 	
  *:B?E2 $$Z6<"!%%k48>B	 $$Z6<"  ''"3!#!$%68NPRS		
 4;8ZQU3V!EhZP  "**&22"**
 	
_
 P
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	 'si   J+E5 E,E5 E/ #E5 #E2$DE5 +J+,E5 /E5 2E5 5
J(?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                   K   ddl m}m}m}m}m}	m}
 i }	 |xs t        |        d{   xs d} ||| |||
|       d{   }t        j                  d||d| d{   }t        j                  |	j                  |j                  dd	      d
             t        |di       xs i }|j                  dd      xs d	}|j                  dd      xs d	}|j                  dd      xs d	}|j                  j!                   ||||||
t        |dd	                   |S 7 7 7 # t"        $ r)}|	j%                  |||       d{  7   t'        j(                  dj+                  t-        |                   t'        j.                  t1        j2                                t5        |t6              r\t9        t        |dt-        |j:                              t        |dd      t        |dd      t        |dt<        j>                              t-        |       }t9        t        |d|      t        |dd      t        |dd      t        |dd            d}~ww xY ww)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}

    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   rT   NrE   rU   r   r[   r\   r]   r^   r_   r`   ra   rb   rc   rd   rf   Blitellm.proxy.proxy_server.retrieve_file(): Exception occured - {}ri   rj   rk   rl   rY   rm   rW   ro   ) rp   rN   rO   rP   rQ   rR   rS   r   rt   afile_retrievery   rz   r{   r'   r|   r}   rw   r~   r   r   rX   r   r   r   r   r   r   r   r   rZ   r   r   rG   rH   r   rJ   rL   rN   rO   rP   rQ   rR   rS   rV   r$   r   r   r`   ra   rb   r   r   s                       r!   get_filer     s    F  DD >wOO 	 1-/%
 
 !// 
 3W
HL
 

 	33 $):B ?	 4 	
  *:B?E2 $$Z6<"!%%k48>B	 $$Z6<"  ''"3!#!$%68NPRS		
 O P

:  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	 'j   I!D+ D%D+ D'D+ #D)$C D+ $I!%D+ 'D+ )D+ +
I5IEDIII!c                   K   ddl m}m}m}m}m}	m}
 i }	 |xs t        |        d{   xs d} ||| |||
|       d{   }t        j                  d||d| d{   }t        j                  |	j                  |j                  dd	      d
             t        |di       xs i }|j                  dd      xs d	}|j                  dd      xs d	}|j                  dd      xs d	}|j                  j!                   ||||||
t        |dd	                   |S 7 7 7 # t"        $ r)}|	j%                  |||       d{  7   t'        j(                  dj+                  t-        |                   t'        j.                  t1        j2                                t5        |t6              r\t9        t        |dt-        |j:                              t        |dd      t        |dd      t        |dt<        j>                              t-        |       }t9        t        |d|      t        |dd      t        |dd      t        |dd            d}~ww xY ww)a  
    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   rT   NrE   rU   r   r[   r\   r]   r^   r_   r`   ra   rb   rc   rd   rf   r   ri   rj   rk   rl   rY   rm   rW   ro   ) rp   rN   rO   rP   rQ   rR   rS   r   rt   afile_deletery   rz   r{   r'   r|   r}   rw   r~   r   r   rX   r   r   r   r   r   r   r   r   rZ   r   r   r   s                       r!   delete_filer     s    H  DE >wOO 	 1-/%
 
 !-- 
 3W
HL
 

 	33 $):B ?	 4 	
  *:B?E2 $$Z6<"!%%k48>B	 $$Z6<"  ''"3!#!$%68NPRS		
 Q P

:  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                   K   ddl m}m}m}m}m}	m}
 i }	 |xs t        |        d{   xs d} ||| |||
|       d{   }t        j                  d||d| d{   }t        j                  |	j                  |j                  dd	      d
             t        |di       xs i }|j                  dd      xs d	}|j                  dd      xs d	}|j                  dd      xs d	}|j                  j!                   ||||||
t        |dd	                   |S 7 7 7 # t"        $ r)}|	j%                  |||       d{  7   t'        j(                  dj+                  t-        |                   t'        j.                  t1        j2                                t5        |t6              r\t9        t        |dt-        |j:                              t        |dd      t        |dd      t        |dt<        j>                              t-        |       }t9        t        |d|      t        |dd      t        |dd      t        |dd            d}~ww xY ww)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   rT   NrE   rU   )r$   rI   r[   r\   r]   r^   r_   r`   ra   rb   rc   rd   rf   z?litellm.proxy.proxy_server.list_files(): Exception occured - {}ri   rj   rk   rl   rY   rm   rW   ro   ) rp   rN   rO   rP   rQ   rR   rS   r   rt   
afile_listry   rz   r{   r'   r|   r}   rw   r~   r   r   rX   r   r   r   r   r   r   r   r   rZ   r   r   )rG   rH   rL   rJ   rI   rN   rO   rP   rQ   rR   rS   rV   r$   r   r   r`   ra   rb   r   r   s                       r!   
list_filesr   |  s    F  DE >wOO 	 1-/%
 
 !++ 
 3W
HL
 

 	33 $):B ?	 4 	
  *:B?E2 $$Z6<"!%%k48>B	 $$Z6<"  ''"3!#!$%68NPRS		
 Q P

:  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   )/ry   r   typingr   httpxfastapir   r   r   r   r   r	   r
   r   r   rt   r   r   litellm._loggingr   litellm.proxy._types$litellm.proxy.auth.user_api_key_authr   0litellm.proxy.common_utils.openai_endpoint_utilsr   litellm.routerr   routerr   r"   r   r)   bytesr   r7   r;   boolrA   postUserAPIKeyAuthr   r'   r   r   deleter   r   ro   r#   r!   <module>r      s      
 
 
  5 1 " B "	"
e 
 
 (3- (3- Xf5E $  +,-
  
 +,-
  
 +,-
   9"#H5Cy(/0A(BVVV V sm	V
 V V &VVr 1+,-
  
 &+,-
  
 #+,-
   #(/0A(Bnnn n sm	n
 &nnb )+,-
  
 +,-
  
 +,-
   #(/0A(Bbbb b sm	b
 &bbJ )+,-
  
 +,-
  
 +,-
   #(/0A(Bddd d sm	d
 &ddN +,-
  
 +,-
  
 +,-
   )00A(B"!ccc &c sm	c
 c]ccr#   