
    Ng                         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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            
           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dS )_VectorStoreClientNhostporturlc                     d}||s|rt          |          || _        dS |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   errs        d/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/vectorstores/pathway.py__init__z_VectorStoreClient.__init__   sj      T? &t & oo%DHHH| oo%:2D.....DHHH       querykmetadata_filterreturnc                     ||d}|||d<   | j         dz   }t          j        |t          j        |          ddid          }|                                }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   Nr   z/v1/retrieveContent-Typeapplication/jsonr   )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   s     Q''&&5D"#h'=D!!#%78	
 
 
 MMOO	i%8%89999r   c                 t    | j         dz   }t          j        |i ddi          }|                                }|S )z.Fetch basic statistics about the vector store.z/v1/statisticsr!   r"   r.   r$   r   r,   r-   r.   )r   r   r1   r2   s       r   get_vectorstore_statisticsz-_VectorStoreClient.get_vectorstore_statisticsW   sK     h))=#%78
 
 

 MMOO	r   filepath_globpatternc                 z    | j         dz   }t          j        |||dddi          }|                                }|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   r7   r!   r"   r4   r5   )r   r   r7   r   r1   r2   s         r   get_input_filesz"_VectorStoreClient.get_input_filesc   sY     h%=#2(<  $%78
 
 
 MMOO	r   NNN)r   NNN)__name__
__module____qualname__r   strintr   r   dictr   __call__r6   listr9   r(   r   r   r   r      s        #"!	/ /sm/ sm/ c]	/ / / /8 HL: :: :7?}:	d: : : :8 H
D 
 
 
 
 *..2 !# 'sm 
	     r   r   c                      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dS )PathwayVectorClientz9
    VectorStore connecting to Pathway Vector Store.
    Nr   r   r   r   c                 2    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   s       r   r   zPathwayVectorClient.__init__   s      )tS9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   rH   rI   rJ   s       r   	add_textszPathwayVectorClient.add_texts   s     "
 
 	
r   	embeddingc                      t          d          )Nz>Pathway vector store does not support initializing from_texts.rL   )clsrH   rO   rI   rJ   s        r   
from_textszPathwayVectorClient.from_texts   s     "L
 
 	
r      r   r   c                     |                     dd           }|rt          j        d|           |                     |||          }d |D             S )Nr   zBUnknown kwargs passed to PathwayVectorClient.similarity_search: %sr   r   r   c                 H    g | ]}t          |d          |d                    S )textmetadatapage_contentrX   r	   .0rets     r   
<listcomp>z9PathwayVectorClient.similarity_search.<locals>.<listcomp>   s:     
 
 
MPH#f+JHHH
 
 
r   )poploggingwarningrG   )r   r   r   rJ   r   retss         r   similarity_searchz%PathwayVectorClient.similarity_search   st     !**%6== 	OT   {{!_{MM
 
TX
 
 
 	
r   r   c                 J    |                      |||          }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.
        rU   c                 X    g | ]'}t          |d          |d                   |d         f(S )rW   rX   rY   r'   r	   r[   s     r   r^   zDPathwayVectorClient.similarity_search_with_score.<locals>.<listcomp>   sF     
 
 
 3v;ZIII3v;W
 
 
r   )rG   )r   r   r   r   rb   s        r   similarity_search_with_scorez0PathwayVectorClient.similarity_search_with_score   s<    & {{!_{MM
 

 
 
 	
r   c                     | j         S N)_cosine_relevance_score_fnr   s    r   _select_relevance_score_fnz.PathwayVectorClient._select_relevance_score_fn   s    ..r   c                 4    | j                                         S )z.Fetch basic statistics about the Vector Store.)rG   r6   rj   s    r   r6   z.PathwayVectorClient.get_vectorstore_statistics   s    {55777r   r7   c                 8    | j                             ||          S )z'List files indexed by the Vector Store.)rG   r9   )r   r   r7   s      r   r9   z#PathwayVectorClient.get_input_files   s     {**?<PQQQr   r:   rh   )rS   )rS   Nr;   )r<   r=   r>   __doc__r   r?   r@   r   r   r   rA   r   rN   classmethodr   rR   r
   rc   r   floatrf   r   rk   r6   rC   r9   r(   r   r   rE   rE      s[         #"!	: :sm: sm: c]	:
 
: : : :* +/

 

}

 DJ'

 	


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

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

 
eHeO$	%
 
 
 
4/HeWe^,D / / / /8D 8 8 8 8 *..2R R!#R 'smR 
	R R R R R Rr   rE   )rn   r.   r`   typingr   r   r   r   r   r   r,   langchain_core.documentsr
   langchain_core.embeddingsr   langchain_core.vectorstoresr   r   rE   r(   r   r   <module>ru      s       A A A A A A A A A A A A A A A A  - - - - - - 0 0 0 0 0 0 3 3 3 3 3 3
_ _ _ _ _ _ _ _DeR eR eR eR eR+ eR eR eR eR eRr   