
    ih/%                         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 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DictListOptionalType)
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/dev/engine/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           	          |t        d      |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&   chains         r   queryzVectorStoreIndexWrapper.query    s    $ ;%.  ,1r++
84++88L;KL
PV
 ||U__h789I9IJJr   c           	         K   |t        d      |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/   r0   s         r   aqueryzVectorStoreIndexWrapper.aquery@   s     $ ;%.  ,1r++
84++88L;KL
PV
 mmU__h$?@@%BRBRSS@s   A&A<(A:)A<c           	          |t        d      |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_keyr0   s         r   query_with_sourcesz*VectorStoreIndexWrapper.query_with_sources`   s~    $ ;%.  ,1r+;;
84++88L;KL
PV
 ||U//:;;r   c           	         K   |t        d      |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,   r5   r8   r0   s         r   aquery_with_sourcesz+VectorStoreIndexWrapper.aquery_with_sources   s     $ ;%.  ,1r+;;
84++88L;KL
PV
 ]]E$6$6#ABBBBs   A&A/(A-)A/)NN)__name__
__module____qualname____doc__r   __annotations__r   model_configstrr   r   r   r   r3   r6   dictr9   r;   r   r   r   r   r      sp   7 $L ,059	KK '(K #4S>2	K
 K 
KF ,059	TT '(T #4S>2	T
 T 
TF ,059	<< '(< #4S>2	<
 < 
<F ,059	CC '(C #4S>2	C
 C 
Cr   r   c                  n    ddl } 	 ddlm} | j	                  d       |S # t        $ r t        d      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)warnings)langchain_community.vectorstores.inmemoryrE   ImportErrorwarn)rF   rE   s     r   _get_in_memory_vectorstorerJ      sJ    
Q
 MM	C
   
P
 	

s    4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)r1   rR   docsloaders       r   from_loadersz$VectorstoreIndexCreator.from_loaders   s;      	'FKK&	'""4((r   c                    K   g }|D ],  }|j                         2 3 d{   }|j                  |        | j                  |       d{   S 7 06 L7 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)
alazy_loadappendafrom_documents)r1   rR   rW   rX   docs        r   afrom_loadersz%VectorstoreIndexCreator.afrom_loaders   sb       	!F#..0 ! !cC 	! ))$///!0/s1   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   )rP   split_documentsrN   rV   rO   rQ   r   r1   r`   sub_docsr   s       r   rV   z&VectorstoreIndexCreator.from_documents   sV     %%55i@9d**99dnn
(,(?(?
 ';??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   )rP   rc   rN   r]   rO   rQ   r   rd   s       r   r]   z'VectorstoreIndexCreator.afrom_documents   sd      %%55i@@D00@@dnn
(,(?(?
 
 ';??
s   AA&A$A&N)r<   r=   r>   r?   r   rJ   rN   r   r   r@   r
   r   rP   r   rC   rQ   r   rA   r   r   r   rY   r_   r	   rV   r]   r   r   r   rL   rL      s    %).2*OT+&  "'8R"SM<S$T:: $L
)D$4 )9P )04
+; 0@W 0@X @;R @@h@	 @r   rL   N) r?   typingr   r   r   r   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   rJ   rL   r   r   r   <module>rq      sl    - 2 2 6 - 0 < 3 Q 1 1 R :LL L
HCi HCVD$5 $J@i J@r   