
    g|w                     $   d 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ddl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	lmZmZmZmZ dd
l ddlm Z   e       Z! e       Z" e       Z#	 	 	 d de$de
d   dee	e$e$f      dee	e$e$f      fdZ%	 	 	 d de$de
d   dee	e$e$f      dee	e$e$f      def
dZ&	 	 	 d de$de
d   dee	e$e$f      dee	e$e$f      deeeef   f
dZ'	 	 	 d de$de
d   dee	e$e$f      dee	e$e$f      def
dZ(	 	 	 	 d!de
d   dee$   dee	e$e$f      dee	e$e$f      fdZ)	 	 	 	 d!de
d   dee$   dee	e$e$f      dee	e$e$f      fdZ*	 	 	 d dede
d   de
d   dee	e$e$f      dee	e$e$f      defdZ+	 	 	 d dede
d   de
d   dee	e$e$f      dee	e$e$f      deeeeeef   f   fdZ,	 	 	 d de$de
d   dee	e$e$f      dee	e$e$f      def
dZ-	 	 	 d de$de
d   dee	e$e$f      dee	e$e$f      deeeeeef   f   f
dZ.y)"z_
Main File for Files API implementation

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

    N)partial)Any	CoroutineDictLiteralOptionalUnioncast)get_secret_str)AzureOpenAIFilesAPI)FileDeleted
FileObjectOpenAIFilesAPI)VertexAIFilesHandler)CreateFileRequestFileContentRequest	FileTypesHttpxBinaryResponseContent)*)supports_httpx_timeoutfile_idcustom_llm_provider)openaiazureextra_headers
extra_bodyc                 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)a
    Async: Get file contents

    LiteLLM Equivalent of GET https://api.openai.com/v1/files
    Tis_asyncN)
asyncioget_event_loopr   file_retrievecontextvarscopy_contextrunrun_in_executoriscoroutine	Exceptionr   r   r   r   kwargsloopfuncctxfunc_with_contextinit_responseresponsees               G/var/www/openai/venv/lib/python3.12/site-packages/litellm/files/main.pyafile_retriever3   %   s     %%'!z 
 
 &&(#CGGT2"2249JKK}-**H  %H L*
  Y   B1A,B 0B1B BB B1B B1B B 	B.'B))B..B1returnc                 $   	 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 }|dk(  r|j                  xs+ t        j                  xs t        j                  d	      xs d
}	|j                  xs, t        j                  xs t        j                  dd      xs d}
|j                  xs9 t        j                  xs' t        j                   xs t        j                  d      }t"        j%                  | ||	|||j&                  |
      }n[|dk(  r|j                  xs t        j                  xs t)        d      }	|j*                  xs t        j*                  xs t)        d      }|j                  xs< t        j                  xs* t        j,                  xs t)        d      xs t)        d      }|j                  di       }||j                  dd       nt)        d       t.        j%                  ||	||||j&                  |       }n]t        j0                  j3                  dj5                  |      d|t	        j6                  ddt	        j8                  dd                  t;        t<        |      S # t>        $ 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
    request_timeoutX  NF     @r   Tr   OPENAI_API_BASEhttps://api.openai.com/v1OPENAI_ORGANIZATIONOPENAI_API_KEYr   	_is_asyncapi_baseapi_keytimeoutmax_retriesorganizationr   AZURE_API_BASEAZURE_API_VERSIONAZURE_OPENAI_API_KEYAZURE_API_KEYr   azure_ad_tokenAZURE_AD_TOKENr@   rA   rB   api_versionrC   rD   r   zXLiteLLM doesn't support {} for 'file_retrieve'. Only 'openai' and 'azure' are supported.n/a  Unsupported providercreate_thread"https://github.com/BerriAI/litellmmethodurlstatus_codecontentrequestmessagemodelllm_providerr0    ) GenericLiteLLMParamsrC   get
isinstancehttpxTimeoutr   readfloatpoprA   litellmosgetenvrE   rB   
openai_keyopenai_files_instanceretrieve_filerD   r   rM   	azure_keyazure_files_instance
exceptionsBadRequestErrorformatResponseRequestr
   r   r(   r   r   r   r   r*   optional_paramsrC   read_timeoutr@   rA   rE   rB   r0   rM   r1   s                  r2   r"   r"   M   s   a.88!))VVZZ8I3-OVSV 7EMM2&':;uD"<<.3L"G GU]])KGnG_GJJz51T9	(*  (( /##/99.// /	   ,, ''992D9 	   '' /??/%%/ 99-.	  -::#!+77) ; H !G+&//g73C3Cg~VfGgH++ 7&&7!"56   '' 3??3$$3 ""893 "/2  ),,\2>J%/6/0+99#!'+77 : H $$44ryy' 0 #2!MMFjk 5   J))    K<K? ?	LL

Lc                   K   	 t        j                         }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
    Tr   N)r    r!   r   file_deleter#   r$   r%   r&   r'   r
   r   r(   r)   s               r2   afile_deleterz      s     %%'!z 
 
 &&(#CGGT2"2249JKK}-**H$HK** L*
  sM   B>A,B+ 0B'1B+ B)B+ &B>'B+ )B+ +	B;4B66B;;B>c                 $   	 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 }|dk(  r|j                  xs+ t        j                  xs t        j                  d	      xs d
}	|j                  xs, t        j                  xs t        j                  dd      xs d}
|j                  xs9 t        j                  xs' t        j                   xs t        j                  d      }t"        j%                  | ||	|||j&                  |
      }n[|dk(  r|j                  xs t        j                  xs t)        d      }	|j*                  xs t        j*                  xs t)        d      }|j                  xs< t        j                  xs* t        j,                  xs t)        d      xs t)        d      }|j                  di       }||j                  dd       nt)        d       t.        j%                  ||	||||j&                  |       }n]t        j0                  j3                  dj5                  |      d|t	        j6                  ddt	        j8                  dd                  t;        t<        |      S # t>        $ r}|d}~ww xY w)!zW
    Delete file

    LiteLLM Equivalent of DELETE https://api.openai.com/v1/files
    r8   r9   NFr:   r   Tr   r;   r<   r=   r>   r?   r   rF   rG   rH   rI   r   rJ   rK   rL   JLiteLLM doesn't support {} for 'create_batch'. Only 'openai' is supported.rN   rO   rP   rQ   rR   rS   rV   rZ   r^   ) r_   rC   r`   ra   rb   rc   r   rd   re   rf   rA   rg   rh   ri   rE   rB   rj   rk   delete_filerD   r   rM   rm   rn   ro   rp   rq   rr   rs   r
   r   r(   rt   s                  r2   ry   ry      s   ^.88!))VVZZ8I3-OVSV 7EMM2&':;uD"<<.3L"G GU]])KGnG_GJJz51T9	(*  (( /##/99.// /	   ,, ''992D9 	   '' /??/%%/ 99-.	  -88#!+77) 9 H !G+&//g73C3Cg~VfGgH++ 7&&7!"56   '' 3??3$$3 ""893 "/2  ),,\2>J%/6/0+77#!'+77 8 H $$44dkk' 0 #2!MMFjk 5   K** rw   purposec                 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%   r&   r'   r(   )r   r~   r   r   r*   r+   r,   r-   r.   r/   r0   r1   s               r2   
afile_listr   T  s     %%'!z 
 
 &&(#CGGT2"2249JKK}-**H  %H L*
  r4   c                    	 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 }| dk(  r|j                  xs+ t        j                  xs t        j                  d	      xs d
}	|j                  xs, t        j                  xs t        j                  dd      xs d}
|j                  xs9 t        j                  xs' t        j                   xs t        j                  d      }t"        j%                  |||	|||j&                  |
      }|S | dk(  r|j                  xs t        j                  xs t)        d      }	|j*                  xs t        j*                  xs t)        d      }|j                  xs< t        j                  xs* t        j,                  xs t)        d      xs t)        d      }|j                  di       }||j                  dd       nt)        d       t.        j%                  ||	||||j&                  |      }|S t        j0                  j3                  dj5                  |       d| t	        j6                  ddt	        j8                  dd                  # t:        $ r}|d}~ww xY w)!zS
    List files

    LiteLLM Equivalent of GET https://api.openai.com/v1/files
    r8   r9   NFr:   r   Tr   r;   r<   r=   r>   )r~   r@   rA   rB   rC   rD   rE   r   rF   rG   rH   rI   r   rJ   rK   )r@   rA   rB   rM   rC   rD   r~   zTLiteLLM doesn't support {} for 'file_list'. Only 'openai' and 'azure' are supported.rN   rO   rP   r   rR   rS   rV   rZ   r^   )r_   rC   r`   ra   rb   rc   r   rd   re   rf   rA   rg   rh   ri   rE   rB   rj   rk   
list_filesrD   r   rM   rm   rn   ro   rp   rq   rr   rs   r(   )r   r~   r   r   r*   ru   rC   rv   r@   rA   rE   rB   r0   rM   r1   s                  r2   r   r   |  s   `.88!))VVZZ8I3-OVSV 7EMM2&':;uD"<<.3L"G GU]])KGnG_GJJz51T9	(*  (( /##/99.// /	   ,, ''992D9 	   '' /??/%%/ 99-.	  -77#!+77) 8 Hj Y !G+&//g73C3Cg~VfGgH++ 7&&7!"56   '' 3??3$$3 ""893 "/2  ),,\2>J%/6/0+66#!'+77 7 H,  $$44nuu' 0 #2!MMBfg 5    s&   FK0 C>K0 AK0 0	L 9K;;L file)
assistantsbatchz	fine-tune)r   r   	vertex_aic                 h  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)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_fileN)
r    r!   r   create_filer#   r$   r%   r&   r'   r(   )r   r~   r   r   r   r*   r+   r,   r-   r.   r/   r0   r1   s                r2   r   r     s     %%'!%~ 
 
 &&(#CGGT2"2249JKK}-**H  %H L*
  sY   B2A-B 1B2B BB B2B B2B B 	B/(B**B//B2c                 ~   	 |j                  dd      du }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}t        | |||      }
|d	k(  r|j                  xs+ t        j                  xs t        j                  d
      xs d}|j                  xs, t        j                  xs t        j                  dd      xs d}|j                   xs9 t        j                   xs' t        j"                  xs t        j                  d      }t$        j'                  |||||j(                  ||
      }|S |dk(  r|j                  xs t        j                  xs t+        d      }|j,                  xs t        j,                  xs t+        d      }|j                   xs< t        j                   xs* t        j.                  xs t+        d      xs t+        d      }|j                  di       }||j                  dd       nt+        d       t0        j'                  ||||||j(                  |
      }|S |dk(  r|j                  xs d}|j2                  xs t        j2                  xs t+        d      }|j4                  xs t        j4                  xs t+        d      }|j6                  xs t+        d      }t8        j'                  |||||||j(                  |
      }|S t        j:                  j=                  dj?                  |      d|t        j@                  d d!t        jB                  d"d#$      %      &      # tD        $ 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
    r   FTr8   r9   Nr:   )r   r~   r   r   r   r;   r<   r=   r>   )r@   rA   rB   rC   rD   rE   create_file_datar   rF   rG   rH   rI   r   rJ   rK   )r@   rA   rB   rM   rC   rD   r   r    VERTEXAI_PROJECTVERTEXAI_LOCATIONVERTEXAI_CREDENTIALS)r@   rA   vertex_projectvertex_locationvertex_credentialsrC   rD   r   r|   rN   rO   rP   rQ   rR   rS   rV   rZ   r^   )#rf   r_   rC   r`   ra   rb   rc   r   rd   re   r   rA   rg   rh   ri   rE   rB   rj   rk   r   rD   r   rM   rm   rn   r   r   r   vertex_ai_files_instancero   rp   rq   rr   rs   r(   )r   r~   r   r   r   r*   r@   ru   rC   rv   _create_file_requestrA   rE   rB   r0   rM   vertex_ai_projectvertex_ai_locationr   r1   s                       r2   r   r     s   AJJ~u5=	.88 "))VVZZ8I3-OVSV 7EMM2&':;uD"<<.3L"G GU]])KGnG_G0'!	 
 (*  (( /##/99.// /	   ,, ''992D9 	   '' /??/%%/ 99-.	  -88#!+77)!5 9 H^ M !G+&//g73C3Cg~VfGgH++ 7&&7!"56   '' 3??3$$3 ""893 "/2  ),,\2>J%/6/0+77#!'+77!5 8 H` O !K/&//52H.. 6))6!"45   // 7**7!"56 
 "1!C!C "~&H 0;;#!0 2#5+77!5 < 	H.  $$44dkk' 0 #2!MMFjk 5    s-   F N, #C>N, "B,N, AN, ,	N<5N77N<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)r   Tafile_contentN)
r    r!   r   file_contentr#   r$   r%   r&   r'   r(   r)   s               r2   r   r     s     %%'"& 
 
 &&(#CGGT2"2249JKK}-**H  %H L*
  r4   c                 "   	 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}t        | ||      }|j                  dd      du }	|d	k(  r|j                  xs+ t        j                  xs t        j                  d
      xs d}
|j                  xs, t        j                  xs t        j                  dd      xs d}|j                   xs9 t        j                   xs' t        j"                  xs t        j                  d      }t$        j'                  |	||
|||j(                  |      }|S |dk(  r|j                  xs t        j                  xs t+        d      }
|j,                  xs t        j,                  xs t+        d      }|j                   xs< t        j                   xs* t        j.                  xs t+        d      xs t+        d      }|j                  di       }||j                  dd       nt+        d       t0        j'                  |	|
||||j(                  |      }|S t        j2                  j5                  dj7                  |      d|t	        j8                  ddt	        j:                  dd                   # t<        $ r}|d}~ww xY w)"r7   r8   r9   NFr:   )r   r   r   r   Tr   r;   r<   r=   r>   )r@   file_content_requestrA   rB   rC   rD   rE   r   rF   rG   rH   rI   r   rJ   rK   )r@   rA   rB   rM   rC   rD   r   zWLiteLLM doesn't support {} for 'file_content'. Only 'openai' and 'azure' are supported.rN   rO   rP   rQ   rR   rS   rV   rZ   r^   )r_   rC   r`   ra   rb   rc   r   rd   re   r   rf   rA   rg   rh   ri   rE   rB   rj   rk   r   rD   r   rM   rm   rn   ro   rp   rq   rr   rs   r(   )r   r   r   r   r*   ru   rC   rv   _file_content_requestr@   rA   rE   rB   r0   rM   r1   s                   r2   r   r     s.   f.88!))VVZZ8I3-OVSV 7EMM2&':;uD"<<.3L"G GU]])KGnG_G 2'!!
 JJ6$>	(*  (( /##/99.// /	   ,, ''992D9 	   '' /??/%%/ 99-.	  -99#%:!+77) : Hj Y !G+&//g73C3Cg~VfGgH++ 7&&7!"56   '' 3??3$$3 ""893 "/2  ),,\2>J%/6/0+88#!'+77%: 9 H,  $$44qxx' 0 #2!MMFjk 5    s&   FK> "C>K> !AK> >	LL		L)r   NN)r   NNN)/__doc__r    r#   rh   	functoolsr   typingr   r   r   r   r   r	   r
   rb   rg   r    litellm.llms.azure.files.handlerr   litellm.llms.openai.openair   r   r   $litellm.llms.vertex_ai.files.handlerr   litellm.types.llms.openair   r   r   r   litellm.types.routerlitellm.utilsr   rk   rn   r   strr3   r"   rz   ry   r   r   r   r   r   r   r^       r2   <module>r      s     	  G G G   " @ N N E  # 0 '( *, /1  7?.2+/	%% !23% DcN+% c3h(	%T 7?.2+/	mm !23m DcN+m c3h(	m mf 7?.2+/	%% !23% DcN+% c3h(	% sC#$%T 7?.2+/	jj !23j DcN+j c3h(	j j^ 7?!.2+/	% !23%c]% DcN+% c3h(	%R 7?!.2+/	l !23lc]l DcN+l c3h(	ld DL.2+/'
'78' !!?@' DcN+	'
 c3h(' 'Z DL.2+/N
N78N !!?@N DcN+	N
 c3h(N :yc:!5667Nf 7?.2+/	%% !23% DcN+% c3h(	%  %T 7?.2+/	rr !23r DcN+r c3h(	r %yc;U1U'VVWrr   