
    3fi                       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	 ddl
mZmZmZ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mZ dd
lm Z m!Z! ddl"m#Z# ddl$m%Z% ddl&m'Z(  edd      Z)erddl*m+Z+ ddl,m-Z-  ej\                  e/      Z0 G d d      Z1ddZ2ddZ3ddZ4d dZ5d!dZ6d"dZ7d"dZ8 e        G d de             Z9y)#z3Apache Cassandra DB graph vector store integration.    )annotationsN)asdictis_dataclass)TYPE_CHECKINGAnyAsyncIterableIterableListOptionalSequenceTupleTypeTypeVarcast)beta)Document)override)GraphVectorStoreNode)METADATA_LINKS_KEYLink)	MmrHelper)	SetupMode)	CassandraCGVSTCassandraGraphVectorStore)bound)Session)
Embeddingsc                  4    e Zd ZU ded<   ded<   ded<   d	dZy)
AdjacentNodestrid
list[Link]linkslist[float]	embeddingc                ^    |j                   xs d| _         |j                  | _        || _        y)zCreate an Adjacent Node. N)r#   r%   r'   )selfnoder'   s      n/var/www/auto_recruiter/arenv/lib/python3.12/site-packages/langchain_community/graph_vectorstores/cassandra.py__init__zAdjacentNode.__init__1   s#    ''-RZZ
"    N)r+   r   r'   r&   returnNone)__name__
__module____qualname____annotations__r-    r.   r,   r!   r!   ,   s    G#r.   r!   c                b     G d dt         j                        }t        j                  | |      S )Nc                        e Zd Zd fdZ xZS )+_serialize_links.<locals>.SetAndLinkEncoderc                    t        |t              st        |      rt        |      S t        |t              rt        |      S t        |   |      S N)
isinstancetyper   r   r	   listsuperdefault)r*   obj	__class__s     r,   r?   z3_serialize_links.<locals>.SetAndLinkEncoder.default:   sC    c4(\#->c{"#x(Cy  7?3''r.   )r@   r   r/   r   )r1   r2   r3   r?   __classcell__)rA   s   @r,   SetAndLinkEncoderr8   9   s    	( 	(r.   rC   )cls)jsonJSONEncoderdumps)r%   rC   s     r,   _serialize_linksrH   8   s(    	(D,, 	( ::e!233r.   c           	         t        t        t        t        t        f      t        j                  | xs d            D ch c]  }t        |d   |d   |d          c}S c c}w )Nz[]kind	directiontag)rJ   rK   rL   )r   r=   dictr"   r   rE   loadsr   )	json_bloblinks     r,   _deserialize_linksrQ   G   s\     d38n-tzz):Kt/LM 	$v,${*;eM  s   Ac                :    d| j                    d| j                   S )Nzlink::)rJ   rL   rP   s    r,   _metadata_link_keyrU   N   s    499+Qtxxj))r.   c                      y)NrP   r5   r5   r.   r,   _metadata_link_valuerW   R   s    r.   c                    | j                   j                         }t        |j                  t                    }||t        <   t        | j                  | j                  |t        |            S )N)r#   textmetadatar%   )	rZ   copyrQ   getr   r   r#   page_contentr=   )docrZ   r%   s      r,   _doc_to_noder_   V   sZ    ||  "Hx||,>?@E#(H 665k	 r.   c                ^    | j                   D ch c]  }|j                  dv s| c}S c c}w )Ninbidirr%   rK   r+   rP   s     r,   _incoming_linksrf   c   s%    !ZZMT4>>_+LDMMM   **c                ^    | j                   D ch c]  }|j                  dv s| c}S c c}w )N)outrc   rd   re   s     r,   _outgoing_linksrj   g   s&    !ZZNT4>>=M+MDNNNrg   c            
         e Zd Z	 	 	 	 d,dej                  dd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d-dZeed.d              Z	 	 d/	 	 	 	 	 d0dZ	d1dZ
d2dZ	 	 	 	 d3d	Ze	 	 	 	 	 	 d4d
       Ze	 	 	 	 	 	 d5d       Ze	 	 d6	 	 	 	 	 	 	 	 	 d7d       Ze	 	 d6	 	 	 	 	 	 	 	 	 d7d       Ze	 	 d6	 	 	 	 	 	 	 	 	 d8d       Ze	 	 d6	 	 	 	 	 	 	 	 	 d8d       Z	 	 d9	 	 	 	 	 d:dZ	 	 d9	 	 	 	 	 d:dZd;dZd;dZd<dZedddddd ed      dd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d=d       Zedddddd ed      dd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d>d       Zedddd 	 	 	 	 	 	 	 	 	 	 	 d?d!       Zedddd 	 	 	 	 	 	 	 	 	 	 	 d@d"       ZdAd#Z	 	 d/	 	 	 	 	 	 	 	 	 	 	 dBd$Ze	 	 d/	 	 	 	 	 	 	 dCd%       Z e!	 dDdddddddd&	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dEd'       Z"e!	 dDdddddddd&	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dEd(       Z#e	 dD	 	 	 	 	 dFd)       Z$e!dddddddd&	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dGd*       Z%e!dddddddd&	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dGd+       Z&y)Hr   Nr)   )body_index_options
setup_modemetadata_deny_listc                   || _         |g }|j                  t               t        |||||||d|f      | _        | j                  j
                  }	|	j                  d| d| d      | _        y)a  Apache Cassandra(R) for graph-vector-store workloads.

        To use it, you need a recent installation of the `cassio` library
        and a Cassandra cluster / Astra DB instance supporting vector capabilities.

        Example:
            .. code-block:: python

                    from langchain_community.graph_vectorstores import
                        CassandraGraphVectorStore
                    from langchain_openai import OpenAIEmbeddings

                    embeddings = OpenAIEmbeddings()
                    session = ...             # create your Cassandra session object
                    keyspace = 'my_keyspace'  # the keyspace should exist already
                    table_name = 'my_graph_vector_store'
                    vectorstore = CassandraGraphVectorStore(
                        embeddings,
                        session,
                        keyspace,
                        table_name,
                    )

        Args:
            embedding: Embedding function to use.
            session: Cassandra driver session. If not provided, it is resolved from
                cassio.
            keyspace: Cassandra keyspace. If not provided, it is resolved from cassio.
            table_name: Cassandra table (required).
            ttl_seconds: Optional time-to-live for the added texts.
            body_index_options: Optional options used to create the body index.
                Eg. body_index_options = [cassio.table.cql.STANDARD_ANALYZER]
            setup_mode: mode used to create the Cassandra table (SYNC,
                ASYNC or OFF).
            metadata_deny_list: Optional list of metadata keys to not index.
                i.e. to fine-tune which of the metadata fields are indexed.
                Note: if you plan to have massive unique text metadata entries,
                consider not indexing them for performance
                (and to overcome max-length limitations).
                Note: the `metadata_indexing` parameter from
                langchain_community.utilities.cassandra.Cassandra is not
                exposed since CassandraGraphVectorStore only supports the
                deny_list option.
        N	deny_list)r'   sessionkeyspace
table_namettl_secondsrl   rm   metadata_indexingz
            INSERT INTO .z{ (
                row_id, body_blob, vector, attributes_blob, metadata_s
            ) VALUES (?, ?, ?, ?, ?)
            )r'   appendr   CassandraVectorStorevector_storerq   prepare_insert_node)
r*   r'   rq   rr   rs   rt   rl   rm   rn   store_sessions
             r,   r-   z"CassandraGraphVectorStore.__init__m   s    p #%!#!!"450!#1!*,>?	
 "&!2!2!:!:)11!
!J< 0
r.   c                    | j                   S r:   )r'   )r*   s    r,   
embeddingsz$CassandraGraphVectorStore.embeddings   s     ~~r.   c                l    ||xs i S |i n|j                         }t               |t        |      <   |S )NrT   )r[   rW   rU   )r*   rZ   outgoing_linkmetadata_filters       r,   _get_metadata_filterz.CassandraGraphVectorStore._get_metadata_filter   s?    
  >r! ( 0"hmmoBVBX*>?r.   c                   t        |j                  j                  t                    }||j                  t        <   |D cg c]  }|j                  dv rt        |       c}D ]  }||j                  v s|j                  |=   |S c c}w )zRestores the links in the document by deserializing them from metadata.

        Args:
            doc: A single Document

        Returns:
            The same Document with restored links.
        ra   rT   )rQ   rZ   r\   r   rK   rU   )r*   r^   r%   rP   incoming_link_keys        r,   _restore_linksz(CassandraGraphVectorStore._restore_links   s     #3<<#3#34F#GH+0'(
 "
~~0 D)"
 	4
 !CLL0LL!23	4 
"
s    !Bc                    |j                   j                         }t        |j                        |t        <   t        |      D ]  }t               |t        |      <    |S )Nr+   rT   )rZ   r[   rH   r%   r   rf   rW   rU   )r*   r+   rZ   incoming_links       r,    _get_node_metadata_for_insertionz:CassandraGraphVectorStore._get_node_metadata_for_insertion   sX    ==%%''7

'C#$ -$7 	VM?S?UH'];<	Vr.   c                   g }g }|D ]y  }|j                   st        j                  d      n|j                   }t        |j                  | j                  |      |      }|j                  |       |j                  |       { ||fS )N   r   )r]   rZ   r#   )r#   secrets	token_hexr   rY   r   rw   )r*   nodesdocsidsr+   node_idr^   s          r,   _get_docs_for_insertionz1CassandraGraphVectorStore._get_docs_for_insertion   s      		 D26''g''*twwG!YY>>D>IC
 KKJJw		  c{r.   c                f    | j                  |      \  }}| j                  j                  ||      S )Add nodes to the graph store.

        Args:
            nodes: the nodes to add.
            **kwargs: Additional keyword arguments.
        r   r   )r   ry   add_documents)r*   r   kwargsr   r   s        r,   	add_nodesz#CassandraGraphVectorStore.add_nodes  s7     222?s  ..t.==r.   c                  K   | j                  |      \  }}| j                  j                  ||       d{   D ]  }| 	 y7 w)r   r   r   N)r   ry   aadd_documents)r*   r   r   r   r   inserted_ids         r,   
aadd_nodesz$CassandraGraphVectorStore.aadd_nodes  sP      222?s!%!2!2!A!A$C!A!PP 	K	Ps   6A
AA
   c                     | j                   j                  d|||d|D cg c]  }| j                  |       c}S c c}w )Y  Retrieve documents from this graph store.

        Args:
            query: The query string.
            k: The number of Documents to return. Defaults to 4.
            filter: Optional metadata to filter the results.
            **kwargs: Additional keyword arguments.

        Returns:
            Collection of retrieved documents.
        querykfilterr5   )ry   similarity_searchr   r*   r   r   r   r   r^   s         r,   r   z+CassandraGraphVectorStore.similarity_search!  sZ    * ;t((::  	
 $
 	
 
   >c                   K    | j                   j                  d|||d| d{   D cg c]  }| j                  |       c}S 7 !c c}w w)r   r   Nr5   )ry   asimilarity_searchr   r   s         r,   r   z,CassandraGraphVectorStore.asimilarity_search>  si     * BT..AA  	 
 $
 	

    $AAAA
A
Ac                     | j                   j                  |f||d|D cg c]  }| j                  |       c}S c c}w )  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.
            filter: Filter on the metadata to apply.
            **kwargs: Additional arguments are ignored.

        Returns:
            The list of Documents most similar to the query vector.
        r   r   )ry   similarity_search_by_vectorr   r*   r'   r   r   r   r^   s         r,   r   z5CassandraGraphVectorStore.similarity_search_by_vector[  s[    * Et((DD 	
 $
 	
 
r   c                   K    | j                   j                  |f||d| d{   D cg c]  }| j                  |       c}S 7 !c c}w w)r   r   N)ry   asimilarity_search_by_vectorr   r   s         r,   r   z6CassandraGraphVectorStore.asimilarity_search_by_vectorx  si     * LT..KK 	 
 $
 	

r   c                    | j                   j                  |xs i |      D cg c]  }| j                  |       c}S c c}w )Get documents via a metadata search.

        Args:
            filter: the metadata to query for.
            n: the maximum number of documents to return.
        r   n)ry   metadata_searchr   r*   r   r   r^   s       r,   r   z)CassandraGraphVectorStore.metadata_search  sO     ((88| 9 
 $
 	
 
s   ?c                   K   | j                   j                  |xs i |       d{   D cg c]  }| j                  |       c}S 7 !c c}w w)r   r   N)ry   ametadata_searchr   r   s       r,   r   z*CassandraGraphVectorStore.ametadata_search  s^      "..??| @  
 $
 	

s    %AA	AAAAc                d    | j                   j                  |      }|| j                  |      S dS zRetrieve a single document from the store, given its document ID.

        Args:
            document_id: The document ID

        Returns:
            The the document if it exists. Otherwise None.
        document_idN)ry   get_by_document_idr   r*   r   r^   s      r,   r   z,CassandraGraphVectorStore.get_by_document_id  s7     22{2K+.?t""3'DDr.   c                   K   | j                   j                  |       d{   }|| j                  |      S dS 7 wr   )ry   aget_by_document_idr   r   s      r,   r   z-CassandraGraphVectorStore.aget_by_document_id  sD      %%99k9RR+.?t""3'DD Ss    ><>c                X    | j                   j                  |      }|yt        |      S )zRetrieve a single node from the store, given its ID.

        Args:
            node_id: The node ID

        Returns:
            The the node if it exists. Otherwise None.
        r   Nr^   )ry   r   r_   )r*   r   r^   s      r,   get_nodez"CassandraGraphVectorStore.get_node  s0     22w2G;$$r.   r5      d   
   g      ?z-inf)initial_rootsr   depthfetch_k
adjacent_klambda_multscore_thresholdr   c          	       	K    j                   j                  |      t        |||      i t               i d	 fd}d	 fd}|r ||       d{    dkD  r |        d{    j	                         D ci c]  }|d }}t        |      D ]  }j                         }| n||   dz   }||k  s&j                  |      }|j                          j                  |	       d{   }j                  |       i }|D ]u  }|j                  vst        |      |j                  <   |j                   ||j                  <   ||j                  |j                  |dz         k  sg|||j                  <   w j                  |        t        j                   j"                  j$                        D ]Q  \  }}}|v r8 j'                  |         }||j(                  d	<   ||j(                  d
<   | Cd| }t+        |       y7 7 c c}w 7 1w)  Retrieve documents from this graph store using MMR-traversal.

        This strategy first retrieves the top `fetch_k` results by similarity to
        the question. It then selects the top `k` results based on
        maximum-marginal relevance using the given `lambda_mult`.

        At each step, it considers the (remaining) documents from `fetch_k` as
        well as any documents connected by edges to a selected document
        retrieved based on similarity (a "root").

        Args:
            query: The query string to search for.
            initial_roots: Optional list of document IDs to use for initializing search.
                The top `adjacent_k` nodes adjacent to each initial root will be
                included in the set of initial candidates. To fetch only in the
                neighborhood of these nodes, set `fetch_k = 0`.
            k: Number of Documents to return. Defaults to 4.
            fetch_k: Number of initial Documents to fetch via similarity.
                Will be added to the nodes adjacent to `initial_roots`.
                Defaults to 100.
            adjacent_k: Number of adjacent Documents to fetch.
                Defaults to 10.
            depth: Maximum depth of a node (number of edges) from a node
                retrieved via similarity. Defaults to 2.
            lambda_mult: Number between 0 and 1 that determines the degree
                of diversity among the results with 0 corresponding to maximum
                diversity and 1 to minimum diversity. Defaults to 0.5.
            score_threshold: Only documents with a score greater than or equal
                this threshold will be chosen. Defaults to -infinity.
            filter: Optional metadata to filter the results.
            **kwargs: Additional keyword arguments.
        )r   query_embeddingr   r   c                  K   j                  | D ci c]  }|t                c}       j                  |        d {   j                  	
       d {   }i }|D ]C  }|j                  vst        |      |j                  <   |j                  ||j                  <   E j                  |       y c c}w 7 7 gw)Nr%   r   
k_per_linkr   retrieved_docsr   )updateset_get_outgoing_links_get_adjacentr#   rj   r'   add_candidates)neighborhood
content_idadjacent_nodesnew_candidatesadjacent_noder   r   helperoutgoing_links_mapr   r   r*   visited_linkss        r,   fetch_neighborhoodzKCassandraGraphVectorStore.ammr_traversal_search.<locals>.fetch_neighborhood  s     
 %%5ABzSU"B #'":":<"HHM $(#5#5# /%- $6 $ N 68N!/ O ##+==;J*<&}'7'78 8E7N7NN=#3#34O !!.1/ C
 Is4   CB?C CCCC9ACCc                    K   j                   j                  
       d {   } i }| D ]3  \  }}}|vr||<   |	vst        |      }t        |      	|<   |||<   5 j	                  |       y 7 Pw)Nr'   r   r   r   )ry   .asimilarity_search_with_embedding_id_by_vectorr_   rj   r   )results
candidatesr^   r'   doc_idr+   r   r   r   r   r   r   r*   r   s         r,   fetch_initial_candidateszQCassandraGraphVectorStore.ammr_traversal_search.<locals>.fetch_initial_candidates=  s      ''VV-! W    24J*1 3&Y/-0N6*!33',D1@d1K&v.)2Jv&3 !!*-!s   "A8A6A84A8Nr      r   r   similarity_score	mmr_score"retrieved_docs should contain id: )r   Sequence[str]r/   r0   )r/   r0   )r'   embed_queryr   r   candidate_idsrangepop_bestpopdifference_updater   r   r#   rj   r\   r   zipselected_idsselected_similarity_scoresselected_mmr_scoresr   rZ   RuntimeError) r*   r   r   r   r   r   r   r   r   r   r   r   r   candidate_iddepths_selected_id
next_depthselected_outgoing_linksr   r   r   r   r   r   r^   msgr   r   r   r   r   s    `    ``  `                 @@@@@r,   ammr_traversal_searchz/CassandraGraphVectorStore.ammr_traversal_search  s    ^ ..44U;+#+	
 46#&5.0	2 	2>	. 	., $]333Q;*,,, 7=6J6J6LMl,/MM q 0	6A //+K",q0JE!
 +=*@*@*M' (99-H (,'9'91$3)!#1 (: ( " $$%<=!#%3 BM$''/AA?N!.@*=+;+;< <I;R;R}'7'78%

=3C3CUQY(OO 8BF=#3#34#B$ %%n5a0	6d 47--&&4
 	(/F$i
 ')).*@A3C/0,5[)	:6(C"3''	(u 4, N*"sV   A I+H4,I?H7 I
H: 2I;IH?*I:AIB&I7I:Ic               l    	
 d	
 fd}t        j                   |             S )r   c                 ~   K    j                   d
	d	} | 2 cg c3 d {   }|7 6 c}S c c}w w)N)	r   r   r   r   r   r   r   r   r   r5   )r  )
async_iterr^   r   r   r   r   r   r   r   r   r   r   r*   s     r,   collect_docszDCassandraGraphVectorStore.mmr_traversal_search.<locals>.collect_docs  s`     333 +%' / J *444#C4444s$   !=842
4848=r/   Iterable[Document]asynciorun)r*   r   r   r   r   r   r   r   r   r   r   r  s   ``````````` r,   mmr_traversal_searchz.CassandraGraphVectorStore.mmr_traversal_search  s"    `	5 	5 {{<>**r.   r   )r   r   r   c                 	
K   i i i 	d	 fd
d	 
fd j                   j                  ||      } 
d|       d{    D ]-  }|	v r j                  	|          d| }t        |       y7 7w)	  Retrieve documents from this knowledge store.

        First, `k` nodes are retrieved using a vector search for the `query` string.
        Then, additional nodes are discovered up to the given `depth` from those
        starting nodes.

        Args:
            query: The query string.
            k: The number of Documents to return from the initial vector search.
                Defaults to 4.
            depth: The maximum depth of edges to traverse. Defaults to 1.
            filter: Optional metadata to filter the results.
            **kwargs: Additional keyword arguments.

        Returns:
            Collection of retrieved documents.
        c           	       K   t               }|D ]  }|j                  |j                  vr||j                  <   | j                  |j                        k  sM| |j                  <   | k  sbt        |      }t	        |      D ].  }| j                  |      k  s| |<   |j                  |       0  |rg }|D ]T  }j                  |      }|j                  t        j                  j                  j                  |d                   V t        j                  |  d{   }	|	D cg c]  } | dz   |       }
}t        j                  |
  d{    yy7 :c c}w 7 w)	z1Recursively visit nodes and their outgoing links.Nr   r   rZ   r   i  r   r   dr   )r   r#   r\   r_   rj   addr   rw   r	  create_taskry   r   gather)r  r   outgoing_linksr^   r+   rP   metadata_search_tasksr   r   r   visit_target_tasksr   r   r   r*   visit_targetsvisited_idsr   s              r,   visit_nodesz@CassandraGraphVectorStore.atraversal_search.<locals>.visit_nodes  s     ),N =66%vv^314svv. KOOCFFE::./CFF+ u9#/C#8D )8T(B =#$(9(9$(F#F ;<M$$7$2$6$6t$<==, (*%%3 M&*&?&?!'&3 '@ 'O *00++ -->>'6$ ?  !(0E FF CJ&:>MAE5&" & nn&8999)  G& :sG   E3<E3E320E3#BE3/E*0E38E,E3#E1$E3,E3c                z   K   t               }|D ]g  }|j                  |j                  	vr|	|j                  <    j                  |j                        k  sM|j                  |j                         i |r|D cg c]  }|	v r  	|   g       }}|D cg c]5  }|	vr/t	        j
                  
j                  j                  |            7 }}t	        j                  |  d{   }|j                   fd|D               t	        j                  |  d{    yyc c}w c c}w 7 E7 w)z1Visit target nodes retrieved from outgoing links.Nr  r   c              3  8   K   | ]  }| |g        y w)Nr  r5   ).0new_docr  r  s     r,   	<genexpr>zUCassandraGraphVectorStore.atraversal_search.<locals>.visit_targets.<locals>.<genexpr>^  s+      (*  !7)44(s   )
r   r#   r\   r  r	  r  ry   r   r  extend)r  r   new_ids_at_next_depthr^   r   visit_node_tasksfetch_tasksnew_docsr   r   r*   r  r  s   `       r,   r  zBCassandraGraphVectorStore.atraversal_search.<locals>.visit_targets@  sS     %(E! :66%vv^314svv.KOOCFFE::-11#&&9: % #8$/  !>&+A*BC$  $ #8	 ^3	 ''))==&=Q  9@8T2T '' (#+(  nn&6777/ %$ 3U 8sF   D;<D;#D;D-D; :D2D;1D724D;&D9'D;9D;r   r   r  Nr   )r  intr   r  r/   r0   )ry   r   r   r   )r*   r   r   r   r   r   initial_docsr   r   r   r  r  r  r   s   `  ``    @@@@@r,   atraversal_searchz+CassandraGraphVectorStore.atraversal_search  s     N ') *, /12	: 2	:h$	8 $	8N ((:: ; 

 AL111! 	(F')).*@AA:6(C"3''	( 	2s   ABB
8Bc               X     d fd}t        j                   |             S )r  c                 t   K    j                   dd} | 2 cg c3 d {   }|7 6 c}S c c}w w)N)r   r   r   r   r5   )r'  )r  r^   r   r   r   r   r   r*   s     r,   r  z@CassandraGraphVectorStore.traversal_search.<locals>.collect_docs  sQ     /// 	
 J *444#C4444s$   83/-
/3/38r  r  )r*   r   r   r   r   r   r  s   `````` r,   traversal_searchz*CassandraGraphVectorStore.traversal_searchu  s     8	5 	5 {{<>**r.   c                  K   t               }|D cg c]  }| j                  j                  |        }}t        j                  |  d{   }|D ],  }|t        |      }|j                  t        |             . |S c c}w 7 <w)a.  Return the set of outgoing links for the given source IDs asynchronously.

        Args:
            source_ids: The IDs of the source nodes to retrieve outgoing links for.

        Returns:
            A set of `Link` objects representing the outgoing links from the source
            nodes.
        r   Nr   r   )r   ry   r   r	  r  r_   r   rj   )r*   
source_idsr%   	source_id
coroutinesr   r^   r+   s           r,   r   z-CassandraGraphVectorStore._get_outgoing_links  s      
 (
 11i1H

 
 ^^Z00 	9C#,_$78	9
 
 1s!   B
#BB
BB
1B
c                x  K   i }g }|D ]F  }| j                  ||      }	|j                  | j                  j                  ||xs d|	             H t	        j
                  |  d{   }
|
D ]7  }|D ]0  \  }}}||vr|||<   ||vst        |      }t        ||      ||<   2 9 |j                         S 7 Pw)a  Return the target nodes with incoming links from any of the given links.

        Args:
            links: The links to look for.
            query_embedding: The query embedding. Used to rank target nodes.
            retrieved_docs: A cache of retrieved docs. This will be added to.
            k_per_link: The number of target nodes to fetch for each link.
            filter: Optional metadata to filter the results.

        Returns:
            Iterable of adjacent edges.
        r  r   r   Nr   )r+   r'   )	r   rw   ry   r   r	  r  r_   r!   values)r*   r%   r   r   r   r   targetstasksrP   r   r   resultr^   r'   r   r+   s                   r,   r   z'CassandraGraphVectorStore._get_adjacent  s     ( ,. 	D"77" 8 O
 LL!!PP- &B* Q 	  .. 	SF*0 S&Y/-0N6*('C0D&2	&RGFOS	S ~~ /s   A%B:'B8(B:1B:c                    g }t        |       D ]:  \  }}t        |      }|
||   |_        |
||   |_        |j	                  |       < |S )N)r]   )	enumerater   rZ   r#   rw   )texts	metadatasr   r   irY   r^   s          r,   _build_docs_from_textsz0CassandraGraphVectorStore._build_docs_from_texts  sd      " ' 	GAt!C $(|QKK	 r.   )rq   rr   rs   r   rt   rl   rn   c               `    | j                  |||      } | j                  d|||||||	|
d|S )  Create a CassandraGraphVectorStore from raw texts.

        Args:
            texts: Texts to add to the vectorstore.
            embedding: Embedding function to use.
            metadatas: Optional list of metadatas associated with the texts.
            session: Cassandra driver session.
                If not provided, it is resolved from cassio.
            keyspace: Cassandra key space.
                If not provided, it is resolved from cassio.
            table_name: Cassandra table (required).
            ids: Optional list of IDs associated with the texts.
            ttl_seconds: Optional time-to-live for the added texts.
            body_index_options: Optional options used to create the body index.
                Eg. body_index_options = [cassio.table.cql.STANDARD_ANALYZER]
            metadata_deny_list: Optional list of metadata keys to not index.
                i.e. to fine-tune which of the metadata fields are indexed.
                Note: if you plan to have massive unique text metadata entries,
                consider not indexing them for performance
                (and to overcome max-length limitations).
                Note: the `metadata_indexing` parameter from
                langchain_community.utilities.cassandra.Cassandra is not
                exposed since CassandraGraphVectorStore only supports the
                deny_list option.

        Returns:
            a CassandraGraphVectorStore.
        r6  r7  r   	documentsr'   rq   rr   rs   rt   rl   rn   r5   )r9  from_documentsrD   r6  r'   r7  rq   rr   rs   r   rt   rl   rn   r   r   s                r,   
from_textsz$CassandraGraphVectorStore.from_texts  sa    X )) * 
 "s!! 

!#11

 

 
	
r.   c               |   K   | j                  |||      } | j                  d|||||||	|
d| d{   S 7 w)r;  r<  r=  Nr5   )r9  afrom_documentsr@  s                r,   afrom_textsz%CassandraGraphVectorStore.afrom_texts<  so     X )) * 
 )S(( 

!#11

 

 

 
	
 

s   3<:<c                @    |t        | |      D ]  \  }}||_         | S r:   )r   r#   )r   r   r^   r   s       r,   _add_ids_to_docsz*CassandraGraphVectorStore._add_ids_to_docsz  s.    
 ?"4~  V r.   c               l     | d|||||||	d|
}|j                  | j                  ||             |S )a|  Create a CassandraGraphVectorStore from a document list.

        Args:
            documents: Documents to add to the vectorstore.
            embedding: Embedding function to use.
            session: Cassandra driver session.
                If not provided, it is resolved from cassio.
            keyspace: Cassandra key space.
                If not provided, it is resolved from cassio.
            table_name: Cassandra table (required).
            ids: Optional list of IDs associated with the documents.
            ttl_seconds: Optional time-to-live for the added documents.
            body_index_options: Optional options used to create the body index.
                Eg. body_index_options = [cassio.table.cql.STANDARD_ANALYZER]
            metadata_deny_list: Optional list of metadata keys to not index.
                i.e. to fine-tune which of the metadata fields are indexed.
                Note: if you plan to have massive unique text metadata entries,
                consider not indexing them for performance
                (and to overcome max-length limitations).
                Note: the `metadata_indexing` parameter from
                langchain_community.utilities.cassandra.Cassandra is not
                exposed since CassandraGraphVectorStore only supports the
                deny_list option.

        Returns:
            a CassandraGraphVectorStore.
        )r'   rq   rr   rs   rt   rl   rn   r   r   r>  r5   )r   rF  rD   r>  r'   rq   rr   rs   r   rt   rl   rn   r   stores               r,   r?  z(CassandraGraphVectorStore.from_documents  s[    T  	
!#11	
 	
 	c&:&:	s&:&STr.   c                  K    | d|||||t         j                  ||	d|
}|j                  | j                  ||             d{    |S 7 w)a}  Create a CassandraGraphVectorStore from a document list.

        Args:
            documents: Documents to add to the vectorstore.
            embedding: Embedding function to use.
            session: Cassandra driver session.
                If not provided, it is resolved from cassio.
            keyspace: Cassandra key space.
                If not provided, it is resolved from cassio.
            table_name: Cassandra table (required).
            ids: Optional list of IDs associated with the documents.
            ttl_seconds: Optional time-to-live for the added documents.
            body_index_options: Optional options used to create the body index.
                Eg. body_index_options = [cassio.table.cql.STANDARD_ANALYZER]
            metadata_deny_list: Optional list of metadata keys to not index.
                i.e. to fine-tune which of the metadata fields are indexed.
                Note: if you plan to have massive unique text metadata entries,
                consider not indexing them for performance
                (and to overcome max-length limitations).
                Note: the `metadata_indexing` parameter from
                langchain_community.utilities.cassandra.Cassandra is not
                exposed since CassandraGraphVectorStore only supports the
                deny_list option.


        Returns:
            a CassandraGraphVectorStore.
        )r'   rq   rr   rs   rt   rm   rl   rn   rH  rI  Nr5   )r   ASYNCr   rF  rJ  s               r,   rC  z)CassandraGraphVectorStore.afrom_documents  s|     V  

!# 11

 

 ""**	s*C # 
 	
 	
 	
s   AAA	A)NNr)   N)r'   r   rq   zSession | Nonerr   
str | Noners   r"   rt   
int | Nonerl   zlist[tuple[str, Any]] | Nonerm   r   rn   Optional[list[str]]r/   r0   )r/   zEmbeddings | None)NN)rZ   dict[str, Any] | Noner   zLink | Noner/   dict[str, Any])r^   r   r/   r   )r+   r   r/   rR  )r   Iterable[Node]r/   z tuple[list[Document], list[str]])r   rS  r   r   r/   Iterable[str])r   rS  r   r   r/   zAsyncIterable[str])r   N)
r   r"   r   r%  r   rQ  r   r   r/   list[Document])
r'   r&   r   r%  r   rQ  r   r   r/   rU  )N   )r   rQ  r   r%  r/   r  )r   r"   r/   zDocument | None)r   r"   r/   zNode | None)r   r"   r   r   r   r%  r   r%  r   r%  r   r%  r   floatr   rW  r   rQ  r   r   r/   AsyncIterable[Document])r   r"   r   r   r   r%  r   r%  r   r%  r   r%  r   rW  r   rW  r   rQ  r   r   r/   r  )r   r"   r   r%  r   r%  r   rQ  r   r   r/   rX  )r   r"   r   r%  r   r%  r   rQ  r   r   r/   r  )r,  rT  r/   	set[Link])r%   rY  r   r&   r   zdict[str, Document]r   rO  r   rQ  r/   zIterable[AdjacentNode])r6  	List[str]r7  Optional[List[dict]]r   Optional[List[str]]r/   List[Document]r:   )rD   Type[CGVST]r6  rZ  r'   r   r7  r[  rq   Optional[Session]rr   Optional[str]rs   r"   r   r\  rt   Optional[int]rl   Optional[List[Tuple[str, Any]]]rn   rP  r   r   r/   r   )r   r]  r   r\  r/   r]  )rD   r^  r>  r]  r'   r   rq   r_  rr   r`  rs   r"   r   r\  rt   ra  rl   rb  rn   rP  r   r   r/   r   )'r1   r2   r3   r   SYNCr-   propertyr   r~   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rW  r  r  r'  r*  r   r   staticmethodr9  classmethodrA  rD  rF  r?  rC  r5   r.   r,   r   r   k   sT   
 #'#"&Q
 <@ )26Q
Q
  Q
 	Q

 Q
  Q
 9Q
 Q
 0Q
 
Q
f   
 +/%)
'
 #
 
	
.#	)" >> > 
	> >   
	   (,	

 
 &	

 
 

 
8  (,	

 
 &	

 
 

 
8  (,	

 
 &	

 
 

 
8  (,	

 
 &	

 
 

 
< )-
%
 
 
	
* )-
%
 
 
	
&
E
E% 
 (* !&v(,x(x( %	x(
 x( x( x( x( x( x( &x( x( 
!x( x(t 
 (* !&v(,>+>+ %	>+
 >+ >+ >+ >+ >+ >+ &>+ >+ 
>+ >+@ 
 (,U(U( 	U(
 U( &U( U( 
!U( U(n 
 (,%+%+ 	%+
 %+ &%+ %+ 
%+ %+NB "&(,1 1  %1  ,	1 
 1  &1  
 1 f  +/#'' ! 
	 " 
 +/	;
 &*"&#'%)>B26;
;
;
 ;
 (	;
 #;
  ;
 ;
 !;
 #;
 <;
 0;
 ;
 
;
 ;
z 
 +/	;
 &*"&#'%)>B26;
;
;
 ;
 (	;
 #;
  ;
 ;
 !;
 #;
 <;
 0;
 ;
 
;
 ;
z  $(  
   &*"&#'%)>B2644!4 4
 #4  4 4 !4 #4 <4 04 4 
4 4l  &*"&#'%)>B2688!8 8
 #8  8 8 !8 #8 <8 08 8 
8 8r.   )r%   r$   r/   r"   )rO   rN  r/   rY  )rP   r   r/   r"   )r/   r"   )r^   r   r/   r   )r+   zNode | AdjacentNoder/   rY  ):__doc__
__future__r   r	  rE   loggingr   dataclassesr   r   typingr   r   r   r	   r
   r   r   r   r   r   r   langchain_core._apir   langchain_core.documentsr   typing_extensionsr   +langchain_community.graph_vectorstores.baser   r   ,langchain_community.graph_vectorstores.linksr   r   1langchain_community.graph_vectorstores.mmr_helperr   'langchain_community.utilities.cassandrar   *langchain_community.vectorstores.cassandrar   rx   r   cassandra.clusterr   langchain_core.embeddingsr   	getLoggerr1   loggerr!   rH   rQ   rU   rW   r_   rf   rj   r   r5   r.   r,   <module>rx     s    9 "     ,    % - & N Q G = X:;)4 
		8	$	# 	#4*
NO H 0 H Hr.   