
    Ngf                     j    d dl Z d dlmZmZmZmZmZ d dlmZ d dl	m
Z
 d dlmZ  G d de          ZdS )    N)AnyListOptionalSequenceTuple)Document)TextSplitter)MultiVectorRetrieverc                   l   e Zd ZU dZeed<   	 	 dZee         ed<   	 dZee	e
                  ed<   	 	 	 ddee         deee
                  d	ed
eee         eee
ef                  f         fdZ	 	 ddee         deee
                  d	eded
df
dZ	 	 ddee         deee
                  d	eded
df
dZdS )ParentDocumentRetrievera   Retrieve small chunks then retrieve their parent documents.

    When splitting documents for retrieval, there are often conflicting desires:

    1. You may want to have small documents, so that their embeddings can most
        accurately reflect their meaning. If too long, then the embeddings can
        lose meaning.
    2. You want to have long enough documents that the context of each chunk is
        retained.

    The ParentDocumentRetriever strikes that balance by splitting and storing
    small chunks of data. During retrieval, it first fetches the small chunks
    but then looks up the parent ids for those chunks and returns those larger
    documents.

    Note that "parent document" refers to the document that a small chunk
    originated from. This can either be the whole raw document OR a larger
    chunk.

    Examples:

        .. code-block:: python

            from langchain_chroma import Chroma
            from langchain_community.embeddings import OpenAIEmbeddings
            from langchain_text_splitters import RecursiveCharacterTextSplitter
            from langchain.storage import InMemoryStore

            # This text splitter is used to create the parent documents
            parent_splitter = RecursiveCharacterTextSplitter(chunk_size=2000, add_start_index=True)
            # This text splitter is used to create the child documents
            # It should create documents smaller than the parent
            child_splitter = RecursiveCharacterTextSplitter(chunk_size=400, add_start_index=True)
            # The vectorstore to use to index the child chunks
            vectorstore = Chroma(embedding_function=OpenAIEmbeddings())
            # The storage layer for the parent documents
            store = InMemoryStore()

            # Initialize the retriever
            retriever = ParentDocumentRetriever(
                vectorstore=vectorstore,
                docstore=store,
                child_splitter=child_splitter,
                parent_splitter=parent_splitter,
            )
    child_splitterNparent_splitterchild_metadata_fieldsT	documentsidsadd_to_docstorereturnc                 0   | j         | j                             |          }|d |D             }|st          d          n1t          |          t          |          k    rt          d          |}g }g }t	          |          D ]\  }}||         }	| j                            |g          }
| j        |
D ]fd| j        D             _        |
D ]|	j        | j        <   |	                    |
           |
                    |	|f           ||fS )Nc                 N    g | ]"}t          t          j                              #S  )struuiduuid4).0_s     j/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain/retrievers/parent_document_retriever.py
<listcomp>zBParentDocumentRetriever._split_docs_for_adding.<locals>.<listcomp>Q   s&    <<<Qs4:<<((<<<    z8If ids are not passed in, `add_to_docstore` MUST be TruezaGot uneven list of documents and ids. If `ids` is provided, should be same length as `documents`.c                 ,    i | ]}|j         |         S r   )metadata)r   k_docs     r   
<dictcomp>zBParentDocumentRetriever._split_docs_for_adding.<locals>.<dictcomp>e   s/     % % %014=+% % %r   )r   split_documents
ValueErrorlen	enumerater   r   r    id_keyextendappend)selfr   r   r   doc_idsdocs	full_docsidoc_idsub_docsr"   s              @r   _split_docs_for_addingz.ParentDocumentRetriever._split_docs_for_addingH   s|    +,<<YGGI;<<)<<<G"  N  
 9~~S)) R   G		** 	) 	)FAs!*C*::C5AAH)5$  D% % % %595O% % %DMM ! 1 1-0dk**KK!!!c3Z((((Yr   kwargsc                     |                      |||          \  }} | j        j        |fi | |r| j                            |           dS dS )a  Adds documents to the docstore and vectorstores.

        Args:
            documents: List of documents to add
            ids: Optional list of ids for documents. If provided should be the same
                length as the list of documents. Can be provided if parent documents
                are already in the document store and you don't want to re-add
                to the docstore. If not provided, random UUIDs will be used as
                ids.
            add_to_docstore: Boolean of whether to add documents to docstore.
                This can be false if and only if `ids` are provided. You may want
                to set this to False if the documents are already in the docstore
                and you don't want to re-add them.
        N)r3   vectorstoreadd_documentsdocstoremsetr+   r   r   r   r4   r-   r.   s          r   r7   z%ParentDocumentRetriever.add_documentso   sk    * 55ioVVi&&t66v666 	*My)))))	* 	*r   c                    K   |                      |||          \  }} | j        j        |fi | d {V  |r"| j                            |           d {V  d S d S )N)r3   r6   aadd_documentsr8   amsetr:   s          r   r<   z&ParentDocumentRetriever.aadd_documents   s       55ioVVi-d-d==f========= 	1-%%i00000000000	1 	1r   )NT)__name__
__module____qualname____doc__r	   __annotations__r   r   r   r   r   r   r   boolr   r3   r   r7   r<   r   r   r   r   r   
   s        - -^ !   =$.2OXl+222O 6:8HSM2999 $( $	% %>% d3i % 	%
 
tH~tE#x-$899	:% % % %T $( $	* *>* d3i * 	*
 * 
* * * *: $( $	
1 
1>
1 d3i 
1 	
1
 
1 

1 
1 
1 
1 
1 
1r   r   )r   typingr   r   r   r   r   langchain_core.documentsr   langchain_text_splittersr	   langchain.retrieversr
   r   r   r   r   <module>rH      s     7 7 7 7 7 7 7 7 7 7 7 7 7 7 - - - - - - 1 1 1 1 1 1 5 5 5 5 5 5I1 I1 I1 I1 I12 I1 I1 I1 I1 I1r   