a
    ag/%                     @   s   d 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 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DictListOptionalType)
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   k/var/www/html/cobodadashboardai.evdpl.com/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   sR   |du rt d|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.
        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)	retriever)NotImplementedErrorr   from_chain_typer   as_retrieverinvoke	input_key
output_keyselfr   r   r   r    chainr   r   r   query    s    zVectorStoreIndexWrapper.queryc                    sX   |du rt d|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   aquery@   s    zVectorStoreIndexWrapper.aqueryc                 K   sL   |du rt d|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_sources`   s    z*VectorStoreIndexWrapper.query_with_sourcesc                    sR   |du rt d|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
   r   r   r,   r.   dictr0   r1   r   r   r   r   r      sT   
  #  #  #  r   c                  C   sB   ddl } zddlm} W n ty2   tdY n0 | 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)warningsZ)langchain_community.vectorstores.inmemoryr:   ImportErrorwarn)r;   r:   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 )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*   rE   docsloaderr   r   r   from_loaders   s    	z$VectorstoreIndexCreator.from_loadersc                    s@   g }|D ]&}|  2 z3 dH W }|| q6 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.
        N)Z
alazy_loadappendafrom_documents)r*   rE   rI   rJ   docr   r   r   afrom_loaders   s
    	z%VectorstoreIndexCreator.afrom_loaders)	documentsr   c                 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   )rC   split_documentsrA   rH   rB   rD   r   r*   rP   Zsub_docsr   r   r   r   rH      s    	z&VectorstoreIndexCreator.from_documentsc                    s6   | 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.
        NrQ   )rC   rR   rA   rM   rB   rD   r   rS   r   r   r   rM      s    z'VectorstoreIndexCreator.afrom_documentsN)r2   r3   r4   r5   r   r>   rA   r   r   r6   r	   r   rC   r   r9   rD   r   r7   r   r   r   rK   rO   r   rH   rM   r   r   r   r   r?      s"   
r?   N) r5   typingr   r   r   r   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   r>   r?   r   r   r   r   <module>   s    