
    AHj                     r   d Z ddlZddlZddlZddlZ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Zed   Zed   Zed   Zed   Zed	   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 ddlm Z  ddl!m"Z" ddl#m$Z$m%Z% ddl&m'Z' ddl(m)Z) ddl*m+Z+m,Z,m-Z- ddl.m/Z/ ddl0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6 ddl7 ddl8m9Z9m:Z: ddl;m<Z<m=Z=m>Z>m?Z?  e'       Z@ e-       ZA e        ZB e/       ZC e"       ZDe=	 	 	 	 d-de4ded   dee3   dedeeeEeEf      deeeEeEf      d e6fd!       ZFe=	 	 	 	 d.de4ded   dee3   dee   deeeEeEf      deeeEeEf      d ee6e
e	e	e6f   f   fd"       ZGe=	 	 	 d/d#eEdedeeeEeEf      deeeEeEf      d e6f
d$       ZHe=	 	 	 d/d#eEdedeeeEeEf      deeeEeEf      d e,f
d%       ZIe=	 	 	 d/d#eEdedeeeEeEf      deeeEeEf      d e
e	e	e,f   f
d&       ZJe=	 	 	 	 d-d#eEd'eeE   deeeEf   deeeEeEf      deeeEeEf      d e+fd(       ZKe=	 	 	 	 d0dedeeE   deeeEeEf      deeeEeEf      fd)       ZLe=	 	 	 	 d0dedeeE   deeeEeEf      deeeEeEf      fd*       ZMe=	 	 	 d/d#eEdedeeeEeEf      deeeEeEf      d e5f
d+       ZNe=	 	 	 	 d.d#eEd'eeE   deeeeEf      deeeEeEf      deeeEeEf      d ee5e
e	e	e5f   f   fd,       ZOy)1z_
Main File for Files API implementation

https://platform.openai.com/docs/api-reference/files

    N)partial)Any	CoroutineDictLiteralOptionalUnioncast)openaiazuregemini	vertex_aibedrockhosted_vllmmanus	anthropic)r   r   r   r   r   r   r   )r   r   r   r   r   )r   r   r   r   )r   r   r   r   r   r   r   )get_secret_str)get_llm_providerLogging)get_azure_credentials)AzureOpenAIFilesAPI)BedrockFilesHandler)AsyncHTTPHandlerHTTPHandler)BaseLLMHTTPHandler)get_openai_credentials)FileDeleted
FileObjectOpenAIFilesAPI)VertexAIFilesHandler)CreateFileRequestFileContentRequestFileExpiresAfter	FileTypesHttpxBinaryResponseContentOpenAIFileObject)*)+OPENAI_COMPATIBLE_BATCH_AND_FILES_PROVIDERSLlmProviders)ProviderConfigManagerclientget_litellm_paramssupports_httpx_timeoutfilepurpose)
assistantsbatchz	fine-tunemessagesexpires_aftercustom_llm_providerextra_headers
extra_bodyreturnc                 t  K   	 t        j                         }d|d<   | |||||d|}t        t        fi |}	t	        j
                         }
t        |
j                  |	      }|j                  d|       d{   }t        j                  |      r| d{   }|S |}|S 7 )7 # t        $ r}|d}~ww xY ww)z
    Async: Files are used to upload documents that can be used with features like Assistants, Fine-tuning, and Batch API.

    LiteLLM Equivalent of POST: POST https://api.openai.com/v1/files
    Tacreate_file)r/   r0   r4   r5   r6   r7   N)
asyncioget_event_loopr   create_filecontextvarscopy_contextrunrun_in_executoriscoroutine	Exception)r/   r0   r4   r5   r6   r7   kwargsloop	call_argsfuncctxfunc_with_contextinit_responseresponsees                  J/root/tools/cai/cai_env/lib/python3.12/site-packages/litellm/files/main.pyr:   r:   O   s     %%'!%~ *#6*$
 
	 {0i0&&(#CGGT2"2249JKK}-**H  %H L*
  sY   B8A3B% 7B!8B% B#B% B8B%  B8!B% #B% %	B5.B00B55B8c                    	 |j                  dd      du }t        di |}t        di |}	t        t        t
           |j                  d            }
|
t        d      |j                  d      }|j                  xs |j                  dd	      xs d	}|Ht        |t        j                        r.t        t        t        |            du r|j                  xs d	}|}n,|&t        |t        j                        st        |      }n|d
}|t!        | ||||      }nt!        | |||      }t#        j$                  dt'        |            }|St(        j+                  ||	||xs i |j,                  |j.                  |
||t        |t0        t2        f      r|nd|
      }|S |t4        v rrt7        |j,                  |j.                  |j8                        }t:        j+                  ||j,                  |j.                  ||j<                  |j8                  |      }|S |dk(  rst?        |j,                  |j.                  |j@                        }tB        j+                  ||j,                  |j.                  |j@                  ||j<                  ||	      }|S tD        jF                  jI                  djK                  |      d|t        jL                  ddt        jN                  dd                  # tP        $ r}|d}~ww xY w)z
    Files are used to upload documents that can be used with features like Assistants, Fine-tuning, and Batch API.

    LiteLLM Equivalent of POST: POST https://api.openai.com/v1/files

    Specify either provider_list or custom_llm_provider.
    r:   FTlitellm_logging_objNzlogging_obj is requiredr,   request_timeoutX       @)r/   r0   r4   r6   r7   )r/   r0   r6   r7    modelprovider)
provider_configlitellm_paramscreate_file_dataheadersapi_baseapi_keylogging_obj	_is_asyncr,   timeoutr[   r\   organization)r^   r[   r\   r_   max_retriesra   rY   r   r[   r\   api_version)r^   r[   r\   rd   r_   rb   rY   rX   zxLiteLLM doesn't support {} for 'create_file'. Only ['openai', 'azure', 'vertex_ai', 'manus', 'anthropic'] are supported.n/a  Unsupported providerr=   "https://github.com/BerriAI/litellmmethodurlstatus_codecontentrequestmessagerU   llm_providerrK    ))popGenericLiteLLMParamsdictr
   r   LiteLLMLoggingObjget
ValueErrorr_   
isinstancehttpxTimeoutr.   strreadfloatr"   r+   get_provider_files_configr*   base_llm_http_handlerr=   r[   r\   r   r   r)   r   ra   openai_files_instancerb   r   rd   azure_files_instancelitellm
exceptionsBadRequestErrorformatResponseRequestrC   )r/   r0   r4   r5   r6   r7   rD   r^   optional_paramslitellm_params_dictr]   r,   r_   read_timeout_create_file_requestrW   rK   openai_credsazure_credsrL   s                       rM   r=   r=   |   s=   "oJJ~u5=	.88"nVn&'4I)J
 677H% "))VVZZ8I3-OVSV 7EMM2&tC1D'EF%O"<<.3L"G GU]])KGnG_G$#4++%$  $5+%	$  0II!"56
 &,88 /2!5%+(11'//'# )"6K9I+JK   9 Hz Y !$OO1(11'//,99L
 -88#%..$,,+77)66!5 9 HL ; !G+/(11'//+77K
 ,77#$--#++'33+77!52 8 	H.  $$44 S  Z  Z' 0 #2!MMDhi 5    s-   FK0 !A9K0 A7K0 AK0 0	L 9K;;L file_idc                   K   	 t        j                         }d|d<   t        t        | |||fi |}t	        j
                         }t        |j                  |      }|j                  d|       d{   }	t        j                  |	      r|	 d{   }
n|	}
t        di |
j                         S 7 ?7 "# t        $ r}|d}~ww xY ww)a
    Async: Get file contents

    LiteLLM Equivalent of GET https://api.openai.com/v1/files
    Tis_asyncNrs   )r;   r<   r   file_retriever>   r?   r@   rA   rB   r'   
model_dumprC   )r   r5   r6   r7   rD   rE   rG   rH   rI   rJ   rK   rL   s               rM   afile_retriever      s     %%'!z 
 
 &&(#CGGT2"2249JKK}-**H$H8("5"5"788 L*
  sM   CA,B4 0B01B4 B2B4 /C0B4 2B4 4	C=B??CCc                 v   	 t        d$i |}|j                  xs |j                  dd      xs d}|:t        |t        j
                        r t        |      du r|j                  xs d}|}n,|&t        |t        j
                        st        |      }n|d}|j                  dd      du }|t        v rrt        |j                  |j                  |j                        }	t        j!                  | ||	j                  |	j                  ||j"                  |	j                  	      }
n|d
k(  rrt%        |j                  |j                  |j&                        }t(        j!                  ||j                  |j                  |j&                  ||j"                  |       }
nzt+        j,                  dt/        |            }|t1        d$i |}|j                  |d<   |j                  |d<   |j                  d      }|rddlm}  |dg d|rdndt7        j6                         |j                  dt9        t;        j<                                     t9        |j                  d      xs d            }|j                  d      }t>        j!                  | |||xs i |||t        |t@        tB        f      r|nd|      }
n]tD        jF                  jI                  djK                  |      d|t	        jL                  ddt	        jN                  dd !      "      #      tQ        tR        |
      S # tT        $ r}|d}~ww xY w)%{
    Returns the contents of the specified file.

    LiteLLM Equivalent of POST: POST https://api.openai.com/v1/files
    rP   rQ   NFrR   r   Tr`   r   r^   r[   r\   r_   rb   ra   r   rc   )r^   r[   r\   rd   r_   rb   r   rS   rT   r\   r[   rO   r   r   r   r   litellm_call_ididrU   r3   stream	call_type
start_timer   function_idr,   r   rW   rX   rZ   r]   r^   r,   r_   zoLiteLLM doesn't support {} for 'file_retrieve'. Only 'openai', 'azure', 'manus', and 'anthropic' are supported.re   rf   rg   create_threadrh   ri   rl   rp   rs   )+ru   r_   rx   rz   r{   r|   r.   r~   r   rt   r)   r   r[   r\   ra   r   retrieve_filerb   r   rd   r   r+   r   r*   r-   *litellm.litellm_core_utils.litellm_loggingr   timer}   uuid_moduleuuid4r   r   r   r   r   r   r   r   r   r
   r   rC   )r   r5   r6   r7   rD   r   r_   r   r^   r   rK   r   rW   r   r]   rw   r,   rL   s                     rM   r   r   (  sC   o.88!))VVZZ8I3-OVSV 7EMM2&':;uD"<<.3L"G GU]])KGnG_GJJz51T9	"MM1(11'//,99L
 -::#%..$,,+77)66 ; H !G+/(11'//+77K
 ,99#$--#++'33+77 : H 4MM%&9:O *&8&B6&B#1@1H1H#I.2A2J2J#J/$jj)>?& #4 !#$6?"2_#'99;(.

-s;3D3D3F/G) %(

4(8(>B$?
#K  H-0>>#$3#6)/R +' "-&v=M/NO  "# ?   ((88 N  U  U+  !4"^^$' 6 %_Jn o 9   J)) s   L%L( (	L81L33L8c                   K   	 t        j                         }|j                  dd      }d|d<   t        t        | ||||fi |}t        j                         }t        |j                  |      }	|j                  d|	       d{   }
t        j                  |
      r|
 d{   }n|
}t        t        |      S 7 67 # t        $ r}|d}~ww xY ww)z^
    Async: Delete file

    LiteLLM Equivalent of DELETE https://api.openai.com/v1/files
    rU   NTr   )r;   r<   rt   r   file_deleter>   r?   r@   rA   rB   r
   r   rC   r   r5   r6   r7   rD   rE   rU   rG   rH   rI   rJ   rK   rL   s                rM   afile_deleter     s     %%'

7D)!z 
 
 &&(#CGGT2"2249JKK}-**H$HK** L*
  sM   CA?B> B:B> "B<#B> 9C:B> <B> >	CC		CCrU   c                    	 	 |t        ||      \  }}}}t        d$i |}t        d$i |}|j                  xs |j                  dd      xs d}	|j                  d      }
|	:t        |	t        j                        r t        |      du r|	j                  xs d}|}	n,|	&t        |	t        j                        st        |	      }	n|	d}	|j                  dd      du }|t        v rrt        |j                  |j                   |j"                  	      }t$        j'                  | ||j                  |j                   |	|j(                  |j"                  
      }n|dk(  rtt+        |j                  |j                   |j,                        }t.        j'                  ||j                  |j                   |j,                  |	|j(                  | |
|	      }n^t1        j2                  dt5        |            }||j                   |d<   |j                  |d<   |j                  d      }|rddlm}  |dg d|rdndt;        j:                         |j                  dt=        t?        j@                                     t=        |j                  d      xs d            }tB        j'                  | |||xs i |||
t        |
tD        tF        f      r|
nd|	      }n]tH        jJ                  jM                  djO                  |      d|t        jP                  ddt        jR                  dd !      "      #      tU        tV        |      S # t        $ r Y 4w xY w# t        $ r}|d}~ww xY w)%zW
    Delete file

    LiteLLM Equivalent of DELETE https://api.openai.com/v1/files
    NrP   rQ   r,   FrR   r   Tr`   r   r   rc   )	r^   r[   r\   rd   r_   rb   r   r,   rX   rS   rT   r\   r[   rO   r   r   r   r   r   r   r   r   zwLiteLLM doesn't support {} for 'file_delete'. Only 'openai', 'azure', 'gemini', 'manus', and 'anthropic' are supported.re   rf   rg   r   rh   ri   rl   rp   rs   ),r   rC   ru   r-   r_   rx   rz   r{   r|   r.   r~   r   rt   r)   r   r[   r\   ra   r   delete_filerb   r   rd   r   r+   r   r*   r   r   r   r}   r   r   r   r   r   r   r   r   r   r   r   r
   r   )r   rU   r5   r6   r7   rD   _r   r   r_   r,   r   r^   r   rK   r   rW   r]   rw   rL   s                       rM   r   r     s   u	 /?.0,&1
 /880:6:!))VVZZ8I3-OVSVH% 7EMM2&':;uD"<<.3L"G GU]])KGnG_GJJz51T9	"MM1(11'//,99L
 -88#%..$,,+77)66 9 H !G+/(11'//+77K
 ,77#$--#++'33+772 8 
H 4MM%&9:O *1@1H1H#I.2A2J2J#J/$jj)>?& #4 !#$4=.=#'99;(.

-s;3D3D3F/G) %(

4(8(>B$?
#K 1<<#$3#6)/R +' "-&v=M/NO  "# =   ((88 V  ]  ]+  !4"^^$' 6 %_Jn o 9   K**[  		\  s5   L> L'M >	MM 
MM 	MMMc                 f  K   	 t        j                         }d|d<   t        t        | |||fi |}t	        j
                         }t        |j                  |      }|j                  d|       d{   }	t        j                  |	      r|	 d{   }
|
S |	}
|
S 7 )7 # t        $ r}|d}~ww xY ww)zZ
    Async: List files

    LiteLLM Equivalent of GET https://api.openai.com/v1/files
    Tr   N)
r;   r<   r   	file_listr>   r?   r@   rA   rB   rC   )r5   r0   r6   r7   rD   rE   rG   rH   rI   rJ   rK   rL   s               rM   
afile_listr   Z  s     %%'!z 
 
 &&(#CGGT2"2249JKK}-**H  %H L*
  sY   B1A,B 0B1B BB B1B B1B B 	B.'B))B..B1c                 R   	 t        d#i |}|j                  xs |j                  dd      xs d}|:t        |t        j
                        r t        |       du r|j                  xs d}|}n,|&t        |t        j
                        st        |      }n|d}|j                  dd      du }t        j                  dt        |       	      }	|	t        d#i |}
|j                  |
d
<   |j                  |
d<   |j                  d      }|oddlm}  |dg d|rdndt%        j$                         |j                  dt'        t)        j*                                     t'        |j                  dd                  }|j                  d      }t,        j/                  ||	|
|xs i |||t        |t0        t2        f      r|nd|      }|S | t4        v rrt7        |j                  |j                  |j8                        }t:        j/                  |||j                  |j                  ||j<                  |j8                        }|S | dk(  rrt?        |j                  |j                  |j@                        }tB        j/                  ||j                  |j                  |j@                  ||j<                  |      }|S tD        jF                  jI                  djK                  |       d| t	        jL                  ddt	        jN                  dd       !      "      # tP        $ r}|d}~ww xY w)$zS
    List files

    LiteLLM Equivalent of GET https://api.openai.com/v1/files
    rP   rQ   NFrR   r   TrS   rT   r\   r[   rO   r   r   r   r   r   r   r   r,   )r0   rW   rX   rZ   r]   r^   r,   r_   r`   )r0   r^   r[   r\   r_   rb   ra   r   rc   )r^   r[   r\   rd   r_   rb   r0   zkLiteLLM doesn't support {} for 'file_list'. Only 'openai', 'azure', 'manus', and 'anthropic' are supported.re   rf   rg   rh   ri   rl   rp   rs   ))ru   r_   rx   rz   r{   r|   r.   r~   r   rt   r+   r   r*   r-   r\   r[   r   r   r   r}   r   r   r   
list_filesr   r   r)   r   ra   r   rb   r   rd   r   r   r   r   r   r   r   rC   )r5   r0   r6   r7   rD   r   r_   r   r^   rW   r   r]   rw   r,   rK   r   r   rL   s                     rM   r   r     sH   n.88!))VVZZ8I3-OVSV 7EMM2&':;uD"<<.3L"G GU]])KGnG_GJJz51T9	 0II!"56
 &"4">v">-<-D-D	*.=.F.F
+ **%:;K" 0 .7l[#yy{$*JJ)3{/@/@/B+C% !$FJJtR$8 9
 ZZ)F,77 /2%+'# )"6K9I+JK   8 H O $OO1(11'//,99L
 -77#%..$,,+77)66 8 HJ 9 !G+/(11'//+77K
 ,66#$--#++'33+77 7 H,  $$44 F  M  M' 0 #2!MMBfg 5    s-   GL A9L A6L 9AL 	L&L!!L&c                   K   	 t        j                         }d|d<   |j                  dd      }t        t        | ||||fi |}t        j                         }t        |j                  |      }	|j                  d|	       d{   }
t        j                  |
      r|
 d{   }|S |
}|S 7 )7 # t        $ r}|d}~ww xY ww)r   Tafile_contentrU   N)r;   r<   rt   r   file_contentr>   r?   r@   rA   rB   rC   r   s                rM   r   r     s     %%'"&

7D) 
 
 &&(#CGGT2"2249JKK}-**H  %H L*
  sY   CA?B1 B-B1 "B/#B1 (C)B1 ,C-B1 /B1 1	C:B<<CCc                    	 t        d)i |}t        d)i |}|j                  xs |j                  dd      xs d}|j                  d      }		 |t	        ||      \  }
}}
}
|Ht        |t        j                        r.t        t        t        |            du r|j                  xs d}|}n,|&t        |t        j                        st        |      }n|d}t        | ||      }|j                  dd      d	u }t!        j"                  d
t%        |            }||j&                  |d<   |j(                  |d<   |j                  d      }|ot+        d
g d|rdndt-        j,                         |j                  dt        t/        j0                                     t        |j                  d      xs d
            }t2        j5                  ||||xs i |||	t        |	t6        t8        f      r|	nd|      }|S |t:        v rrt=        |j(                  |j&                  |j>                        }t@        jC                  |||j(                  |j&                  ||jD                  |j>                        }|S |dk(  rttG        |j(                  |j&                  |jH                        }tJ        jC                  ||j(                  |j&                  |jH                  ||jD                  ||	|	      }|S |dk(  r|j(                  xs d
}|jL                  xs tN        jL                  xs tQ        d      }|jR                  xs tN        jR                  xs tQ        d      }|jT                  xs tQ        d      }tV        jC                  ||||||||jD                        }|S |dk(  r1tX        jC                  |||j(                  |||jD                        }|S tN        jZ                  j]                  d j_                  |      d!|t        j`                  d"d#t        jb                  d$d%&      '      (      # t
        $ r Y w xY w# t
        $ r}|d}~ww xY w)*r   rP   rQ   r,   NFrR   )r   r6   r7   r   TrS   rT   r\   r[   rO   r   r   r   r   )file_content_requestrW   rX   rZ   r]   r^   r,   r_   r`   )r^   r   r[   r\   r_   rb   ra   r   rc   )	r^   r[   r\   rd   r_   rb   r   r,   rX   r   VERTEXAI_PROJECTVERTEXAI_LOCATIONVERTEXAI_CREDENTIALS)r^   r   r[   vertex_credentialsvertex_projectvertex_locationr_   rb   r   )r^   r   r[   r   r_   rb   zLiteLLM doesn't support {} for 'file_content'. Supported providers are 'openai', 'azure', 'vertex_ai', 'bedrock', 'manus', 'anthropic'.re   rf   rg   r   rh   ri   rl   rp   rs   )2ru   r-   r_   rx   r   rC   rz   r{   r|   r.   r
   r}   r~   r   r#   rt   r+   r   r*   r\   r[   rw   r   r   r   r   retrieve_file_contentr   r   r)   r   ra   r   r   rb   r   rd   r   r   r   r   r   r   vertex_ai_files_instancebedrock_files_instancer   r   r   r   r   )r   rU   r5   r6   r7   rD   r   r   r_   r,   r   r   _file_content_requestr^   rW   r]   rK   r   r   r[   vertex_ai_projectvertex_ai_locationr   rL   s                           rM   r   r   ,  s   ^.880:6:!))VVZZ8I3-OVSVH%	 /?.0,&1 7EMM2&tC1D'EF%O"<<.3L"G GU]])KGnG_G 2'!!
 JJ6$>	 0II!"56
 &-<-D-D	*.=.F.F
+ **%:;K"/ 1:o#yy{$*JJ)3{/@/@/B+C% !$FJJt$4$: ;
 -BB%: /2%+'# )"6K9I+JK   C H O"MM1(11'//,99L
 -99#%:%..$,,+77)66 : HT C !G+/(11'//+77K
 ,88#$--#++'33+77%:2 9 
Hv a !K/&//52H.. 6))6!"45   // 7**7!"56 
 "1!C!C "~&H 0<<#%:!#50 2+77 = 	H@ - !I--::#%:(11 3+77 ; H*  $$44 b  i  i' 0 #2!MMFjk 5  G  		`  s_   AP; P+ !FP; 8A9P; 2A8P; +B,P; 5P; AP; +	P84P; 7P88P; ;	QQQ)Nr   NN)NNNN)r   NN)r   NNN)P__doc__r;   r>   r   uuidr   	functoolsr   typingr   r   r   r   r   r	   r
   r{   FileCreateProviderFileRetrieveProviderFileDeleteProviderFileListProviderFileContentProviderr   r   1litellm.litellm_core_utils.get_llm_provider_logicr   r   r   rw   litellm.llms.azure.common_utilsr    litellm.llms.azure.files.handlerr   "litellm.llms.bedrock.files.handlerr   &litellm.llms.custom_httpx.http_handlerr   r   *litellm.llms.custom_httpx.llm_http_handlerr    litellm.llms.openai.common_utilsr   litellm.llms.openai.openair   r   r    $litellm.llms.vertex_ai.files.handlerr!   litellm.types.llms.openair"   r#   r$   r%   r&   r'   litellm.types.routerlitellm.types.utilsr)   r*   litellm.utilsr+   r,   r-   r.   r   r   r   r   r   r}   r:   r=   r   r   r   r   r   r   r   r   rs       rM   <module>r      s        G G G  	  Q  NO BC R   " N S A @ B P I C N N E  #  +,  '( *, /1 ,.   15.6.2+/)
)CD) ,-) ,	)
 DcN+) c3h() ) )X  158<.2+/
CD ,- ""45	
 DcN+ c3h( YsC1A'ABBC D  19.2+/	%%-% DcN+% c3h(	% % %P  19.2+/	{{-{ DcN+{ c3h(	{ { {~  /7.2+/	''+' DcN+' c3h(	' sC#$' 'T   :B.2+/BBC=B 1367B DcN+	B
 c3h(B B BL ,4!.2+/	%)%c]% DcN+% c3h(	% %P ,4!.2+/	z)zc]z DcN+z c3h(	z zz  08.2+/	'',' DcN+' c3h(	'  ' 'T   EI.2+/kkC=k "%(;S(@"ABk DcN+	k
 c3h(k %yc;U1U'VVWk kr   