-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
140 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 4 additions & 7 deletions
11
libs/langchain/ragstack_langchain/graph_store/extractors/edge_extractor.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
from dataclasses import dataclass | ||
from typing import Literal, Dict, Any, Set, Union | ||
|
||
from langchain_core.documents import Document | ||
|
||
|
||
@dataclass(frozen=True) | ||
class Link: | ||
kind: str | ||
direction: Literal["incoming", "outgoing", "bidir"] | ||
|
||
def __post_init__(self): | ||
if self.__class__ in [Link, LinkTag]: | ||
raise TypeError( | ||
f"Abstract class {self.__class__.__name__} cannot be instantiated" | ||
) | ||
|
||
|
||
@dataclass(frozen=True) | ||
class LinkTag(Link): | ||
tag: str | ||
|
||
|
||
@dataclass(frozen=True) | ||
class OutgoingLinkTag(LinkTag): | ||
def __init__(self, kind: str, tag: str) -> None: | ||
super().__init__(kind=kind, tag=tag, direction="outgoing") | ||
|
||
|
||
@dataclass(frozen=True) | ||
class IncomingLinkTag(LinkTag): | ||
def __init__(self, kind: str, tag: str) -> None: | ||
super().__init__(kind=kind, tag=tag, direction="incoming") | ||
|
||
|
||
@dataclass(frozen=True) | ||
class BidirLinkTag(LinkTag): | ||
def __init__(self, kind: str, tag: str) -> None: | ||
super().__init__(kind=kind, tag=tag, direction="bidir") | ||
|
||
|
||
LINKS = "links" | ||
|
||
|
||
def get_links(doc_or_md: Union[Document, Dict[str, Any]]) -> Set[Link]: | ||
"""Get the links from a document or metadata. | ||
Args: | ||
doc_or_md: The metadata to get the link tags from. | ||
Returns: | ||
The set of link tags from the document or metadata. | ||
""" | ||
|
||
if isinstance(doc_or_md, Document): | ||
doc_or_md = doc_or_md.metadata | ||
|
||
links = doc_or_md.setdefault(LINKS, set()) | ||
if not isinstance(links, Set): | ||
links = set(links) | ||
doc_or_md[LINKS] = links | ||
return links | ||
|
||
|
||
def add_links(doc_or_md: Union[Document, Dict[str, Any]], *links: Link) -> None: | ||
"""Add links to the given metadata. | ||
Args: | ||
doc_or_md: The document or metadata to add the links to. | ||
*links: The links to add to the metadata. | ||
""" | ||
get_links(doc_or_md).update(links) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.