a
    bgc                     @  s   d Z ddlmZ ddlZddlZddlZddlZddlmZm	Z	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 eeZG d	d
 d
eZdZG dd dZdS )z,Module providing Infinispan as a VectorStore    )annotationsN)AnyIterableListOptionalTupleTypeUnioncast)Response)Document)
Embeddings)VectorStorec                   @  s  e Zd ZdZdQddddddZd	d	d
ddZddd
ddZd	dddddZdddddZddddZ	dRdddddZ
ddd d!Zddd"d#Zd$dd%d&Zddd'd(Zddd)d*ZdSd+d,d-dd.d/d0d1ZdTdddd3d4d5d6ZdUdddd7d4d8d9ZdVd:ddd3d;d<d=ZdWd:dd7d>d?d@Zdd7dAdBdCZd	ddDdEdFdGZdDddHdIZedXdKd.dLd,ddMdMdd dN	dOdPZdS )YInfinispanVSa  `Infinispan` VectorStore interface.

        This class exposes the method to present Infinispan as a
        VectorStore. It relies on the Infinispan class (below) which takes care
        of the REST interface with the server.

    Example:
        ... code-block:: python
            from langchain_community.vectorstores import InfinispanVS
            from mymodels import RGBEmbeddings
            ...
            vectorDb = InfinispanVS.from_documents(docs,
                            embedding=RGBEmbeddings(),
                            output_fields=["texture", "color"],
                            lambda_key=lambda text,meta: str(meta["_key"]),
                            lambda_content=lambda item: item["color"])

        or an empty InfinispanVS instance can be created if preliminary setup
        is required before populating the store

        ... code-block:: python
            from langchain_community.vectorstores import InfinispanVS
            from mymodels import RGBEmbeddings
            ...
            ispnVS = InfinispanVS()
            # configure Infinispan here
            # i.e. create cache and schema

            # then populate the store
            vectorDb = InfinispanVS.from_documents(docs,
                            embedding=RGBEmbeddings(),
                            output_fields: ["texture", "color"],
                            lambda_key: lambda text,meta: str(meta["_key"]),
                            lambda_content: lambda item: item["color"])
    NzOptional[Embeddings]zOptional[List[str]]r   )	embeddingidskwargsc                   s   t f i | _| _t jdd _t jdd _| _ jdd _ jdkrp jdd _nt	
dt  jd	d _ jdkr jd
d _nt	
dt  jd fdd _ jd fdd _ jd _| _dS )at  
        Parameters
        ----------
        cache_name: str
            Embeddings cache name. Default "vector"
        entity_name: str
            Protobuf entity name for the embeddings. Default "vector"
        text_field: str
            Protobuf field name for text. Default "text"
        vector_field: str
            Protobuf field name for vector. Default "vector"
        lambda_content: lambda
            Lambda returning the content part of an item. Default returns text_field
        lambda_metadata: lambda
            Lambda returning the metadata part of an item. Default returns items
            fields excepts text_field, vector_field, _type
        output_fields: List[str]
            List of fields to be returned from item, if None return all fields.
            Default None
        kwargs: Any
            Rest of arguments passed to Infinispan. See docs
cache_namevectorentity_nameZ	textfield Z
text_fieldtextz9`textfield` is deprecated. Please use `text_field` param.ZvectorfieldZvector_fieldz=`vectorfield` is deprecated. Please use `vector_field` param.Zlambda_contentc                   s
     | S N)_default_contentitemself {/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/vectorstores/infinispanvs.py<lambda>i       z'InfinispanVS.__init__.<locals>.<lambda>Zlambda_metadatac                   s
     | S r   )_default_metadatar   r   r   r   r    l   r!   Zoutput_fieldsN)
Infinispanispn_configurationstrget_cache_name_entity_name
_embedding
_textfieldwarningswarnDeprecationWarning_vectorfield_to_content_to_metadata_output_fields_ids)r   r   r   r   r   r   r   __init__8   s6    

zInfinispanVS.__init__dict)r   returnc                 C  s4   t |}|| jd  || jd  |dd  |S )N_type)r5   popr/   r+   )r   r   metar   r   r   r"   q   s
    zInfinispanVS._default_metadatazdict[str, Any]c                 C  s   | | jS r   )r'   r+   )r   r   r   r   r   r   x   s    zInfinispanVS._default_contentintr&   )templ	dimensionr6   c                 C  s  d}|| j || jf }d}| D ]\}}t|trR|d| d t| d 7 }nt|trz|d| d t| d 7 }nt|tr|d| d t| d 7 }n\t|tr|d| d t| d 7 }n4t|tr|d	| d t| d 7 }nt	d
| |d7 }q"|d7 }|S )NzV
/**
* @Indexed
*/
message %s {
/**
* @Vector(dimension=%d)
*/
repeated float %s = 1;
   zoptional string z = z;
zoptional int64 zoptional double zoptional bytes zoptional bool zEUnable to build proto schema for metadata. Unhandled type for field:    z}
)
r)   r/   items
isinstancer&   r:   floatbytesbool	Exception)r   r;   r<   Zmetadata_proto_tplZmetadata_protoidxfvr   r   r   schema_builder{   s4    






zInfinispanVS.schema_builderr   )protor6   c                 C  s   | j | jd |S )zDeploy the schema for the vector db
        Args:
            proto(str): protobuf schema
        Returns:
            An http Response containing the result of the operation
        .proto)r$   schema_postr)   )r   rI   r   r   r   schema_create   s    zInfinispanVS.schema_create)r6   c                 C  s   | j | jd S )zDelete the schema for the vector db
        Returns:
            An http Response containing the result of the operation
        rJ   )r$   schema_deleter)   r   r   r   r   rM      s    zInfinispanVS.schema_deleter   )configr6   c                 C  s&   |dkrd| j  d }| j| j|S )zCreate the cache for the vector db
        Args:
            config(str): configuration of the cache.
        Returns:
            An http Response containing the result of the operation
        r   aZ  
            {
  "distributed-cache": {
    "owners": "2",
    "mode": "SYNC",
    "statistics": true,
    "encoding": {
      "media-type": "application/x-protostream"
    },
    "indexing": {
      "enabled": true,
      "storage": "filesystem",
      "startup-mode": "AUTO",
      "indexing-mode": "AUTO",
      "indexed-entities": [
        "z"
      ]
    }
  }
}
)r)   r$   
cache_postr(   )r   rN   r   r   r   cache_create   s    zInfinispanVS.cache_createc                 C  s   | j | jS )zDelete the cache for the vector db
        Returns:
            An http Response containing the result of the operation
        )r$   cache_deleter(   r   r   r   r   rQ      s    zInfinispanVS.cache_deletec                 C  s   | j | jS )zClear the cache for the vector db
        Returns:
            An http Response containing the result of the operation
        )r$   cache_clearr(   r   r   r   r   rR      s    zInfinispanVS.cache_clearrC   c                 C  s   | j | jS )zOChecks if the cache exists
        Returns:
            true if exists
        )r$   cache_existsr(   r   r   r   r   rS      s    zInfinispanVS.cache_existsc                 C  s   | j | jS )zClear the index for the vector db
        Returns:
            An http Response containing the result of the operation
        )r$   index_clearr(   r   r   r   r   cache_index_clear   s    zInfinispanVS.cache_index_clearc                 C  s   | j | jS )z{Rebuild the for the vector db
        Returns:
            An http Response containing the result of the operation
        )r$   index_reindexr(   r   r   r   r   cache_index_reindex   s    z InfinispanVS.cache_index_reindexzIterable[str]zOptional[List[dict]]zOptional[List[float]]	List[str])texts	metadataslast_vectorr   r6   c                 K  s   g }t |}|r|  | j|}|r2|| |sDdd |D }| jpVdd |D }t t|||}	|	D ]J\}
}}d| j| j|i}|	|
 t
|}| j||| j || ql|S )Nc                 S  s   g | ]}i qS r   r   .0_r   r   r   
<listcomp>  r!   z*InfinispanVS.add_texts.<locals>.<listcomp>c                 S  s   g | ]}t t qS r   )r&   uuiduuid4r\   r   r   r   r_     r!   r7   )listr8   r*   Zembed_documentsappendr3   zipr)   r/   updatejsondumpsr$   putr(   )r   rY   rZ   r[   r   resultZtexts_lZembedsr   Z
data_inputmetadataembedkeydataZdata_strr   r   r   	add_texts   s$    


zInfinispanVS.add_texts   zList[Document])querykr   r6   c                 K  s   | j ||d}dd |D S )z"Return docs most similar to query.)rp   rq   c                 S  s   g | ]\}}|qS r   r   r]   docr^   r   r   r   r_     r!   z2InfinispanVS.similarity_search.<locals>.<listcomp>)similarity_search_with_score)r   rp   rq   r   	documentsr   r   r   similarity_search  s    zInfinispanVS.similarity_searchzList[Tuple[Document, float]]c                 K  s   | j |}| j||d}|S )a  Perform a search on a query string and return results with score.

        Args:
            query (str): The text being searched.
            k (int, optional): The amount of results to return. Defaults to 4.

        Returns:
            List[Tuple[Document, float]]
        )r   rq   )r*   embed_query&similarity_search_with_score_by_vector)r   rp   rq   r   rk   ru   r   r   r   rt     s    z)InfinispanVS.similarity_search_with_scorezList[float])r   rq   r   r6   c                 K  s   |  ||}dd |D S )Nc                 S  s   g | ]\}}|qS r   r   rr   r   r   r   r_   +  r!   z<InfinispanVS.similarity_search_by_vector.<locals>.<listcomp>)rx   )r   r   rq   r   resr   r   r   similarity_search_by_vector'  s    z(InfinispanVS.similarity_search_by_vector)r   rq   r6   c                 C  s   | j du r:d| j d | j d t| d t| }nld}| j dd D ]}|d | d	 }qL|d | j d  }|d
 | j d | j d t| d t| }| j|| j}t	|j
}| |S )a$  Return docs most similar to embedding vector.

        Args:
            embedding: Embedding to look up documents similar to.
            k: Number of Documents to return. Defaults to 4.

        Returns:
            List of pair (Documents, score) most similar to the query vector.
        Nzselect v, score(v) from z v where v.z <-> ~zselect zv.,z, score(v) from )r2   r)   r/   rf   rg   r&   r$   	req_queryr(   loadsr   _query_result_to_docs)r   r   rq   	query_strZ
query_projfieldZ	query_resri   r   r   r   rx   -  sT    
z3InfinispanVS.similarity_search_with_score_by_vector)ri   r6   c                   st   g }|d D ]b}|d pi  | j d u r0 d }n fdd| j D }t| || |d}|| d f q|S )Nhitshit*c                   s   i | ]}|  |qS r   )r'   )r]   rl   r   r   r   
<dictcomp>a  r!   z6InfinispanVS._query_result_to_docs.<locals>.<dictcomp>)Zpage_contentrj   zscore())r2   r   r0   r1   rc   )r   ri   ru   rowentityrs   r   r   r   r   X  s    

z"InfinispanVS._query_result_to_docsNone)rj   r<   r6   c                 C  sz   |  ||}| |}|j| jjjks.J dt|jd d u sFJ | 	 sv| 
 }|j| jjjksnJ d|   d S )Nz)Unable to create schema. Already exists? errorz(Unable to create cache. Already exists? )rH   rL   status_coder$   CodesOKrf   r   r   rS   rP   rU   )r   rj   r<   schemaoutputr   r   r   	configurei  s    
zInfinispanVS.configurec                 C  s   |    |   d S r   )rM   rQ   r   r   r   r   config_clearz  s    zInfinispanVS.config_clearTzType[InfinispanVS]r   zOptional[bool])	clsrY   r   rZ   r   	clear_oldauto_configr   r6   c           
      K  s   | f ||d|}|rnt |p g dkrn|r4|  ||t |d  }	ttt |}||d t |	 n"|rz|  ||t |d  }	|r|j|||	d |S )a  Return VectorStore initialized from texts and embeddings.

        In addition to parameters described by the super method, this
        implementation provides other configuration params if different
        configuration from default is needed.

        Parameters
        ----------
        ids : List[str]
            Additional list of keys associated to the embedding. If not
            provided UUIDs will be generated
        clear_old : bool
            Whether old data must be deleted. Default True
        auto_config: bool
            Whether to do a complete server setup (caches,
            protobuf definition...). Default True
        kwargs: Any
            Rest of arguments passed to InfinispanVS. See docs)r   r   r   r>   )r   )	lenr   rw   r
   r   r5   r   rR   rn   )
r   rY   r   rZ   r   r   r   r   ZinfinispanvsZvecr   r   r   
from_texts~  s    zInfinispanVS.from_texts)NN)r   )NN)ro   )ro   )ro   )ro   )NNTT)__name__
__module____qualname____doc__r4   r"   r   rH   rL   rM   rP   rQ   rR   rS   rU   rW   rn   rv   rt   rz   rx   r   r   r   classmethodr   r   r   r   r   r      sF   &  9%	#
      +    r   
   c                   @  sV  e Zd ZdZddddgdddddf	dddd	ddd
d
d
dd
ddZd9ddd
ddddZd:ddd
ddddZd;ddd
ddddZdddddddZdddddddZ	dddddd Z
dddd!d"d#Zdddd$d%d&Zddd'd(d)Zddd'd*d+Zddd,d-d.Zdd
d,d/d0Zdd
d1d2d3Zddd,d4d5Zddd,d6d7Zd8S )<r#   a>  Helper class for `Infinispan` REST interface.

    This class exposes the Infinispan operations needed to
    create and set up a vector db.

    You need a running Infinispan (15+) server without authentication.
    You can easily start one, see:
    https://github.com/rigazilla/infinispan-vector#run-infinispan
    httpr   z127.0.0.1:11222z/rest/v2/cachesz/rest/v2/schemasTr&   rX   rC   r   )
r   userpasswordhosts	cache_url
schema_urluse_post_for_queryhttp2verifyr   c
                 K  s   zddl }W n ty&   tdY n0 |j| _|
| _|| _|| _|| _|d | _| jd | j | _	|| _
|| _|| _|| _| jr| jr| jdkr|j| j| jd}n| j| jf}|j| j| j ||	d| _n|j| j| j |	d| _dS )	aK  
        Parameters
        ----------
        schema: str
            Schema for HTTP request: "http" or "https". Default "http"
        user, password: str
            User and password if auth is required. Default None
        hosts: List[str]
            List of server addresses. Default ["127.0.0.1:11222"]
        cache_url: str
            URL endpoint for cache API. Default "/rest/v2/caches"
        schema_url: str
            URL endpoint for schema API. Default "/rest/v2/schemas"
        use_post_for_query: bool
            Whether POST method should be used for query. Default True
        http2: bool
            Whether HTTP/2 protocol should be used. `pip install "httpx[http2]"` is
            needed for HTTP/2. Default True
        verify:  bool
            Whether TLS certificate must be verified. Default True
        r   NzCould not import httpx python package. Please install it with `pip install httpx`or `pip install "httpx[http2]"` if you need HTTP/2.z://r   )usernamer   )r   http1authr   )r   r   r   )httpxImportErrorcodesr   r%   Z_schema_userZ	_passwordZ_host_default_node
_cache_url_schema_url_use_post_for_queryZ_http2Z
DigestAuthZClient_h2c)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r4     sD    #



zInfinispan.__init__Fr   )rp   r   localr6   c                 C  s"   | j r| |||S | |||S )a  Request a query
        Args:
            query(str): query requested
            cache_name(str): name of the target cache
            local(boolean): whether the query is local to clustered
        Returns:
            An http Response containing the result set or errors
        )r   _query_post
_query_get)r   rp   r   r   r   r   r   r~     s    	zInfinispan.req_query)r   r   r   r6   c                 C  sN   | j | j d | d t| }d|i}t|}| jj||dditd}|S )N/z?action=search&local=rp   Content-Typeapplication/jsoncontentheaderstimeout)r   r   r&   rf   rg   r   postREST_TIMEOUT)r   r   r   r   api_urlrm   Z	data_jsonresponser   r   r   r     s*    
zInfinispan._query_postc                 C  s<   | j | j d | d | d t| }| jj|td}|S )Nr   z?action=search&query=z&local=r   )r   r   r&   r   r'   r   )r   r   r   r   r   r   r   r   r   r   '  s$    
zInfinispan._query_get)rl   rm   r   r6   c                 C  s8   | j | j d | d | }| jj||dditd}|S )a  Post an entry
        Args:
            key(str): key of the entry
            data(str): content of the entry in json format
            cache_name(str): target cache
        Returns:
            An http Response containing the result of the operation
        r   r   r   r   r   r   r   r   r   r   rl   rm   r   r   r   r   r   r   r   7  s    	zInfinispan.postc                 C  s8   | j | j d | d | }| jj||dditd}|S )a  Put an entry
        Args:
            key(str): key of the entry
            data(str): content of the entry in json format
            cache_name(str): target cache
        Returns:
            An http Response containing the result of the operation
        r   r   r   r   )r   r   r   rh   r   r   r   r   r   rh   I  s    	zInfinispan.put)rl   r   r6   c                 C  s6   | j | j d | d | }| jj|dditd}|S )zGet an entry
        Args:
            key(str): key of the entry
            cache_name(str): target cache
        Returns:
            An http Response containing the entry or errors
        r   r   r   )r   r   )r   r   r   r'   r   )r   rl   r   r   r   r   r   r   r'   [  s
    
zInfinispan.get)namerI   r6   c                 C  s*   | j | j d | }| jj||td}|S )zDeploy a schema
        Args:
            name(str): name of the schema. Will be used as a key
            proto(str): protobuf schema
        Returns:
            An http Response containing the result of the operation
        r   )r   r   )r   r   r   r   r   )r   r   rI   r   r   r   r   r   rK   i  s    zInfinispan.schema_post)r   rN   r6   c                 C  s0   | j | j d | }| jj||dditd}|S )zCreate a cache
        Args:
            name(str): name of the cache.
            config(str): configuration of the cache.
        Returns:
            An http Response containing the result of the operation
        r   r   r   r   r   )r   r   rN   r   r   r   r   r   rO   u  s    zInfinispan.cache_post)r   r6   c                 C  s(   | j | j d | }| jj|td}|S )zDelete a schema
        Args:
            name(str): name of the schema.
        Returns:
            An http Response containing the result of the operation
        r   r   )r   r   r   deleter   r   r   r   r   r   r   r   rM     s    zInfinispan.schema_deletec                 C  s(   | j | j d | }| jj|td}|S )zDelete a cache
        Args:
            name(str): name of the cache.
        Returns:
            An http Response containing the result of the operation
        r   r   )r   r   r   r   r   r   r   r   r   rQ     s    zInfinispan.cache_delete)r   r6   c                 C  s,   | j | j d | d }| jj|td}|S )zClear a cache
        Args:
            cache_name(str): name of the cache.
        Returns:
            An http Response containing the result of the operation
        r   ?action=clearr   r   )r   r   r   r   r   r   r   rR     s    zInfinispan.cache_clearc                 C  s"   | j | j d | d }| |S )zCheck if a cache exists
        Args:
            cache_name(str): name of the cache.
        Returns:
            True if cache exists
        r   r   )r   r   resource_existsr   r   r   r   r   r   rS     s    zInfinispan.cache_exists)r   r6   c                 C  s   | j j|td}|j| jjkS )zCheck if a resource exists
        Args:
            api_url(str): url of the resource.
        Returns:
            true if resource exists
        r   )r   headr   r   r   r   )r   r   r   r   r   r   r     s    zInfinispan.resource_existsc                 C  s(   | j | j d | d }| jj|tdS )zClear an index on a cache
        Args:
            cache_name(str): name of the cache.
        Returns:
            An http Response containing the result of the operation
        r   z/search/indexes?action=clearr   r   r   r   r   r   rT     s    zInfinispan.index_clearc                 C  s(   | j | j d | d }| jj|tdS )zRebuild index on a cache
        Args:
            cache_name(str): name of the cache.
        Returns:
            An http Response containing the result of the operation
        r   z/search/indexes?action=reindexr   r   r   r   r   r   rV     s    zInfinispan.index_reindexN)F)F)F)r   r   r   r   r4   r~   r   r   r   rh   r'   rK   rO   rM   rQ   rR   rS   r   rT   rV   r   r   r   r   r#     s8   "L  
r#   )r   
__future__r   rf   loggingr`   r,   typingr   r   r   r   r   r   r	   r
   r   r   Zlangchain_core.documentsr   Zlangchain_core.embeddingsr   Zlangchain_core.vectorstoresr   	getLoggerr   loggerr   r   r#   r   r   r   r   <module>   s"   (
   