Triplet extraction with LLMs¶
narrativegraphs ships with a component that takes natural language instructions for doing triplet extraction of the entities and relations that you are interested in. An LlmTripletExtractor is created with instructions and passed to the NarrativeGraph object on creation.
This notebook downloads its text with gutenbergpy. If you are running it outside the repository, install it first: pip install gutenbergpy.
In [7]:
Copied!
import re
from gutenbergpy.textget import get_text_by_id, strip_headers
# first, let us fetch something as documents
raw_text = get_text_by_id(1342) # Pride & Prejudice by Jane Austen
clean_text = strip_headers(raw_text)
decoded_text = clean_text.decode("utf-8")
splitter = re.compile(r"\n{4}(?:Chapter|CHAPTER) [IVX]+\.")
chapters = splitter.split(decoded_text)[1:3] # just the first two chapters for demo purposes
ids, paragraphs = zip(*[(f"{i}-{j}", p)
for i, c in enumerate(chapters)
for j, p in enumerate(c.split("\n\n"))])
import re
from gutenbergpy.textget import get_text_by_id, strip_headers
# first, let us fetch something as documents
raw_text = get_text_by_id(1342) # Pride & Prejudice by Jane Austen
clean_text = strip_headers(raw_text)
decoded_text = clean_text.decode("utf-8")
splitter = re.compile(r"\n{4}(?:Chapter|CHAPTER) [IVX]+\.")
chapters = splitter.split(decoded_text)[1:3] # just the first two chapters for demo purposes
ids, paragraphs = zip(*[(f"{i}-{j}", p)
for i, c in enumerate(chapters)
for j, p in enumerate(c.split("\n\n"))])
In [8]:
Copied!
from narrativegraphs import NarrativeGraph
from narrativegraphs.nlp.triplets import LlmTripletExtractor
from narrativegraphs.nlp.common.llm import OpenAiCompatibleClient
extractor = LlmTripletExtractor(
"""The text is a paragraph from Pride & Prejudice by Jane Austen.
Extract only character names as subjects and objects. Predicates must be verbs or contain verbs.""",
llm=OpenAiCompatibleClient(model="qwen/qwen3.5-9b", base_url="http://127.0.0.1:1234/v1") # Local LM Studio
)
model = NarrativeGraph(
triplet_extractor=extractor,
)
model.fit(paragraphs, doc_ids=ids)
from narrativegraphs import NarrativeGraph
from narrativegraphs.nlp.triplets import LlmTripletExtractor
from narrativegraphs.nlp.common.llm import OpenAiCompatibleClient
extractor = LlmTripletExtractor(
"""The text is a paragraph from Pride & Prejudice by Jane Austen.
Extract only character names as subjects and objects. Predicates must be verbs or contain verbs.""",
llm=OpenAiCompatibleClient(model="qwen/qwen3.5-9b", base_url="http://127.0.0.1:1234/v1") # Local LM Studio
)
model = NarrativeGraph(
triplet_extractor=extractor,
)
model.fit(paragraphs, doc_ids=ids)
Extracting triplets: 0%| | 0/77 [00:00<?, ?it/s]
Out[8]:
<narrativegraphs.graphs.NarrativeGraph at 0x3052c3920>
In [10]:
Copied!
model.serve_visualizer()
model.serve_visualizer()
INFO: Started server process [66951] INFO: Waiting for application startup. INFO: Application startup complete. INFO: Uvicorn running on http://127.0.0.1:8001 (Press CTRL+C to quit) INFO: Shutting down INFO: Waiting for application shutdown. INFO: Application shutdown complete. INFO: Finished server process [66951]
In [ ]:
Copied!