o
    ưi3                     @  s  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< dBddZ,				dCdDd%d&Z-e(					dEdFd,d-Z.		.dGdHd8d9Z/e(		.dGdHd:d;Z0e(		.dGdId=d>Z1e(					dEdJd@dAZ2dS )Kz
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)openaiZbedrockgeminiZ
s3_vectorsZ	vertex_aiz!Dict[str, Type[BaseRAGIngestion]]INGESTION_REGISTRYproviderstrreturnType[BaseRAGIngestion]c                 C  s8   t | }|du rdt  }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
    Nz, z
Provider 'z;' is not supported for RAG ingestion. Supported providers: )r   getjoinkeys
ValueError)r    ingestion_class	supported r*   G/home/app/Keep/.python/lib/python3.10/site-packages/litellm/rag/main.pyget_ingestion_class:   s   
r,   ingest_optionsr   	file_data Optional[Tuple[str, bytes, str]]file_urlOptional[str]file_idrouterOptional['Router']r   c           	        sF   |  dpi }| dd}t|}|| |d}|j|||dI dH S )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   )r-   r3   )r.   r0   r2   N)r$   r,   r   )	r-   r.   r0   r2   r3   Zvector_store_configr    r(   Z	ingestionr*   r*   r+   _execute_ingest_pipelineQ   s   r7   Dict[str, Any]fileOptional[Dict[str, str]]timeout%Optional[Union[float, httpx.Timeout]]c              
     s   t  }z<t }d|d< ttf| |||||d|}	t }
t|
j|	}|d|I dH }t	|r<|I dH }|W S |}|W S  t
y] } ztjd| di d|||dd}~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   )r-   r.   r9   r0   r2   r;   Nr5   r6   modelr6   Zoriginal_exceptionZcompletion_kwargsextra_kwargs)localsasyncioget_event_loopr   r   contextvarscopy_contextrunrun_in_executoriscoroutine	Exceptionlitellmexception_typer$   )r-   r.   r9   r0   r2   r;   kwargs
local_varsloopfuncctxfunc_with_contextinit_responseresponseer*   r*   r+   r   }   sF   !

r   Fr>   messages	List[Any]retrieval_configrerankOptional[Dict[str, Any]]streamboolr   c              	     sD  | dd}t|}|stdtjjd|d ||dd|ddd	|I dH }d}	|d
g }
|r[|dr[t|}|r[tj	|d |||dddI dH }	t
||	}
t|
}|dd |g |d g }|dur|jd| ||d|I dH }ntjd| ||d|I dH }|st|trtj|||	d}|S )z)
    Execute the RAG query pipeline.
    r3   Nz(No query found in messages for RAG queryvector_store_idZtop_k
   r6   r   )r[   r   Zmax_num_resultsr6   dataenabledr>   top_n   )r>   r   	documentsr_   )r>   rT   rY   )rR   Zsearch_resultsZrerank_resultsr*   )popr   Zextract_query_from_messagesr'   rI   Zvector_storesZasearchr$   Zextract_documents_from_searchZarerankZget_top_chunks_from_rerankZbuild_context_messageZacompletion
isinstancer   Zadd_search_results_to_response)r>   rT   rV   rW   rY   rK   r3   Z
query_textZsearch_responseZrerank_responseZcontext_chunksra   Zcontext_messageZmodified_messagesrR   r*   r*   r+   _execute_query_pipeline   sf   





re   c              
     s   t  }z;t }d|d< ttf| ||||d|}t }	t|	j|}
|d|
I dH }t	|r;|I dH }|W S |}|W S  t
yX } ztj| |d|||dd}~ww )z&
    Async: Query a RAG pipeline.
    Tr   r>   rT   rV   rW   rY   Nr6   r=   )r@   rA   rB   r   r   rC   rD   rE   rF   rG   rH   rI   rJ   r$   )r>   rT   rV   rW   rY   rK   rL   rM   rN   rO   rP   rQ   rR   rS   r*   r*   r+   r     sD   


r   8Union[ModelResponse, Coroutine[Any, Any, ModelResponse]]c           	   
   K  s   t  }z+|dddu }|rtd| ||||d|W S t td| ||||d|W S  tyG } ztj| |	d|||dd}~ww )	z
    Query a RAG pipeline.
    r   FTrf   r6   r=   Nr*   )
r@   rc   re   rA   rB   run_until_completerH   rI   rJ   r$   )	r>   rT   rV   rW   rY   rK   rL   	_is_asyncrS   r*   r*   r+   r   >  sD   	
r   @Union[RAGIngestResponse, Coroutine[Any, Any, RAGIngestResponse]]c              
   K  s   ddl }t }zL|dddu }	|d}
|dur9|du r9|dd}|d	d
}|dd}||}|||f}|	rEt| ||||
dW S t t| ||||
dW S  t	yp } zt
jd| di d|||dd}~w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   filenameZdocumentcontent content_typezapplication/octet-stream)r-   r.   r0   r2   r3   r5   r6   r=   )base64r@   rc   r$   	b64decoder7   rA   rB   rh   rH   rI   rJ   )r-   r.   r9   r0   r2   r;   rK   ro   rL   ri   r3   rk   Zcontent_b64rn   Zcontent_bytesrS   r*   r*   r+   r   l  sL   !


	r   )r    r!   r"   r#   )NNNN)r-   r   r.   r/   r0   r1   r2   r1   r3   r4   r"   r   )NNNNN)r-   r8   r.   r/   r9   r:   r0   r1   r2   r1   r;   r<   r"   r   )NF)r>   r!   rT   rU   rV   r8   rW   rX   rY   rZ   r"   r   )r>   r!   rT   rU   rV   r8   rW   rX   rY   rZ   r"   rg   )r-   r8   r.   r/   r9   r:   r0   r1   r2   r1   r;   r<   r"   rj   )3__doc__
__future__r   __all__rA   rC   	functoolsr   typingr   r	   r
   r   r   r   r   r   r   ZhttpxrI   Z$litellm.rag.ingestion.base_ingestionr   Z'litellm.rag.ingestion.bedrock_ingestionr   Z&litellm.rag.ingestion.gemini_ingestionr   Z&litellm.rag.ingestion.openai_ingestionr   Z*litellm.rag.ingestion.s3_vectors_ingestionr   Z)litellm.rag.ingestion.vertex_ai_ingestionr   Zlitellm.rag.rag_queryr   Zlitellm.types.ragr   r   Zlitellm.types.utilsr   Zlitellm.utilsr   r   r   __annotations__r,   r7   r   re   r   r   r   r*   r*   r*   r+   <module>   st    ,
	,HM.-