mirror of
https://github.com/trustgraph-ai/trustgraph.git
synced 2026-07-19 18:21:03 +02:00
212 lines
5.8 KiB
Python
Executable file
212 lines
5.8 KiB
Python
Executable file
|
|
"""
|
|
Simple decoder, accepts text chunks input, applies entity
|
|
relationship analysis to get entity relationship edges which are output as
|
|
graph edges.
|
|
"""
|
|
|
|
import json
|
|
import urllib.parse
|
|
|
|
from .... schema import Chunk, Triple, Triples
|
|
from .... schema import Metadata, Value
|
|
from .... schema import PromptRequest, PromptResponse
|
|
from .... rdf import RDF_LABEL, TRUSTGRAPH_ENTITIES, SUBJECT_OF
|
|
|
|
from .... base import FlowProcessor, RequestResponseSpec, ConsumerSpec
|
|
from .... base import ProducerSpec
|
|
|
|
RDF_LABEL_VALUE = Value(value=RDF_LABEL, is_uri=True)
|
|
SUBJECT_OF_VALUE = Value(value=SUBJECT_OF, is_uri=True)
|
|
|
|
default_ident = "kg-extract-relationships"
|
|
|
|
class Processor(FlowProcessor):
|
|
|
|
def __init__(self, **params):
|
|
|
|
id = params.get("id")
|
|
|
|
super(Processor, self).__init__(
|
|
**params | {
|
|
"id": id,
|
|
}
|
|
)
|
|
|
|
self.register_specification(
|
|
ConsumerSpec(
|
|
name = "input",
|
|
schema = Chunk,
|
|
handler = self.on_message
|
|
)
|
|
)
|
|
|
|
self.register_specification(
|
|
RequestResponseSpec(
|
|
request_name = "prompt-request",
|
|
request_schema = PromptRequest,
|
|
response_name = "prompt-response",
|
|
response_schema = PromptResponse,
|
|
)
|
|
)
|
|
|
|
self.register_specification(
|
|
ProducerSpec(
|
|
name = "triples",
|
|
schema = Triples
|
|
)
|
|
)
|
|
|
|
def to_uri(self, text):
|
|
|
|
part = text.replace(" ", "-").lower().encode("utf-8")
|
|
quoted = urllib.parse.quote(part)
|
|
uri = TRUSTGRAPH_ENTITIES + quoted
|
|
|
|
return uri
|
|
|
|
async def emit_triples(self, pub, metadata, triples):
|
|
|
|
t = Triples(
|
|
metadata=metadata,
|
|
triples=triples,
|
|
)
|
|
await pub.send(t)
|
|
|
|
async def on_message(self, msg, consumer, flow):
|
|
|
|
v = msg.value()
|
|
print(f"Indexing {v.metadata.id}...", flush=True)
|
|
|
|
chunk = v.chunk.decode("utf-8")
|
|
|
|
print(chunk, flush=True)
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
resp = await flow("prompt-request").request(
|
|
PromptRequest(
|
|
id="extract-relationships",
|
|
terms={
|
|
"text": json.dumps(chunk)
|
|
},
|
|
)
|
|
)
|
|
print("Response", resp, flush=True)
|
|
|
|
if resp.error is not None:
|
|
print("Error:", resp.error.message, flush=True)
|
|
raise RuntimeError(resp.error.message)
|
|
|
|
if resp.object is None:
|
|
raise RuntimeError("Expecting object in prompt response")
|
|
|
|
rels = json.loads(resp.object)
|
|
|
|
except Exception as e:
|
|
print("Prompt exception:", e, flush=True)
|
|
raise e
|
|
|
|
triples = []
|
|
|
|
# FIXME: Putting metadata into triples store is duplicated in
|
|
# relationships extractor too
|
|
for t in v.metadata.metadata:
|
|
triples.append(t)
|
|
|
|
for rel in rels:
|
|
|
|
s = rel["s"]
|
|
p = rel["p"]
|
|
o = rel["o"]
|
|
|
|
if s == "": continue
|
|
if p == "": continue
|
|
if o == "": continue
|
|
|
|
if s is None: continue
|
|
if p is None: continue
|
|
if o is None: continue
|
|
|
|
s_uri = self.to_uri(s)
|
|
s_value = Value(value=str(s_uri), is_uri=True)
|
|
|
|
p_uri = self.to_uri(p)
|
|
p_value = Value(value=str(p_uri), is_uri=True)
|
|
|
|
if rel["o_entity"]:
|
|
o_uri = self.to_uri(o)
|
|
o_value = Value(value=str(o_uri), is_uri=True)
|
|
else:
|
|
o_value = Value(value=str(o), is_uri=False)
|
|
|
|
triples.append(Triple(
|
|
s=s_value,
|
|
p=p_value,
|
|
o=o_value
|
|
))
|
|
|
|
# Label for s
|
|
triples.append(Triple(
|
|
s=s_value,
|
|
p=RDF_LABEL_VALUE,
|
|
o=Value(value=str(s), is_uri=False)
|
|
))
|
|
|
|
# Label for p
|
|
triples.append(Triple(
|
|
s=p_value,
|
|
p=RDF_LABEL_VALUE,
|
|
o=Value(value=str(p), is_uri=False)
|
|
))
|
|
|
|
if rel["o_entity"]:
|
|
# Label for o
|
|
triples.append(Triple(
|
|
s=o_value,
|
|
p=RDF_LABEL_VALUE,
|
|
o=Value(value=str(o), is_uri=False)
|
|
))
|
|
|
|
# 'Subject of' for s
|
|
triples.append(Triple(
|
|
s=s_value,
|
|
p=SUBJECT_OF_VALUE,
|
|
o=Value(value=v.metadata.id, is_uri=True)
|
|
))
|
|
|
|
if rel["o_entity"]:
|
|
# 'Subject of' for o
|
|
triples.append(Triple(
|
|
s=o_value,
|
|
p=SUBJECT_OF_VALUE,
|
|
o=Value(value=v.metadata.id, is_uri=True)
|
|
))
|
|
|
|
await self.emit_edges(
|
|
flow("triples"),
|
|
Metadata(
|
|
id=v.metadata.id,
|
|
metadata=[],
|
|
user=v.metadata.user,
|
|
collection=v.metadata.collection,
|
|
),
|
|
triples
|
|
)
|
|
|
|
except Exception as e:
|
|
print("Exception: ", e, flush=True)
|
|
|
|
print("Done.", flush=True)
|
|
|
|
@staticmethod
|
|
def add_args(parser):
|
|
|
|
FlowProcessor.add_args(parser)
|
|
|
|
def run():
|
|
|
|
Processor.launch(default_ident, __doc__)
|
|
|