
    3fi                     "   d dl mZ d dlmZ d dlmZmZ d dlmZ d dl	m
Z
  e        ed       G d d	                    Zd
Z e       de
dee   fd       Z e       de
d
eeee   f   ddfd       Z e       de
d
eeee   f   de
fd       Zy)    )Iterable)	dataclass)LiteralUnion)beta)DocumentT)frozenc                       e Zd ZU dZeed<   	 ed   ed<   	 eed<   	 edededd fd       Zedededd fd       Z	edededd fd	       Z
y
)LinkaR  A link to/from a tag of a given kind.

    Documents in a :class:`graph vector store <langchain_community.graph_vectorstores.base.GraphVectorStore>`
    are connected via "links".
    Links form a bipartite graph between documents and tags: documents are connected
    to tags, and tags are connected to other documents.
    When documents are retrieved from a graph vector store, a pair of documents are
    connected with a depth of one if both documents are connected to the same tag.

    Links have a ``kind`` property, used to namespace different tag identifiers.
    For example a link to a keyword might use kind ``kw``, while a link to a URL might
    use kind ``url``.
    This allows the same tag value to be used in different contexts without causing
    name collisions.

    Links are directed. The directionality of links controls how the graph is
    traversed at retrieval time.
    For example, given documents ``A`` and ``B``, connected by links to tag ``T``:

    +----------+----------+---------------------------------+
    | A to T   | B to T   | Result                          |
    +==========+==========+=================================+
    | outgoing | incoming | Retrieval traverses from A to B |
    +----------+----------+---------------------------------+
    | incoming | incoming | No traversal from A to B        |
    +----------+----------+---------------------------------+
    | outgoing | incoming | No traversal from A to B        |
    +----------+----------+---------------------------------+
    | bidir    | incoming | Retrieval traverses from A to B |
    +----------+----------+---------------------------------+
    | bidir    | outgoing | No traversal from A to B        |
    +----------+----------+---------------------------------+
    | outgoing | bidir    | Retrieval traverses from A to B |
    +----------+----------+---------------------------------+
    | incoming | bidir    | No traversal from A to B        |
    +----------+----------+---------------------------------+

    Directed links make it possible to describe relationships such as term
    references / definitions: term definitions are generally relevant to any documents
    that use the term, but the full set of documents using a term generally aren't
    relevant to the term's definition.

    .. seealso::

        - :mod:`How to use a graph vector store <langchain_community.graph_vectorstores>`
        - :class:`How to link Documents on hyperlinks in HTML <langchain_community.graph_vectorstores.extractors.html_link_extractor.HtmlLinkExtractor>`
        - :class:`How to link Documents on common keywords (using KeyBERT) <langchain_community.graph_vectorstores.extractors.keybert_link_extractor.KeybertLinkExtractor>`
        - :class:`How to link Documents on common named entities (using GliNER) <langchain_community.graph_vectorstores.extractors.gliner_link_extractor.GLiNERLinkExtractor>`

    How to add links to a Document
    ==============================

    How to create links
    -------------------

    You can create links using the Link class's constructors :meth:`incoming`,
    :meth:`outgoing`, and :meth:`bidir`::

        from langchain_community.graph_vectorstores.links import Link

        print(Link.bidir(kind="location", tag="Paris"))

    .. code-block:: output

        Link(kind='location', direction='bidir', tag='Paris')

    Extending documents with links
    ------------------------------

    Now that we know how to create links, let's associate them with some documents.
    These edges will strengthen the connection between documents that share a keyword
    when using a graph vector store to retrieve documents.

    First, we'll load some text and chunk it into smaller pieces.
    Then we'll add a link to each document to link them all together::

        from langchain_community.document_loaders import TextLoader
        from langchain_community.graph_vectorstores.links import add_links
        from langchain_text_splitters import CharacterTextSplitter

        loader = TextLoader("state_of_the_union.txt")

        raw_documents = loader.load()
        text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
        documents = text_splitter.split_documents(raw_documents)

        for doc in documents:
            add_links(doc, Link.bidir(kind="genre", tag="oratory"))

        print(documents[0].metadata)

    .. code-block:: output

        {'source': 'state_of_the_union.txt', 'links': [Link(kind='genre', direction='bidir', tag='oratory')]}

    As we can see, each document's metadata now includes a bidirectional link to the
    genre ``oratory``.

    The documents can then be added to a graph vector store::

        from langchain_community.graph_vectorstores import CassandraGraphVectorStore

        graph_vectorstore = CassandraGraphVectorStore.from_documents(
            documents=documents, embeddings=...
        )

    kind)inoutbidir	directiontagreturnc                     t        | d|      S )zpCreate an incoming link.

        Args:
            kind: the link kind.
            tag: the link tag.
        r   r   r   r   r   r   r   s     j/var/www/auto_recruiter/arenv/lib/python3.12/site-packages/langchain_community/graph_vectorstores/links.pyincomingzLink.incoming   s     377    c                     t        | d|      S )zpCreate an outgoing link.

        Args:
            kind: the link kind.
            tag: the link tag.
        r   r   r   r   s     r   outgoingzLink.outgoing   s     C88r   c                     t        | d|      S )ztCreate a bidirectional link.

        Args:
            kind: the link kind.
            tag: the link tag.
        r   r   r   r   s     r   r   z
Link.bidir   s     c::r   N)__name__
__module____qualname____doc__str__annotations__r   staticmethodr   r   r    r   r   r   r   	   s    jX IW+,,$	H8s 8 8 8 8 9s 9 9 9 9 ;C ;c ;f ; ;r   r   linksdocr   c                     | j                   j                  t        g       }t        |t              st	        |      }|| j                   t        <   |S )zGet the links from a document.

    Args:
        doc: The document to get the link tags from.
    Returns:
        The set of link tags from the document.
    )metadata
setdefaultMETADATA_LINKS_KEY
isinstancelist)r&   r%   s     r   	get_linksr-      sB     LL##$6;EeT"U+0'(Lr   Nc                     t        |       }|D ]5  }t        |t              r|j                  |       %|j	                  |       7 y)zAdd links to the given metadata.

    Args:
        doc: The document to add the links to.
        *links: The links to add to the document.
    N)r-   r+   r   extendappend)r&   r%   links_in_metadatalinks       r   	add_linksr3      sC     "# +dH%$$T*$$T*	+r   c                    t        t        |             }|D ]5  }t        |t              r|j	                  |       %|j                  |       7 t        | j                  i | j                  t        t        |      i      S )zReturn a document with the given links added.

    Args:
        doc: The document to add the links to.
        *links: The links to add to the document.

    Returns:
        A document with a shallow-copy of the metadata with the links added.
    )page_contentr(   )setr-   r+   r   updateaddr   r5   r(   r*   r,   )r&   r%   	new_linksr2   s       r   copy_with_linksr:      s|     IcN#I  dH%T"MM$	  %%
ll
Y
 r   )collections.abcr   dataclassesr   typingr   r   langchain_core._apir   langchain_core.documentsr   r   r*   r,   r-   r3   r:   r$   r   r   <module>r@      s    $ ! ! $ - 
$Q; Q;  Q;h   8 T
  " +8 +U4$+?%@ +T + +  5x~1E+F 8  r   