
    NgO                         d Z ddlZddlmZmZmZ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  G d
 de          ZdededefdZdS )zMilvus Retriever    N)AnyDictListOptional)CallbackManagerForRetrieverRun)Document)
Embeddings)BaseRetriever)model_validator)Milvusc            	       `   e Zd ZU dZeed<   dZeed<   dZe	e
eef                  ed<   dZe	e
eef                  ed<   dZeed	<   dZe	e         ed
<   eed<   eed<    ed          ede
defd                        Z	 ddee         de	ee                  ddfdZdedededee         fdZdS )MilvusRetrieverao  Milvus API retriever.

    See detailed instructions here: https://python.langchain.com/docs/integrations/retrievers/milvus_hybrid_search/

    Setup:
        Install ``langchain-milvus`` and other dependencies:

        .. code-block:: bash

            pip install -U pymilvus[model] langchain-milvus

    Key init args:
        collection: Milvus Collection

    Instantiate:
        .. code-block:: python

            retriever = MilvusCollectionHybridSearchRetriever(collection=collection)

    Usage:
        .. code-block:: python

            query = "What are the story about ventures?"

            retriever.invoke(query)

        .. code-block:: none

            [Document(page_content="In 'The Lost Expedition' by Caspian Grey...", metadata={'doc_id': '449281835035545843'}),
            Document(page_content="In 'The Phantom Pilgrim' by Rowan Welles...", metadata={'doc_id': '449281835035545845'}),
            Document(page_content="In 'The Dreamwalker's Journey' by Lyra Snow..", metadata={'doc_id': '449281835035545846'})]

    Use within a chain:
        .. code-block:: python

            from langchain_core.output_parsers import StrOutputParser
            from langchain_core.prompts import ChatPromptTemplate
            from langchain_core.runnables import RunnablePassthrough
            from langchain_openai import ChatOpenAI

            prompt = ChatPromptTemplate.from_template(
                """Answer the question based only on the context provided.

            Context: {context}

            Question: {question}"""
            )

            llm = ChatOpenAI(model="gpt-3.5-turbo-0125")

            def format_docs(docs):
                return "\n\n".join(doc.page_content for doc in docs)

            chain = (
                {"context": retriever | format_docs, "question": RunnablePassthrough()}
                | prompt
                | llm
                | StrOutputParser()
            )

            chain.invoke("What novels has Lila written and what are their contents?")

        .. code-block:: none

             "Lila Rose has written 'The Memory Thief,' which follows a charismatic thief..."

    embedding_functionLangChainCollectioncollection_nameNcollection_propertiesconnection_argsSessionconsistency_levelsearch_paramsstore	retrieverbefore)modevaluesreturnc                     t          |d         |d         |d         |d         |d                   |d<   |d                             d|d         i	          |d
<   |S )z&Create the Milvus store and retriever.r   r   r   r   r   r   paramr   )search_kwargsr   )r   as_retriever)clsr   s     a/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/retrievers/milvus.pycreate_retrieverz MilvusRetriever.create_retriever`   s}     !'($%*+$%&'
 
w %Wo::"F?$;< ; 
 
{     texts	metadatasc                 <    | j                             ||           dS )zAdd text to the Milvus store

        Args:
            texts (List[str]): The text
            metadatas (List[dict]): Metadata dicts, must line up with existing store
        N)r   	add_texts)selfr%   r&   s      r"   r(   zMilvusRetriever.add_textsp   s"     	
UI.....r$   queryrun_managerkwargsc                P     | j         j        |fd|                                i|S )Nr+   )r   invoke	get_child)r)   r*   r+   r,   s       r"   _get_relevant_documentsz'MilvusRetriever._get_relevant_documents{   sA     %t~$
 
*4466
:@
 
 	
r$   )N)__name__
__module____qualname____doc__r	   __annotations__r   strr   r   r   r   r   r   r   dictr   r
   r   classmethodr#   r   r(   r   r   r0    r$   r"   r   r      s}        B BH #"""0OS0006:8DcN3:::04OXd38n-444&s&&&$(M8D>(((MMM_(###d s    [ $# CG	/ 	/#Y	/+3DJ+?	/		/ 	/ 	/ 	/	
	
 4		

 	
 
h	
 	
 	
 	
 	
 	
r$   r   argsr,   r   c                  N    t          j        dt                     t          | i |S )zDeprecated MilvusRetreiver. Please use MilvusRetriever ('i' before 'e') instead.

    Args:
        *args:
        **kwargs:

    Returns:
        MilvusRetriever
    zfMilvusRetreiver will be deprecated in the future. Please use MilvusRetriever ('i' before 'e') instead.)warningswarnDeprecationWarningr   )r:   r,   s     r"   MilvusRetreiverr?      s5     M	?  
 D+F+++r$   )r4   r<   typingr   r   r   r   langchain_core.callbacksr   langchain_core.documentsr   langchain_core.embeddingsr	   langchain_core.retrieversr
   pydanticr   'langchain_community.vectorstores.milvusr   r   r?   r9   r$   r"   <module>rG      s
      , , , , , , , , , , , , C C C C C C - - - - - - 0 0 0 0 0 0 3 3 3 3 3 3 $ $ $ $ $ $ : : : : : :
s
 s
 s
 s
 s
m s
 s
 s
l,3 ,# ,/ , , , , , ,r$   