a
    bg                     @  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d4dddd fddZd5d	d
dddddZddddddZd6dddddddZd7ddddddddZdddd d!d"Z	ddd#d$d%Z
d8d&dd'd(d)d*Zedddd d+d,d-Zed9ddd
ddd d.d/d0Zed:ddddd d1d2d3Z  ZS );VLitez?VLite is a simple and fast vector database for semantic search.Nr   zOptional[str]r   )embedding_function
collectionkwargsc                   sj   t    || _|p dt j | _zddlm} W n tyN   tdY n0 |f 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__ t/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/vectorstores/vlite.pyr      s    

zVLite.__init__zIterable[str]zOptional[List[dict]]z	List[str])texts	metadatasr   returnc                 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  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   r'   c                 S  s"   g | ]\}}}}||||d qS ))textmetadataid	embeddingr   )r$   r(   r)   r*   r+   r   r   r   r&   8   s   
c                 S  s   g | ]}|d  qS )r   r   )r$   resultr   r   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_textszList[Document])	documentsr   r   c           	   	     s   | ddd |D }g }g }t||D ]\} d|v rzddlm} W n tyb   tdY n0 ||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  s   g | ]}t t qS 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&   ^   r'   )r    )r.   r/   Zvlite.utilsr5   r   extendr)   lenrangeappendpage_contentr2   )	r   r3   r   r    r   r   docr5   Zprocessed_datar   r7   r   add_documents?   s$    

"zVLite.add_documents   r"   int)querykr   r   c                 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.
        )rB   c                 S  s   g | ]\}}|qS r   r   )r$   r=   r%   r   r   r   r&   t   r'   z+VLite.similarity_search.<locals>.<listcomp>)similarity_search_with_score)r   rA   rB   r   Zdocs_and_scoresr   r   r   similarity_searchd   s    zVLite.similarity_searchzOptional[Dict[str, str]]zList[Tuple[Document, float]])rA   rB   filterr   r   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_scoresr+   c                 S  s"   g | ]\}}}t ||d |fqS )r<   r)   r
   )r$   r(   Zscorer)   r   r   r   r&      s   z6VLite.similarity_search_with_score.<locals>.<listcomp>)r   Zembed_queryr   retrieve)	r   rA   rB   rE   r   r)   r+   r1   Zdocuments_with_scoresr   r   r   rC   v   s    z"VLite.similarity_search_with_scorer   None)document_iddocumentr   c                 C  s   | j j||j|jd dS )z/Update an existing document in the vectorstore.)r(   r)   N)r   updater<   r)   )r   rI   rJ   r   r   r   update_document   s    
zVLite.update_document)r    r   c                 C  s   | j |}dd |D }|S )zGet documents by their IDs.c                 S  s   g | ]\}}t ||d qS rF   r
   )r$   r(   r)   r   r   r   r&      s   zVLite.get.<locals>.<listcomp>)r   get)r   r    r1   r3   r   r   r   rM      s
    z	VLite.getzOptional[List[str]]zOptional[bool])r    r   r   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   rN      s    zVLite.delete)r+   r   r   r   c                 K  s   | f ||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   )clsr+   r   r   r   r   r   r   from_existing_index   s    zVLite.from_existing_index)r   r+   r   r   r   r   c                 K  s,   | f ||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)
        rO   )r2   )rP   r   r+   r   r   r   r   r   r   r   
from_texts   s    zVLite.from_texts)r3   r+   r   r   r   c                 K  s*   | f ||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)
        rO   )r>   )rP   r3   r+   r   r   r   r   r   r   from_documents   s    zVLite.from_documents)N)N)r?   )r?   N)N)NN)N)__name__
__module____qualname____doc__r   r2   r>   rD   rC   rL   rM   rN   classmethodrQ   rR   rS   __classcell__r   r   r   r   r      s.     (       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    