o
    `+ i-&                     @   s   d Z ddl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mZ dd	lmZmZmZ dd
lmZ ddlmZ defddZG dd deZdee fddZG dd deZdS )z'Vectorstore stubs for the indexing api.    )AnyOptional)
BaseLoader)Document)
Embeddings)BaseLanguageModel)VectorStore)RecursiveCharacterTextSplitterTextSplitter)	BaseModel
ConfigDictField)RetrievalQAWithSourcesChain)RetrievalQAreturnc                   C   s   t dddS )z=Return the default text splitter used for chunking documents.i  r   )
chunk_sizeZchunk_overlap)r	    r   r   i/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/langchain/indexes/vectorstore.py_get_default_text_splitter   s   r   c                   @   s   e Zd ZU dZeed< edddZ		ddede	e
 d	e	eeef  d
edef
ddZ		ddede	e
 d	e	eeef  d
edef
ddZ		ddede	e
 d	e	eeef  d
edef
ddZ		ddede	e
 d	e	eeef  d
edef
ddZdS )VectorStoreIndexWrapperz-Wrapper around a vectorstore for easy access.vectorstoreTforbidZarbitrary_types_allowedextraNquestionllmretriever_kwargskwargsr   c                 K   sV   |du r
d}t ||pi }tj|fd| jjdi |i|}||j|i|j S )a  Query the vectorstore using the provided LLM.

        Args:
            question: The question or prompt to query.
            llm: The language model to use. Must not be None.
            retriever_kwargs: Optional keyword arguments for the retriever.
            **kwargs: Additional keyword arguments forwarded to the chain.

        Returns:
            The result string from the RetrievalQA chain.
        NThis API has been changed to require an LLM. Please provide an llm to use for querying the vectorstore.
For example,
from langchain_openai import OpenAI
llm = OpenAI(temperature=0)	retrieverr   )NotImplementedErrorr   from_chain_typer   as_retrieverinvoke	input_key
output_keyselfr   r   r   r   msgchainr   r   r   query    s   zVectorStoreIndexWrapper.queryc                    s^   |du rd}t ||pi }tj|fd| jjdi |i|}||j|iI dH |j S )a  Asynchronously query the vectorstore using the provided LLM.

        Args:
            question: The question or prompt to query.
            llm: The language model to use. Must not be None.
            retriever_kwargs: Optional keyword arguments for the retriever.
            **kwargs: Additional keyword arguments forwarded to the chain.

        Returns:
            The asynchronous result string from the RetrievalQA chain.
        Nr   r   r   )r    r   r!   r   r"   ainvoker$   r%   r&   r   r   r   aqueryC   s   zVectorStoreIndexWrapper.aqueryc                 K   sP   |du r
d}t ||pi }tj|fd| jjdi |i|}||j|iS )a  Query the vectorstore and retrieve the answer along with sources.

        Args:
            question: The question or prompt to query.
            llm: The language model to use. Must not be None.
            retriever_kwargs: Optional keyword arguments for the retriever.
            **kwargs: Additional keyword arguments forwarded to the chain.

        Returns:
            A dictionary containing the answer and source documents.
        Nr   r   r   )r    r   r!   r   r"   r#   question_keyr&   r   r   r   query_with_sourcesf   s   z*VectorStoreIndexWrapper.query_with_sourcesc                    sX   |du rd}t ||pi }tj|fd| jjdi |i|}||j|iI dH S )a  Asynchronously query the vectorstore and retrieve the answer and sources.

        Args:
            question: The question or prompt to query.
            llm: The language model to use. Must not be None.
            retriever_kwargs: Optional keyword arguments for the retriever.
            **kwargs: Additional keyword arguments forwarded to the chain.

        Returns:
            A dictionary containing the answer and source documents.
        Nr   r   r   )r    r   r!   r   r"   r+   r-   r&   r   r   r   aquery_with_sources   s   z+VectorStoreIndexWrapper.aquery_with_sources)NN)__name__
__module____qualname____doc__r   __annotations__r   model_configstrr   r   dictr   r*   r,   r.   r/   r   r   r   r   r      sv   
 
&
&
&r   c               
   C   sR   ddl } zddlm} W n ty } zd}t||d}~ww | jddd |S )zGet the InMemoryVectorStore.r   N)InMemoryVectorStorezBPlease install langchain-community to use the InMemoryVectorStore.zUsing InMemoryVectorStore as the default vectorstore.This memory store won't persist data. You should explicitlyspecify a vectorstore when using VectorstoreIndexCreator   )
stacklevel)warningsZ)langchain_community.vectorstores.inmemoryr8   ImportErrorwarn)r;   r8   er(   r   r   r   _get_in_memory_vectorstore   s   
r?   c                   @   s   e Zd ZU dZeedZee e	d< e
e	d< eedZee	d< eedZee	d< eddd	Zd
ee defddZd
ee defddZdee defddZdee defddZdS )VectorstoreIndexCreatorzLogic for creating indexes.)default_factoryvectorstore_cls	embeddingtext_splittervectorstore_kwargsTr   r   loadersr   c                 C   s&   g }|D ]	}| |  q| |S )zCreate a vectorstore index from a list of loaders.

        Args:
            loaders: A list of `BaseLoader` instances to load documents.

        Returns:
            A `VectorStoreIndexWrapper` containing the constructed vectorstore.
        )extendloadfrom_documentsr'   rF   docsloaderr   r   r   from_loaders   s   	
z$VectorstoreIndexCreator.from_loadersc                    s>   g }|D ]}| dd | 2 I dH  q| |I dH S )a  Asynchronously create a vectorstore index from a list of loaders.

        Args:
            loaders: A list of `BaseLoader` instances to load documents.

        Returns:
            A `VectorStoreIndexWrapper` containing the constructed vectorstore.
        c                    s   g | z3 d H W }|q6 S )Nr   ).0docr   r   r   
<listcomp>   s    z9VectorstoreIndexCreator.afrom_loaders.<locals>.<listcomp>N)rG   Z
alazy_loadafrom_documentsrJ   r   r   r   afrom_loaders   s
   	 z%VectorstoreIndexCreator.afrom_loaders	documentsc                 C   s0   | j |}| jj|| jfi | j}t|dS )zCreate a vectorstore index from a list of documents.

        Args:
            documents: A list of `Document` objects.

        Returns:
            A `VectorStoreIndexWrapper` containing the constructed vectorstore.
        r   )rD   split_documentsrB   rI   rC   rE   r   r'   rS   Zsub_docsr   r   r   r   rI      s   	
z&VectorstoreIndexCreator.from_documentsc                    s8   | j |}| jj|| jfi | jI dH }t|dS )zAsynchronously create a vectorstore index from a list of documents.

        Args:
            documents: A list of `Document` objects.

        Returns:
            A `VectorStoreIndexWrapper` containing the constructed vectorstore.
        NrT   )rD   rU   rB   rQ   rC   rE   r   rV   r   r   r   rQ      s   
z'VectorstoreIndexCreator.afrom_documentsN)r0   r1   r2   r3   r   r?   rB   typer   r4   r   r   rD   r
   r7   rE   r   r5   listr   r   rM   rR   r   rI   rQ   r   r   r   r   r@      s(   
 r@   N)r3   typingr   r   Zlangchain_core.document_loadersr   Zlangchain_core.documentsr   Zlangchain_core.embeddingsr   Zlangchain_core.language_modelsr   Zlangchain_core.vectorstoresr   Zlangchain_text_splittersr	   r
   Zpydanticr   r   r   Z*langchain.chains.qa_with_sources.retrievalr   Z"langchain.chains.retrieval_qa.baser   r   r   rW   r?   r@   r   r   r   r   <module>   s      