Edges¶
Edges specify how content should be linked. Often, content in existing vector stores has metadata based on structured information. For example, a vector store containing articles may have information about the authors, keywords, and citations of those articles. Such content can be traversed along relationships already present in that metadata! See Specifying Edges for more on how edges are specified.
Edges can be dynamically specified each time
Since edges can be different each time the traversal is invoked, it is possible to tailor the relationships being used to the question.
Specifying Edges¶
Edges are specified by passing the edges
parameter to traverse
or atraverse
. When used with LangChain, they may be provided when the GraphRetriever
is instantiated or when invoke
or ainvoke
is called.
The following example shows how edges can be defined using metadata from an example article.
Specifying Edges
Content(
id="article1",
content="...",
metadata={
"keywords": ["GPT", "GenAI"],
"authors": ["Ben", "Eric"],
"primary_author": "Eric",
"cites": ["article2", "article3"],
}
)
("keywords", "keywords")
connects to other articles about GPT and GenAI.("authors", "authors")
connects to other articles by any of the same authors.("authors", "primary_author")
connects to other articles whose primary author was Ben or Eric.("cites", Id())
connects to the articles cited (by ID).(Id(), "cites")
connects to articles which cite this one.("cites", "cites")
connects to other articles with citations in common.
Edge Functions¶
While sometimes the information to traverse is missing and the vector store
needs to be re-populated, in other cases the information exist but not quite be
in a suitable format for traversal. For instance, the "authors"
field may
contain a list of authors and their institution, making it impossible to link to
other articles by the same author when they were at a different institution.
In such cases, you can provide a custom
EdgeFunction
to extract the edges for
traversal.