a
    bg                     @   s   d Z ddlZddlZddlmZmZmZmZmZm	Z	 ddl
Z
ddlmZ ddlmZ ddlmZ G dd dZG d	d
 d
eZdS )ah  
Pathway Vector Store client.


The Pathway Vector Server is a pipeline written in the Pathway framweork which indexes
all files in a given folder, embeds them, and builds a vector index. The pipeline reacts
to changes in source files, automatically updating appropriate index entries.

The PathwayVectorClient implements the LangChain VectorStore interface and queries the
PathwayVectorServer to retrieve up-to-date documents.

You can use the client with managed instances of Pathway Vector Store, or run your own
instance as described at https://pathway.com/developers/user-guide/llm-xpack/vectorstore_pipeline/

    N)AnyCallableIterableListOptionalTupleDocument)
Embeddings)VectorStorec                   @   sx   e Zd Zdee ee ee dddZdeeee ee dddZ	e	Z
ed	d
dZdee ee edddZdS )_VectorStoreClientN)hostporturlc                 C   sR   d}|dur$|s|rt ||| _n*|du r4t ||p:d}d| d| | _dS )av  
        A client you can use to query :py:class:`VectorStoreServer`.

        Please provide aither the `url`, or `host` and `port`.

        Args:
            - host: host on which `:py:class:`VectorStoreServer` listens
            - port: port on which `:py:class:`VectorStoreServer` listens
            - url: url at which `:py:class:`VectorStoreServer` listens
        zCEither (`host` and `port`) or `url` must be provided, but not both.NP   zhttp://:)
ValueErrorr   )selfr   r   r   err r   v/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/vectorstores/pathway.py__init__   s    z_VectorStoreClient.__init__   querykmetadata_filterreturnc                 C   sX   ||d}|dur||d< | j d }tj|t|ddidd}| }t|d	d
 dS )av  
        Perform a query to the vector store and fetch results.

        Args:
            - query:
            - k: number of documents to be returned
            - metadata_filter: optional string representing the metadata filtering query
                in the JMESPath format. The search will happen only for documents
                satisfying this filtering.
        )r   r   Nr   z/v1/retrieveContent-Typeapplication/jsonr   )dataheaderstimeoutc                 S   s   | d S )Ndistr   )xr   r   r   <lambda>R       z*_VectorStoreClient.query.<locals>.<lambda>)key)r   requestspostjsondumpssorted)r   r   r   r   r    r   response	responsesr   r   r   r   9   s    

z_VectorStoreClient.queryr   c                 C   s*   | j d }tj|i ddid}| }|S )z.Fetch basic statistics about the vector store.z/v1/statisticsr   r   r*   r!   r   r(   r)   r*   )r   r   r-   r.   r   r   r   get_vectorstore_statisticsW   s    
z-_VectorStoreClient.get_vectorstore_statisticsr   filepath_globpatternr   c                 C   s0   | j d }tj|||dddid}| }|S )a  
        Fetch information on documents in the vector store.

        Args:
            metadata_filter: optional string representing the metadata filtering query
                in the JMESPath format. The search will happen only for documents
                satisfying this filtering.
            filepath_globpattern: optional glob pattern specifying which documents
                will be searched for this query.
        z
/v1/inputs)r   r4   r   r   r0   r1   )r   r   r4   r   r-   r.   r   r   r   get_input_filesc   s    
z"_VectorStoreClient.get_input_files)NNN)r   N)NN)__name__
__module____qualname__r   strintr   r   dictr   __call__r2   listr5   r   r   r   r   r      s,       
  r   c                   @   s  e Zd ZdZdee ee ee ddddZdee ee	e
  ee	e dddZede	e eee	e
  ed d	d
dZdeeee	e dddZdeeee e	eeef  dddZeegef dddZe
dddZd ee ee edddZdS )!PathwayVectorClientz9
    VectorStore connecting to Pathway Vector Store.
    N)r   r   r   r   c                 C   s   t |||| _dS )aO  
        A client you can use to query Pathway Vector Store.

        Please provide aither the `url`, or `host` and `port`.

        Args:
            - host: host on which Pathway Vector Store listens
            - port: port on which Pathway Vector Store listens
            - url: url at which Pathway Vector Store listens
        N)r   client)r   r   r   r   r   r   r   r      s    zPathwayVectorClient.__init__)texts	metadataskwargsr   c                 K   s   t ddS )z(Pathway is not suitable for this method.zKPathway vector store does not support adding or removing texts from client.NNotImplementedError)r   r@   rA   rB   r   r   r   	add_texts   s    zPathwayVectorClient.add_texts)r@   	embeddingrA   rB   r   c                 K   s   t dd S )Nz>Pathway vector store does not support initializing from_texts.rC   )clsr@   rF   rA   rB   r   r   r   
from_texts   s    zPathwayVectorClient.from_texts   )r   r   rB   r   c                 K   s:   | dd }|rtd| | j|||d}dd |D S )Nr   zBUnknown kwargs passed to PathwayVectorClient.similarity_search: %sr   r   r   c                 S   s    g | ]}t |d  |d dqS )textmetadataZpage_contentrL   r   .0retr   r   r   
<listcomp>   s   z9PathwayVectorClient.similarity_search.<locals>.<listcomp>)poploggingwarningr?   )r   r   r   rB   r   retsr   r   r   similarity_search   s    z%PathwayVectorClient.similarity_searchr   c                 C   s   | j |||d}dd |D S )a(  Run similarity search with Pathway with distance.

        Args:
            - query (str): Query text to search for.
            - k (int): Number of results to return. Defaults to 4.
            - metadata_filter (Optional[str]): Filter by metadata.
                Filtering query should be in JMESPath format. Defaults to None.

        Returns:
            List[Tuple[Document, float]]: List of documents most similar to
            the query text and cosine distance in float for each.
            Lower score represents more similarity.
        rJ   c                 S   s(   g | ] }t |d  |d d|d fqS )rK   rL   rM   r#   r   rN   r   r   r   rQ      s   zDPathwayVectorClient.similarity_search_with_score.<locals>.<listcomp>)r?   )r   r   r   r   rU   r   r   r   similarity_search_with_score   s    z0PathwayVectorClient.similarity_search_with_scorer/   c                 C   s   | j S )N)Z_cosine_relevance_score_fnr   r   r   r   _select_relevance_score_fn   s    z.PathwayVectorClient._select_relevance_score_fnc                 C   s
   | j  S )z.Fetch basic statistics about the Vector Store.)r?   r2   rX   r   r   r   r2      s    z.PathwayVectorClient.get_vectorstore_statisticsr3   c                 C   s   | j ||S )z'List files indexed by the Vector Store.)r?   r5   )r   r   r4   r   r   r   r5      s    z#PathwayVectorClient.get_input_files)NNN)N)N)rI   )rI   N)NN)r6   r7   r8   __doc__r   r9   r:   r   r   r   r;   r   rE   classmethodr
   rH   r	   rV   r   floatrW   r   rY   r2   r=   r5   r   r   r   r   r>      s`       
 
     r>   )rZ   r*   rS   typingr   r   r   r   r   r   r(   Zlangchain_core.documentsr	   Zlangchain_core.embeddingsr
   Zlangchain_core.vectorstoresr   r   r>   r   r   r   r   <module>   s    b