o
    Zh                     @  sp   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 deZd	S )
    )annotations)AnyDictIterableListOptionalTuple)uuid4Document)
Embeddings)VectorStorec                      s   e Zd ZdZ	d7d8 fd	d
Z	d7d9ddZd:ddZ	d;d<ddZ		d=d>d!d"Zd?d'd(Z	d@d*d+Z
d7dAd.d/ZedBd1d2Ze		dCdDd3d4Ze	d7dEd5d6Z  ZS )FVLitez?VLite is a simple and fast vector database for semantic search.Nembedding_functionr   
collectionOptional[str]kwargsr   c                   sf   t    || _|pdt j | _zddlm} W n ty%   tdw |dd| ji|| _d S )NZvlite_r   )r   RCould not import vlite python package. Please install it with `pip install vlite`.r    )	super__init__r   r	   hexr   vliter   ImportError)selfr   r   r   r   	__class__r   ]/var/www/html/lang_env/lib/python3.10/site-packages/langchain_community/vectorstores/vlite.pyr      s   
zVLite.__init__textsIterable[str]	metadatasOptional[List[dict]]return	List[str]c                 K  sn   t |}|ddd |D }| j|}|sdd |D }dd t||||D }| j|}dd |D S )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.
        idsc                 S     g | ]}t t qS r   strr	   .0_r   r   r   
<listcomp>4       z#VLite.add_texts.<locals>.<listcomp>c                 S  s   g | ]}i qS r   r   r(   r   r   r   r+   7   s    c                 S  s"   g | ]\}}}}||||d qS ))textmetadataid	embeddingr   )r)   r-   r.   r/   r0   r   r   r   r+   8   s    
c                 S  s   g | ]}|d  qS )r   r   )r)   resultr   r   r   r+   =       )listpopr   Zembed_documentszipr   add)r   r   r    r   r$   Z
embeddingsZdata_pointsresultsr   r   r   	add_texts#   s   zVLite.add_texts	documentsList[Document]c           	   	     s   | ddd |D }g }g }t||D ]O\} d|v rWzddlm} W n ty/   tdw ||d }|| ||jgt|  | fddtt|D  q|	|j
 |	|j q| j|||d	S )
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$   c                 S  r%   r   r&   r(   r   r   r   r+   N   r,   z'VLite.add_documents.<locals>.<listcomp>	file_pathr   )process_filer   c                   s   g | ]	}  d | qS )r*   r   )r)   ir/   r   r   r+   ^   s    )r$   )r4   r5   Zvlite.utilsr<   r   extendr.   lenrangeappendpage_contentr8   )	r   r9   r   r$   r   r    docr<   Zprocessed_datar   r>   r   add_documents?   s&   
"zVLite.add_documents   queryr'   kintc                 K  s   | j ||d}dd |D S )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.
        )rH   c                 S  s   g | ]\}}|qS r   r   )r)   rD   r*   r   r   r   r+   t   r2   z+VLite.similarity_search.<locals>.<listcomp>)similarity_search_with_score)r   rG   rH   r   Zdocs_and_scoresr   r   r   similarity_searchd   s   zVLite.similarity_searchfilterOptional[Dict[str, str]]List[Tuple[Document, float]]c           	      K  s<   |pi }| j |}| jj|||d|d}dd |D }|S )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-   Ztop_kr.   Zreturn_scoresr0   c                 S  s"   g | ]\}}}t ||d |fqS )rC   r.   r
   )r)   r-   Zscorer.   r   r   r   r+      s    z6VLite.similarity_search_with_score.<locals>.<listcomp>)r   Zembed_queryr   retrieve)	r   rG   rH   rL   r   r.   r0   r7   Zdocuments_with_scoresr   r   r   rJ   v   s   z"VLite.similarity_search_with_scoredocument_iddocumentr   Nonec                 C  s   | j j||j|jd dS )z/Update an existing document in the vectorstore.)r-   r.   N)r   updaterC   r.   )r   rQ   rR   r   r   r   update_document   s   

zVLite.update_documentr$   c                 C  s   | j |}dd |D }|S )zGet documents by their IDs.c                 S  s   g | ]
\}}t ||d qS rO   r
   )r)   r-   r.   r   r   r   r+      s    zVLite.get.<locals>.<listcomp>)r   get)r   r$   r7   r9   r   r   r   rV      s
   z	VLite.getOptional[List[str]]Optional[bool]c                 K  s$   |dur| j j|fi | dS dS )zDelete by ids.NT)r   delete)r   r$   r   r   r   r   rY      s   zVLite.deleter0   c                 K  s   | 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   Nr   r   )clsr0   r   r   r   r   r   r   from_existing_index   s   zVLite.from_existing_indexc                 K  s,   | 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)
        rZ   Nr   )r8   )r[   r   r0   r    r   r   r   r   r   r   
from_texts   s   zVLite.from_textsc                 K  s*   | 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)
        rZ   Nr   )rE   )r[   r9   r0   r   r   r   r   r   r   from_documents   s   zVLite.from_documents)N)r   r   r   r   r   r   )r   r   r    r!   r   r   r"   r#   )r9   r:   r   r   r"   r#   )rF   )rG   r'   rH   rI   r   r   r"   r:   )rF   N)
rG   r'   rH   rI   rL   rM   r   r   r"   rN   )rQ   r'   rR   r   r"   rS   )r$   r#   r"   r:   )r$   rW   r   r   r"   rX   )r0   r   r   r'   r   r   r"   r   )NN)r   r#   r0   r   r    r!   r   r   r   r   r"   r   )
r9   r:   r0   r   r   r   r   r   r"   r   )__name__
__module____qualname____doc__r   r8   rE   rK   rJ   rU   rV   rY   classmethodr\   r]   r^   __classcell__r   r   r   r   r      s0    
(
 
r   N)
__future__r   typingr   r   r   r   r   r   uuidr	   Zlangchain_core.documentsr   Zlangchain_core.embeddingsr   Zlangchain_core.vectorstoresr   r   r   r   r   r   <module>   s     