a
    bgO                     @   s   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 deZeeedddZdS )zMilvus Retriever    N)AnyDictListOptional)CallbackManagerForRetrieverRun)Document)
Embeddings)BaseRetriever)model_validator)Milvusc                   @   s   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dee
edddZdee e	ee  ddddZeeeee dd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_functionZLangChainCollectioncollection_nameNcollection_propertiesconnection_argsSessionconsistency_levelsearch_paramsstore	retrieverbefore)mode)valuesreturnc                 C   sH   t |d |d |d |d |d |d< |d jd|d id	|d
< |S )z&Create the Milvus store and retriever.r   r   r   r   r   r   paramr   )Zsearch_kwargsr   )r   Zas_retriever)clsr    r   s/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/retrievers/milvus.pycreate_retriever`   s    

z MilvusRetriever.create_retriever)texts	metadatasr   c                 C   s   | 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    r   r   r   r!   p   s    	zMilvusRetriever.add_texts)queryrun_managerkwargsr   c                K   s   | j j|fd| i|S )Nr$   )r   ZinvokeZ	get_child)r"   r#   r$   r%   r   r   r   _get_relevant_documents{   s    z'MilvusRetriever._get_relevant_documents)N)__name__
__module____qualname____doc__r   __annotations__r   strr   r   r   r   r   r   r   dictr   r	   r
   classmethodr   r   r!   r   r   r&   r   r   r   r   r      s,   
D r   )argsr%   r   c                  O   s   t 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%   r   r   r   MilvusRetreiver   s
    
r3   )r*   r0   typingr   r   r   r   Zlangchain_core.callbacksr   Zlangchain_core.documentsr   Zlangchain_core.embeddingsr   Zlangchain_core.retrieversr	   Zpydanticr
   Z'langchain_community.vectorstores.milvusr   r   r3   r   r   r   r   <module>   s   v