
    h                    v    d dl mZ d dlmZ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  G d de      Zy	)
    )annotations)AnyDictIterableListOptionalTuple)uuid4)Document)
Embeddings)VectorStorec                  ,    e Zd ZdZ	 d	 	 	 	 	 d fdZ	 d	 	 	 	 	 	 	 ddZ	 	 	 	 	 	 ddZ	 d	 	 	 	 	 	 	 ddZ	 	 d	 	 	 	 	 	 	 	 	 ddZddZ	ddZ
ddd	Ze	 	 	 	 	 	 	 	 dd
       Ze	 	 d	 	 	 	 	 	 	 	 	 	 	 dd       Ze	 d	 	 	 	 	 	 	 	 	 dd       Z xZS )VLitez?VLite is a simple and fast vector database for semantic search.c                    t         |           || _        |xs dt               j                   | _        	 ddlm}  |dd| j
                  i|| _        y # t        $ r t        d      w xY w)Nvlite_r   )r   RCould not import vlite python package. Please install it with `pip install vlite`.
collection )	super__init__embedding_functionr
   hexr   vliter   ImportError)selfr   r   kwargsr   	__class__s        d/var/www/html/eduruby.in/venv/lib/python3.12/site-packages/langchain_community/vectorstores/vlite.pyr   zVLite.__init__   sv     	"4$>&(>	# @doo@@
  	> 	s   A A-c                   t        |      }|j                  d|D cg c]  }t        t                      c}      }| j                  j                  |      }|s|D cg c]  }i  }}t        ||||      D 	
cg c]  \  }}}	}
|||	|
d }}	}}}
| j                  j                  |      }|D cg c]  }|d   	 c}S c c}w c c}w c c}
}	}}w c c}w )ar  Run more texts through the embeddings and add to the vectorstore.

        Args:
            texts: Iterable of strings to add to the vectorstore.
            metadatas: Optional list of metadatas associated with the texts.
            kwargs: vectorstore specific parameters

        Returns:
            List of ids from adding the texts into the vectorstore.
        ids)textmetadataid	embeddingr   )	listpopstrr
   r   embed_documentszipr   add)r   texts	metadatasr   _r    
embeddingsr!   r"   r#   r$   data_pointsresultsresults                 r   	add_textszVLite.add_texts#   s      Ujju =!UW =>,,<<UC
%*++I+ 25UIsJ1W
 
-hI xr	R
 
 **..-(/0fq	00 !> ,

 1s   C
	C<C
3Cc           
     x   |j                  d|D cg c]  }t        t                      c}      }g }g }t        ||      D ]  \  }}d|v r	 ddlm}	  |	|d         }
|j                  |
       |j                  |j                  gt        |
      z         |j                  t        t        |
            D cg c]	  }| d|  c}       |j                  |j                         |j                  |j                          | j                  |||      S c c}w # t        $ r t        d      w xY wc c}w )aa  Add a list of documents to the vectorstore.

        Args:
            documents: List of documents to add to the vectorstore.
            kwargs: vectorstore specific parameters such as "file_path" for processing
                    directly with vlite.

        Returns:
            List of ids from adding the documents into the vectorstore.
        r    	file_pathr   )process_filer   r-   )r    )r&   r'   r
   r)   vlite.utilsr5   r   extendr"   lenrangeappendpage_contentr2   )r   	documentsr   r-   r    r+   r,   docr#   r5   processed_datais               r   add_documentszVLite.add_documents?   s)    jjy A!UW AB	9c* 	/GCf$8 ".f[.A!B^,  #,,#n2E!EF

s>7J1KLArd!A3KLMS--.  .!	/" ~~eYC~88) !B # %F  Ms   D
D8D7
D4c                ^    | j                  ||      }|D cg c]  \  }}|	 c}}S c c}}w )zReturn docs most similar to query.

        Args:
            query: Text to look up documents similar to.
            k: Number of Documents to return. Defaults to 4.

        Returns:
            List of Documents most similar to the query.
        )k)similarity_search_with_score)r   queryrB   r   docs_and_scoresr=   r-   s          r   similarity_searchzVLite.similarity_searchd   s1     ;;EQ;G"12Q222s   )c           	         |xs i }| j                   j                  |      }| j                  j                  |||d|      }|D 	cg c]  \  }}	}t	        ||      |	f }
}	}}|
S c c}}	}w )aM  Return docs most similar to query.

        Args:
            query: Text to look up documents similar to.
            k: Number of Documents to return. Defaults to 4.
            filter: Filter by metadata. Defaults to None.

        Returns:
            List of Tuples of (doc, score), where score is the similarity score.
        T)r!   top_kr"   return_scoresr$   r;   r"   )r   embed_queryr   retriever   )r   rD   rB   filterr   r"   r$   r0   r!   scoredocuments_with_scoress              r   rC   z"VLite.similarity_search_with_scorev   s    " <R++77>	**%% & 
 *1!
 !
%eX 4(;UC!
 !
 %$	!
s   A(c                h    | j                   j                  ||j                  |j                         y)z/Update an existing document in the vectorstore.)r!   r"   N)r   updater;   r"   )r   document_iddocuments      r   update_documentzVLite.update_document   s,    

h33h>O>O 	 	
    c                    | j                   j                  |      }|D cg c]  \  }}t        ||       }}}|S c c}}w )zGet documents by their IDs.rJ   )r   getr   )r   r    r0   r!   r"   r<   s         r   rW   z	VLite.get   sF    **..%QX
?MtXH$:
	 
 
s   =c                D    | | j                   j                  |fi | yy)zDelete by ids.NT)r   delete)r   r    r   s      r   rY   zVLite.delete   s&    ?DJJc,V,rU   c                     | d||d|}|S )zLoad an existing VLite index.

        Args:
            embedding: Embedding function
            collection: Name of the collection to load.

        Returns:
            VLite vector store.
        r   r   r   r   )clsr$   r   r   r   s        r   from_existing_indexzVLite.from_existing_index   s      RyZR6RrU   c                D     | d||d|} |j                   ||fi | |S )a  Construct VLite wrapper from raw documents.

        This is a user-friendly interface that:
        1. Embeds documents.
        2. Adds the documents to the vectorstore.

        This is intended to be a quick way to get started.

        Example:
        .. code-block:: python

            from langchain import VLite
            from langchain.embeddings import OpenAIEmbeddings

            embeddings = OpenAIEmbeddings()
            vlite = VLite.from_texts(texts, embeddings)
        r[   r   )r2   )r\   r+   r$   r,   r   r   r   s          r   
from_textszVLite.from_texts   s2    4 RyZR6Ry3F3rU   c                B     | d||d|} |j                   |fi | |S )a  Construct VLite wrapper from a list of documents.

        This is a user-friendly interface that:
        1. Embeds documents.
        2. Adds the documents to the vectorstore.

        This is intended to be a quick way to get started.

        Example:
        .. code-block:: python

            from langchain import VLite
            from langchain.embeddings import OpenAIEmbeddings

            embeddings = OpenAIEmbeddings()
            vlite = VLite.from_documents(documents, embeddings)
        r[   r   )r@   )r\   r<   r$   r   r   r   s         r   from_documentszVLite.from_documents   s2    2 RyZR6RI00rU   )N)r   r   r   Optional[str]r   r   )r+   zIterable[str]r,   Optional[List[dict]]r   r   return	List[str])r<   List[Document]r   r   rd   re   )   )rD   r'   rB   intr   r   rd   rf   )rg   N)
rD   r'   rB   rh   rM   zOptional[Dict[str, str]]r   r   rd   zList[Tuple[Document, float]])rR   r'   rS   r   rd   None)r    re   rd   rf   )r    zOptional[List[str]]r   r   rd   zOptional[bool])r$   r   r   r'   r   r   rd   r   )NN)r+   re   r$   r   r,   rc   r   rb   r   r   rd   r   )
r<   rf   r$   r   r   rb   r   r   rd   r   )__name__
__module____qualname____doc__r   r2   r@   rF   rC   rT   rW   rY   classmethodr]   r_   ra   __classcell__)r   s   @r   r   r      s   I
 %)A&A "A 	A, +/11 (1 	1
 
18#9!#9 #9 
	#9P 33 3 	3
 
3* +/	%% % )	%
 % 
&%@
   	
 
 $ 
 +/$(  (	
 "  
 : 
 %)	!  "	
  
 rU   r   N)
__future__r   typingr   r   r   r   r   r	   uuidr
   langchain_core.documentsr   langchain_core.embeddingsr   langchain_core.vectorstoresr   r   r   rU   r   <module>rv      s,    " > =  . 0 3jK jrU   