
    AHj&4                    <   U d Z ddlmZ g d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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 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( e	rddlm)Z) eeeeedZ*de+d<   ddZ,	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 ddZ-e(	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 d d       Z.	 	 d!	 	 	 	 	 	 	 	 	 	 	 d"dZ/e(	 	 d!	 	 	 	 	 	 	 	 	 	 	 d"d       Z0e(	 	 d!	 	 	 	 	 	 	 	 	 	 	 d#d       Z1e(	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 d$d       Z2y)%z
RAG Ingest API for LiteLLM.

Provides an all-in-one API for document ingestion:
Upload -> (OCR) -> Chunk -> Embed -> Vector Store
    )annotations)ingestaingestqueryaqueryN)partial)	TYPE_CHECKINGAny	CoroutineDictListOptionalTupleTypeUnion)BaseRAGIngestion)BedrockRAGIngestion)GeminiRAGIngestion)OpenAIRAGIngestion)S3VectorsRAGIngestion)VertexAIRAGIngestion)RAGQuery)RAGIngestOptionsRAGIngestResponse)ModelResponse)client)Router)openaibedrockgemini
s3_vectors	vertex_aiz!Dict[str, Type[BaseRAGIngestion]]INGESTION_REGISTRYc                    t         j                  |       }|4dj                  t         j                               }t	        d|  d|       |S )z
    Get the ingestion class for a given provider.

    Args:
        provider: The vector store provider name (e.g., 'openai')

    Returns:
        The ingestion class for the provider

    Raises:
        ValueError: If provider is not supported
    z, z
Provider 'z;' is not supported for RAG ingestion. Supported providers: )r#   getjoinkeys
ValueError)provideringestion_class	supporteds      H/root/tools/cai/cai_env/lib/python3.12/site-packages/litellm/rag/main.pyget_ingestion_classr-   :   s]     ),,X6OII05578	
 #$$-;0
 	
     c                   K   | j                  d      xs i }|j                  dd      }t        |      } || |      }|j                  |||       d{   S 7 w)a  
    Execute the RAG ingest pipeline using provider-specific implementation.

    Args:
        ingest_options: Configuration for the ingest pipeline
        file_data: Tuple of (filename, content_bytes, content_type)
        file_url: URL to fetch file from
        file_id: Existing file ID to use
        router: Optional LiteLLM router for load balancing

    Returns:
        RAGIngestResponse with status and IDs
    vector_storecustom_llm_providerr   )ingest_optionsrouter)	file_datafile_urlfile_idN)r%   r-   r   )	r2   r4   r5   r6   r3   vector_store_configr)   r*   	ingestions	            r,   _execute_ingest_pipeliner9   Q   s|     * ),,^<B"&&'<hGH *(3O  %I !! "    s   AAAAc           
       K   t               }	 t        j                         }d|d<   t        t        f| |||||d|}	t        j                         }
t        |
j                  |	      }|j                  d|       d{   }t        j                  |      r| d{   }|S |}|S 7 )7 # t        $ r>}t        j                  d| j                  di       j                  d      |||      d}~ww xY ww)a  
    Async: Ingest a document into a vector store.

    Args:
        ingest_options: Configuration for the ingest pipeline
        file_data: Tuple of (filename, content_bytes, content_type)
        file: Dict with {filename, content (base64), content_type} - for JSON API
        file_url: URL to fetch file from
        file_id: Existing file ID to use

    Example:
        ```python
        response = await litellm.aingest(
            ingest_options={
                "vector_store": {
                    "custom_llm_provider": "openai",
                    "litellm_credential_name": "my-openai-creds",  # optional
                }
            },
            file_url="https://example.com/doc.pdf",
        )
        ```
    Tr   )r2   r4   filer5   r6   timeoutNr0   r1   modelr1   original_exceptioncompletion_kwargsextra_kwargs)localsasyncioget_event_loopr   r   contextvarscopy_contextrunrun_in_executoriscoroutine	Exceptionlitellmexception_typer%   )r2   r4   r;   r5   r6   r<   kwargs
local_varsloopfuncctxfunc_with_contextinit_responseresponsees                  r,   r   r   }   s    B J"
%%' y	
)	
 	
 &&(#CGGT2"2249JKK}-**H  %H L +
  	
$$ . 2 2>2 F J J%!  !(
 	
	
sY   C5A/B+ =B'>B+ B)B+ "C5#B+ &C5'B+ )B+ +	C249C--C22C5c           
     X  K   |j                  dd      }t        j                  |      }|st        d      t	        j
                  j                  d|d   ||j                  dd      |j                  dd      d	| d{   }d}	|j                  d
g       }
|rr|j                  d      rat        j                  |      }|rJt	        j                  |d   |||j                  dd             d{   }	t        j                  ||	      }
t        j                  |
      }|dd |gz   |d   gz   }| |j                  d| ||d| d{   }n!t	        j                  d| ||d| d{   }|s(t        |t              rt        j                  |||	      }|S 7  7 7 W7 7w)z)
    Execute the RAG query pipeline.
    r3   Nz(No query found in messages for RAG queryvector_store_idtop_k
   r1   r   )rW   r   max_num_resultsr1   dataenabledr>   top_n   )r>   r   	documentsr]   )r>   messagesstream)rT   search_resultsrerank_results )popr   extract_query_from_messagesr(   rK   vector_storesasearchr%   extract_documents_from_searcharerankget_top_chunks_from_rerankbuild_context_messageacompletion
isinstancer   add_search_results_to_response)r>   ra   retrieval_configrerankrb   rM   r3   
query_textsearch_responsererank_responsecontext_chunksr_   context_messagemodified_messagesrT   s                  r,   _execute_query_pipelinery      s     "(Hd!;F 55h?JCDD $1199 ():;(,,Wb9,001FQ	
  O O$((4N &**Y'::?K	$+OOWo #jj!,	% O &@@N
 44^DO "(99Xb\NJ +++ 
&
 	
 
 !,, 
&
 	
 
 j=9::**
 Ok 

sJ   A?F*F!A1F*3F$4AF*F&!F*0F(11F*$F*&F*(F*c           	       K   t               }	 t        j                         }d|d<   t        t        f| ||||d|}t        j                         }	t        |	j                  |      }
|j                  d|
       d{   }t        j                  |      r| d{   }|S |}|S 7 )7 # t        $ r.}t        j                  | |j                  d      |||      d}~ww xY ww)z&
    Async: Query a RAG pipeline.
    Tr   r>   ra   rq   rr   rb   Nr1   r=   )rB   rC   rD   r   r   rE   rF   rG   rH   rI   rJ   rK   rL   r%   )r>   ra   rq   rr   rb   rM   rN   rO   rP   rQ   rR   rS   rT   rU   s                 r,   r   r     s     J
%%'x
-
 
 &&(#CGGT2"2249JKK}-**H  %H L +
  
$$ 0 4 45J K (
 	

sY   C$A.B* <B&=B* B(B* !C$"B* %C$&B* (B* *	C!3)CC!!C$c                >   t               }	 |j                  dd      du }|rt        d| ||||d|S t        j                         j                  t        d| ||||d|      S # t        $ r.}t        j                  | |j                  d      |||      d}~ww xY w)	z
    Query a RAG pipeline.
    r   FTr{   r1   r=   Nre   )
rB   rf   ry   rC   rD   run_until_completerJ   rK   rL   r%   )	r>   ra   rq   rr   rb   rM   rN   	_is_asyncrU   s	            r,   r   r   @  s     J
JJx/47	* !!1   ))+>>' %%5!! 	 	  
$$ 0 4 45J K (
 	

s   &A% 1A% %	B.)BBc           	     $   ddl }t               }	 |j                  dd      du }	|j                  d      }
|N|L|j                  dd      }|j                  d	d
      }|j                  dd      }|j	                  |      }|||f}|	rt        | ||||
      S t        j                         j                  t        | ||||
            S # t        $ r>}t        j                  d| j                  di       j                  d      |||      d}~ww xY w)a  
    Ingest a document into a vector store.

    Args:
        ingest_options: Configuration for the ingest pipeline
        file_data: Tuple of (filename, content_bytes, content_type)
        file: Dict with {filename, content (base64), content_type} - for JSON API
        file_url: URL to fetch file from
        file_id: Existing file ID to use

    Example:
        ```python
        response = litellm.ingest(
            ingest_options={
                "vector_store": {
                    "custom_llm_provider": "openai",
                    "litellm_credential_name": "my-openai-creds",  # optional
                }
            },
            file_data=("doc.txt", b"Hello world", "text/plain"),
        )
        ```
    r   Nr   FTr3   filenamedocumentcontent content_typezapplication/octet-stream)r2   r4   r5   r6   r3   r0   r1   r=   )base64rB   rf   r%   	b64decoder9   rC   rD   r}   rJ   rK   rL   )r2   r4   r;   r5   r6   r<   rM   r   rN   r~   r3   r   content_b64r   content_bytesrU   s                   r,   r   r   n  s6   B J'
JJy%0D8	%+ZZ%9 	 1xx
J7H((9b1K88N4NOL",,[9M!=,?I+-#!  ))+>>(#1'%#!   	
$$ . 2 2>2 F J J%!  !(
 	
	
s   BC 0C 	D9D

D)r)   strreturnzType[BaseRAGIngestion])NNNN)r2   r   r4    Optional[Tuple[str, bytes, str]]r5   Optional[str]r6   r   r3   zOptional['Router']r   r   )NNNNN)r2   Dict[str, Any]r4   r   r;   Optional[Dict[str, str]]r5   r   r6   r   r<   %Optional[Union[float, httpx.Timeout]]r   r   )NF)r>   r   ra   	List[Any]rq   r   rr   Optional[Dict[str, Any]]rb   boolr   r   )r>   r   ra   r   rq   r   rr   r   rb   r   r   z8Union[ModelResponse, Coroutine[Any, Any, ModelResponse]])r2   r   r4   r   r;   r   r5   r   r6   r   r<   r   r   z@Union[RAGIngestResponse, Coroutine[Any, Any, RAGIngestResponse]])3__doc__
__future__r   __all__rC   rE   	functoolsr   typingr	   r
   r   r   r   r   r   r   r   httpxrK   $litellm.rag.ingestion.base_ingestionr   'litellm.rag.ingestion.bedrock_ingestionr   &litellm.rag.ingestion.gemini_ingestionr   &litellm.rag.ingestion.openai_ingestionr   *litellm.rag.ingestion.s3_vectors_ingestionr   )litellm.rag.ingestion.vertex_ai_ingestionr   litellm.rag.rag_queryr   litellm.types.ragr   r   litellm.types.utilsr   litellm.utilsr   r   r#   __annotations__r-   r9   r   ry   r   r   r   re   r.   r,   <module>r      s   #
2   
 
 
   A G E E L J * .  
 !" '%9 5 2 37"!!%&$&/& & 	&
 & &X  37%)"!59C
"C
/C
 #C
 	C

 C
 3C
 C
 C
T (,JJJ %J %	J
 J JZ 
 (,+
+
+
 %+
 %	+

 +
 +
 +
\ 
 (,*
*
*
 %*
 %	*

 *
 >*
 *
Z  37%)"!59J
"J
/J
 #J
 	J

 J
 3J
 FJ
 J
r.   