
    h                         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      Z G d	 d
e      Zy)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            
           e Zd Z	 	 	 ddee   dee   dee   fdZ	 ddededee   d	ee   fd
Z	e	Z
d	efdZ	 	 ddee   dee   d	efdZy)_VectorStoreClientNhostporturlc                 z    d}||s|rt        |      || _        y|t        |      |xs d}d| d| | _        y)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   errs        f/var/www/html/eduruby.in/venv/lib/python3.12/site-packages/langchain_community/vectorstores/pathway.py__init__z_VectorStoreClient.__init__   sQ      T?t o%DH| o%:2D av.DH    querykmetadata_filterreturnc                     ||d}|||d<   | j                   dz   }t        j                  |t        j                  |      ddid      }|j                         }t        |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   r   z/v1/retrieveContent-Typeapplication/json   )dataheaderstimeoutc                     | d   S )Ndist )xs    r   <lambda>z*_VectorStoreClient.query.<locals>.<lambda>R   s
    qy r   )key)r   requestspostjsondumpssorted)r   r   r   r   r"   r   response	responsess           r   r   z_VectorStoreClient.query9   sq     Q'&&5D"#hh'==D!#%78	
 MMO	i%899r   c                 x    | j                   dz   }t        j                  |i ddi      }|j                         }|S )z.Fetch basic statistics about the vector store.z/v1/statisticsr   r    r-   r#   r   r+   r,   r-   )r   r   r0   r1   s       r   get_vectorstore_statisticsz-_VectorStoreClient.get_vectorstore_statisticsW   sC     hh))==#%78

 MMO	r   filepath_globpatternc                 ~    | j                   dz   }t        j                  |||dddi      }|j                         }|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   r6   r   r    r3   r4   )r   r   r6   r   r0   r1   s         r   get_input_filesz"_VectorStoreClient.get_input_filesc   sL     hh%==#2(< $%78
 MMO	r   NNN)r!   NNN)__name__
__module____qualname__r   strintr   r   dictr   __call__r5   listr8   r'   r   r   r   r      s     #"!	/sm/ sm/ c]	/8 HL:: :7?}:	d:8 H
D 
 *..2!# 'sm 
	r   r   c                   R   e Zd ZdZ	 	 	 ddee   dee   dee   ddfdZ	 ddee   d	ee	e
      d
ede	e   fdZe	 dde	e   ded	ee	e
      d
edd f
d       Z	 ddeded
ede	e   fdZ	 	 ddededee   de	eeef      fdZdeegef   fdZde
fdZ	 	 ddee   dee   defdZy)PathwayVectorClientz9
    VectorStore connecting to Pathway Vector Store.
    Nr   r   r   r   c                 (    t        |||      | _        y)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   s       r   r   zPathwayVectorClient.__init__   s      )tS9r   texts	metadataskwargsc                     t        d      )z(Pathway is not suitable for this method.zKPathway vector store does not support adding or removing texts from client.NotImplementedError)r   rG   rH   rI   s       r   	add_textszPathwayVectorClient.add_texts   s     "
 	
r   	embeddingc                     t        d      )Nz>Pathway vector store does not support initializing from_texts.rK   )clsrG   rN   rH   rI   s        r   
from_textszPathwayVectorClient.from_texts   s     "L
 	
r   r   r   c                     |j                  dd       }|rt        j                  d|       | j                  |||      }|D cg c]  }t	        |d   |d          c}S c c}w )Nr   zBUnknown kwargs passed to PathwayVectorClient.similarity_search: %sr   r   r   textmetadatapage_contentrU   )poploggingwarningrF   r	   )r   r   r   rI   r   retsrets          r   similarity_searchz%PathwayVectorClient.similarity_search   sp     !**%6=OOT {{!_{M UY
MPH#f+JH
 	
 
s   A r   c                     | j                  |||      }|D cg c]  }t        |d   |d         |d   f c}S c c}w )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.
        rS   rT   rU   rV   r&   )rF   r	   )r   r   r   r   r[   r\   s         r   similarity_search_with_scorez0PathwayVectorClient.similarity_search_with_score   sR    & {{!_{M 
 3v;ZI3v;W
 	
 
s   ;c                     | j                   S N)_cosine_relevance_score_fnr   s    r   _select_relevance_score_fnz.PathwayVectorClient._select_relevance_score_fn   s    ...r   c                 6    | j                   j                         S )z.Fetch basic statistics about the Vector Store.)rF   r5   rc   s    r   r5   z.PathwayVectorClient.get_vectorstore_statistics   s    {{5577r   r6   c                 :    | j                   j                  ||      S )z'List files indexed by the Vector Store.)rF   r8   )r   r   r6   s      r   r8   z#PathwayVectorClient.get_input_files   s     {{**?<PQQr   r9   ra   )   )rg   Nr:   )r;   r<   r=   __doc__r   r>   r?   r   r   r   r@   r   rM   classmethodr
   rQ   r	   r]   r   floatr_   r   rd   r5   rB   r8   r'   r   r   rD   rD      s    #"!	:sm: sm: c]	:
 
:* +/

}

 DJ'

 	


 
c

 
 +/		
Cy	
 	
 DJ'		

 	
 
	
 	
 $%

 
03
	h
$ )-	

 
 "#	

 
eHeO$	%
4/HeWe^,D /8D 8 *..2R!#R 'smR 
	Rr   rD   )rh   r-   rY   typingr   r   r   r   r   r   r+   langchain_core.documentsr	   langchain_core.embeddingsr
   langchain_core.vectorstoresr   r   rD   r'   r   r   <module>ro      sA       A A  - 0 3
_ _DeR+ eRr   