
    h-&                         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Z G d de      Zdee   fdZ G d de      Zy)z'Vectorstore stubs for the indexing api.    )AnyOptional)
BaseLoader)Document)
Embeddings)BaseLanguageModel)VectorStore)RecursiveCharacterTextSplitterTextSplitter)	BaseModel
ConfigDictField)RetrievalQAWithSourcesChain)RetrievalQAreturnc                      t        dd      S )z=Return the default text splitter used for chunking documents.i  r   )
chunk_sizechunk_overlap)r
        [/var/www/html/eduruby.in/venv/lib/python3.12/site-packages/langchain/indexes/vectorstore.py_get_default_text_splitterr      s    )TKKr   c                      e Zd ZU dZeed<    edd      Z	 	 ddede	e
   d	e	eeef      d
edef
dZ	 	 ddede	e
   d	e	eeef      d
edef
dZ	 	 ddede	e
   d	e	eeef      d
edef
dZ	 	 ddede	e
   d	e	eeef      d
edef
dZy)VectorStoreIndexWrapperz-Wrapper around a vectorstore for easy access.vectorstoreTforbidarbitrary_types_allowedextraNquestionllmretriever_kwargskwargsr   c           	          |d}t        |      |xs i }t        j                  |fd | j                  j                  di |i|}|j                  |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.
        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)	retrieverr   )NotImplementedErrorr   from_chain_typer   as_retrieverinvoke	input_key
output_keyselfr    r!   r"   r#   msgchains          r   queryzVectorStoreIndexWrapper.query    s    $ ;.  &c**+1r++
3d&&33G6FG
 

 ||U__h789I9IJJr   c           	         K   |d}t        |      |xs i }t        j                  |fd | j                  j                  di |i|}|j                  |j                  |i       d{   |j                     S 7 w)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-   s          r   aqueryzVectorStoreIndexWrapper.aqueryC   s     $ ;.  &c**+1r++
3d&&33G6FG
 

 mmU__h$?@@%BRBRSS@s   A(A>*A<+A>c           	          |d}t        |      |xs i }t        j                  |fd | j                  j                  di |i|}|j                  |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.
        r%   r&   r   )r'   r   r(   r   r)   r*   question_keyr-   s          r   query_with_sourcesz*VectorStoreIndexWrapper.query_with_sourcesf   s    $ ;.  &c**+1r+;;
3d&&33G6FG
 

 ||U//:;;r   c           	         K   |d}t        |      |xs i }t        j                  |fd | j                  j                  di |i|}|j                  |j                  |i       d{   S 7 w)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)   r3   r6   r-   s          r   aquery_with_sourcesz+VectorStoreIndexWrapper.aquery_with_sources   s     $ ;.  &c**+1r+;;
3d&&33G6FG
 

 ]]E$6$6#ABBBBs   A(A1*A/+A1)NN)__name__
__module____qualname____doc__r	   __annotations__r   model_configstrr   r   dictr   r1   r4   r7   r9   r   r   r   r   r      sp   7 $L ,059	!K!K '(!K #4S>2	!K
 !K 
!KL ,059	!T!T '(!T #4S>2	!T
 !T 
!TL ,059	!<!< '(!< #4S>2	!<
 !< 
!<L ,059	!C!C '(!C #4S>2	!C
 !C 
!Cr   r   c                      ddl } 	 ddlm} | j	                  dd       |S # t        $ r}d}t        |      |d}~ww xY w)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)warnings)langchain_community.vectorstores.inmemoryrC   ImportErrorwarn)rF   rC   er/   s       r   _get_in_memory_vectorstorerK      sW    &Q MM	C 	     &R#A%&s   ! 	=8=c                       e Zd ZU dZ ee      Zee   e	d<   e
e	d<    ee      Zee	d<    ee      Zee	d<    edd	      Zd
ee   defdZd
ee   defdZdee   defdZdee   defdZy)VectorstoreIndexCreatorzLogic for creating indexes.)default_factoryvectorstore_cls	embeddingtext_splittervectorstore_kwargsTr   r   loadersr   c                 t    g }|D ]!  }|j                  |j                                # | j                  |      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.   rS   docsloaders       r   from_loadersz$VectorstoreIndexCreator.from_loaders   s;      	'FKK&	'""4((r   c           	         K   g }|D ],  }|j                  |j                         2 cg c3 d{   }| | j                  |       d{   S 7 !6 nc c}w c}       Y7 w)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)rU   
alazy_loadafrom_documents)r.   rS   rX   rY   docs        r   afrom_loadersz%VectorstoreIndexCreator.afrom_loaders   sb       	DFKKf.?.?.ABBs	D))$/// CBBC/sA   %A"A
AAAA
A"
A A"AA
A"	documentsc                     | j                   j                  |      } | j                  j                  || j                  fi | j
                  }t        |      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   )rQ   split_documentsrO   rW   rP   rR   r   r.   r`   sub_docsr   s       r   rW   z&VectorstoreIndexCreator.from_documents   sY     %%55i@9d**99NN
 %%

 ';??r   c                    K   | j                   j                  |      } | j                  j                  || j                  fi | j
                   d{   }t        |      S 7 w)zAsynchronously create a vectorstore index from a list of documents.

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

        Returns:
            A `VectorStoreIndexWrapper` containing the constructed vectorstore.
        Nrb   )rQ   rc   rO   r]   rP   rR   r   rd   s       r   r]   z'VectorstoreIndexCreator.afrom_documents   sg      %%55i@@D00@@NN
 %%
 

 ';??
s   AA&A$A&N)r:   r;   r<   r=   r   rK   rO   typer	   r>   r   r   rQ   r   rA   rR   r   r?   listr   r   rZ   r_   r   rW   r]   r   r   r   rM   rM      s    %).2*OT+&  "'8R"SM<S$T:: $L
)D$4 )9P )04
+; 0@W 0@X @;R @"@>@ 
!@r   rM   N)r=   typingr   r   langchain_core.document_loadersr   langchain_core.documentsr   langchain_core.embeddingsr   langchain_core.language_modelsr   langchain_core.vectorstoresr	   langchain_text_splittersr
   r   pydanticr   r   r   *langchain.chains.qa_with_sources.retrievalr   "langchain.chains.retrieval_qa.baser   r   r   rg   rK   rM   r   r   r   <module>rs      si    -   6 - 0 < 3 Q 1 1 R :LL L
TCi TCnD$5 $N@i N@r   