a
    •˜ëj-&  ã                   @   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 edœdd„ZG dd„ deƒZee dœ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   ú\/mnt/bigstor/www/usecases/.venv/lib/python3.9/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ee	e
 e	eeef  eedœdd	„Zdee	e
 e	eeef  eedœd
d„Zdee	e
 e	eeef  eedœdd„Zdee	e
 e	eeef  eedœ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f 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.
        Nú´This 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)Ú	retriever)Ú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f 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"   Ú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f 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#   Úquestion_keyr&   r   r   r   Úquery_with_sourcesf   s    ÿÿþýz*VectorStoreIndexWrapper.query_with_sourcesc                 Ë   sV   |du rd}t |ƒ‚|pi }tj|fd| jjf 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   Úaquery_with_sources‰   s    ÿÿþýz+VectorStoreIndexWrapper.aquery_with_sources)NN)NN)NN)NN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Ú__annotations__r   Úmodel_configÚstrr   r   Údictr   r*   r,   r.   r/   r   r   r   r   r      sT   
þ  üú&  üú&  üú&  üúr   c               
   C   s^   ddl } zddlm} W n2 tyJ } zd}t|ƒ|‚W Y d}~n
d}~0 0 | jddd� |S )zGet the InMemoryVectorStore.r   N)ÚInMemoryVectorStorezBPlease install langchain-community to use the InMemoryVectorStore.z¨Using 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ee ed
œdd„Zee ed
œdd„Zee edœdd„Zee edœ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 )zõ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.
        )ÚextendÚloadÚfrom_documents©r'   rF   Z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>ç   ó    z9VectorstoreIndexCreator.afrom_loaders.<locals>.<listcomp>N)rG   Z
alazy_loadÚafrom_documentsrJ   r   r   r   Úafrom_loadersÜ   s    	 z%VectorstoreIndexCreator.afrom_loaders)Ú	documentsr   c                 C   s0   | j  |¡}| jj|| jfi | j¤Ž}t|d�S )zãCreate 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                 Ã   s6   | j  |¡}| jj|| jfi | j¤ŽI dH }t|d�S )zòAsynchronously 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   rL   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    