o
    Zh                     @   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                
   @   s   e Zd Z			ddee dee dee fddZ	dded	ed
ee dee fddZ	e	Z
defddZ		dd
ee dee defddZdS )_VectorStoreClientNhostporturlc                 C   sT   d}|dur|s
|rt ||| _dS |du rt ||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   _/var/www/html/lang_env/lib/python3.10/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   s    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.queryc                 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filepath_globpatternc                 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   r0   r   r   r-   r.   )r   r   r0   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__r/   listr1   r   r   r   r   r      s@    

r   c                   @   s:  e Zd ZdZ			d dee dee dee ddfddZ	d!d	ee d
ee	e
  dede	e fddZe	d!d	e	e ded
ee	e
  dedd f
ddZ	d"dededede	e fddZ		d#dededee de	eeef  fddZdeegef fddZde
fddZ		d$dee dee defddZdS )%PathwayVectorClientz9
    VectorStore connecting to Pathway Vector Store.
    Nr   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kwargsc                 K      t d)z(Pathway is not suitable for this method.zKPathway vector store does not support adding or removing texts from client.NotImplementedError)r   r>   r?   r@   r   r   r   	add_texts   s   zPathwayVectorClient.add_texts	embeddingc                 K   rA   )Nz>Pathway vector store does not support initializing from_texts.rB   )clsr>   rE   r?   r@   r   r   r   
from_texts   s   zPathwayVectorClient.from_texts   r   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_contentrK   r   .0retr   r   r   
<listcomp>   s    z9PathwayVectorClient.similarity_search.<locals>.<listcomp>)poploggingwarningr=   )r   r   r   r@   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.
        rI   c                 S   s(   g | ]}t |d  |d d|d fqS )rJ   rK   rL   r"   r   rM   r   r   r   rP      s    zDPathwayVectorClient.similarity_search_with_score.<locals>.<listcomp>)r=   )r   r   r   r   rT   r   r   r   similarity_search_with_score   s   z0PathwayVectorClient.similarity_search_with_scorec                 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=   r/   rX   r   r   r   r/      s   
z.PathwayVectorClient.get_vectorstore_statisticsr0   c                 C   s   | j ||S )z'List files indexed by the Vector Store.)r=   r1   )r   r   r0   r   r   r   r1      s   z#PathwayVectorClient.get_input_filesr2   rW   )rH   )rH   Nr3   )r4   r5   r6   __doc__r   r7   r8   r   r   r   r9   r   rD   classmethodr
   rG   r	   rU   r   floatrV   r   rY   r/   r;   r1   r   r   r   r   r<      s    





r<   )rZ   r(   rR   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