mirror of
https://github.com/trustgraph-ai/trustgraph.git
synced 2026-07-22 03:31:02 +02:00
Mostly working
This commit is contained in:
parent
ac383d4452
commit
f02a30a848
1 changed files with 50 additions and 46 deletions
|
|
@ -96,17 +96,17 @@ class Processor(FlowProcessor):
|
||||||
def to_uri(self, text):
|
def to_uri(self, text):
|
||||||
return TRUSTGRAPH_ENTITIES + urllib.parse.quote(text)
|
return TRUSTGRAPH_ENTITIES + urllib.parse.quote(text)
|
||||||
|
|
||||||
def emit_triples(self, pub, metadata, triples):
|
async def emit_triples(self, pub, metadata, triples):
|
||||||
tpls = Triples()
|
tpls = Triples()
|
||||||
tpls.metadata = metadata
|
tpls.metadata = metadata
|
||||||
tpls.triples = triples
|
tpls.triples = triples
|
||||||
pub.publish(tpls)
|
await pub.send(tpls)
|
||||||
|
|
||||||
def emit_entity_contexts(self, pub, metadata, entity_contexts):
|
async def emit_entity_contexts(self, pub, metadata, entity_contexts):
|
||||||
ecs = EntityContexts()
|
ecs = EntityContexts()
|
||||||
ecs.metadata = metadata
|
ecs.metadata = metadata
|
||||||
ecs.entity_contexts = entity_contexts
|
ecs.entities = entity_contexts
|
||||||
pub.publish(ecs)
|
await pub.send(ecs)
|
||||||
|
|
||||||
def parse_json(self, text):
|
def parse_json(self, text):
|
||||||
json_match = re.search(r'```(?:json)?(.*?)```', text, re.DOTALL)
|
json_match = re.search(r'```(?:json)?(.*?)```', text, re.DOTALL)
|
||||||
|
|
@ -162,14 +162,11 @@ class Processor(FlowProcessor):
|
||||||
question = prompt
|
question = prompt
|
||||||
)
|
)
|
||||||
|
|
||||||
if agent_response.error:
|
|
||||||
raise RuntimeError(f"Agent error: {agent_response.error}")
|
|
||||||
|
|
||||||
print("response:", agent_response, flush=True)
|
print("response:", agent_response, flush=True)
|
||||||
|
|
||||||
# Parse JSON response
|
# Parse JSON response
|
||||||
try:
|
try:
|
||||||
extraction_data = self.parse_json(agent_response.answer)
|
extraction_data = self.parse_json(agent_response)
|
||||||
except json.JSONDecodeError as e:
|
except json.JSONDecodeError as e:
|
||||||
raise ValueError(f"Invalid JSON response from agent: {e}")
|
raise ValueError(f"Invalid JSON response from agent: {e}")
|
||||||
|
|
||||||
|
|
@ -180,12 +177,12 @@ class Processor(FlowProcessor):
|
||||||
|
|
||||||
# Emit outputs
|
# Emit outputs
|
||||||
if triples:
|
if triples:
|
||||||
self.emit_triples(flow("triples"), msg.metadata, triples)
|
await self.emit_triples(flow("triples"), v.metadata, triples)
|
||||||
|
|
||||||
if entity_contexts:
|
if entity_contexts:
|
||||||
self.emit_entity_contexts(
|
await self.emit_entity_contexts(
|
||||||
flow("entity-contexts"),
|
flow("entity-contexts"),
|
||||||
msg.metadata,
|
v.metadata,
|
||||||
entity_contexts
|
entity_contexts
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -200,91 +197,98 @@ class Processor(FlowProcessor):
|
||||||
|
|
||||||
# Process definitions
|
# Process definitions
|
||||||
for defn in data.get("definitions", []):
|
for defn in data.get("definitions", []):
|
||||||
|
|
||||||
entity_uri = self.to_uri(defn["entity"])
|
entity_uri = self.to_uri(defn["entity"])
|
||||||
|
|
||||||
# Add entity label
|
# Add entity label
|
||||||
triples.append(Triple(
|
triples.append(Triple(
|
||||||
subject=entity_uri,
|
subject=Value(value=entity_uri, is_uri=True),
|
||||||
predicate=RDF_LABEL,
|
predicate=Value(value=RDF_LABEL, is_uri=True),
|
||||||
object=defn["entity"]
|
object=Value(value=defn["entity"], is_uri=False),
|
||||||
))
|
))
|
||||||
|
|
||||||
# Add definition
|
# Add definition
|
||||||
triples.append(Triple(
|
triples.append(Triple(
|
||||||
subject=entity_uri,
|
subject=Value(value=entity_uri, is_uri=True),
|
||||||
predicate=DEFINITION,
|
predicate=Value(value=DEFINITION, is_uri=True),
|
||||||
object=defn["definition"]
|
object=Value(value=defn["definition"], is_uri=False),
|
||||||
))
|
))
|
||||||
|
|
||||||
# Add subject-of relationship to document
|
# Add subject-of relationship to document
|
||||||
if metadata.id:
|
if metadata.id:
|
||||||
triples.append(Triple(
|
triples.append(Triple(
|
||||||
subject=entity_uri,
|
subject=Value(value=entity_uri, is_uri=True),
|
||||||
predicate=SUBJECT_OF,
|
predicate=Value(value=SUBJECT_OF, is_uri=True),
|
||||||
object=metadata.id
|
object=metadata.id,
|
||||||
))
|
))
|
||||||
|
|
||||||
# Create entity context for embeddings
|
# Create entity context for embeddings
|
||||||
entity_contexts.append(EntityContext(
|
entity_contexts.append(EntityContext(
|
||||||
entity=entity_uri,
|
entity=Value(value=entity_uri, is_uri=True),
|
||||||
context=defn["context"]
|
context=defn["definition"]
|
||||||
))
|
))
|
||||||
|
|
||||||
# Process relationships
|
# Process relationships
|
||||||
for rel in data.get("relationships", []):
|
for rel in data.get("relationships", []):
|
||||||
|
|
||||||
subject_uri = self.to_uri(rel["subject"])
|
subject_uri = self.to_uri(rel["subject"])
|
||||||
predicate_uri = self.to_uri(rel["predicate"])
|
predicate_uri = self.to_uri(rel["predicate"])
|
||||||
|
|
||||||
|
subject_value = Value(value=subject_uri, is_uri=True)
|
||||||
|
predicate_value = Value(value=predicate_uri, is_uri=True)
|
||||||
|
if data.get("object-entity", False):
|
||||||
|
object_value = Value(value=predicate_uri, is_uri=True)
|
||||||
|
else:
|
||||||
|
object_value = Value(value=predicate_uri, is_uri=False)
|
||||||
|
|
||||||
# Add subject and predicate labels
|
# Add subject and predicate labels
|
||||||
triples.append(Triple(
|
triples.append(Triple(
|
||||||
subject=subject_uri,
|
subject=subject_value,
|
||||||
predicate=RDF_LABEL,
|
predicate=Value(value=RDF_LABEL, is_uri=True),
|
||||||
object=rel["subject"]
|
object=Value(value=rel["subject"], is_uri=False),
|
||||||
))
|
))
|
||||||
|
|
||||||
triples.append(Triple(
|
triples.append(Triple(
|
||||||
subject=predicate_uri,
|
subject=predicate_value,
|
||||||
predicate=RDF_LABEL,
|
predicate=Value(value=RDF_LABEL, is_uri=True),
|
||||||
object=rel["predicate"]
|
object=Value(value=rel["predicate"], is_uri=False),
|
||||||
))
|
))
|
||||||
|
|
||||||
# Handle object (entity vs literal)
|
# Handle object (entity vs literal)
|
||||||
if rel.get("object-entity", True):
|
if rel.get("object-entity", True):
|
||||||
object_uri = self.to_uri(rel["object"])
|
object_uri = self.to_uri(rel["object"])
|
||||||
triples.append(Triple(
|
triples.append(Triple(
|
||||||
subject=object_uri,
|
subject=object_value,
|
||||||
predicate=RDF_LABEL,
|
predicate=Value(value=RDF_LABEL, is_uri=True),
|
||||||
object=rel["object"]
|
object=rel["object"],
|
||||||
))
|
))
|
||||||
else:
|
|
||||||
object_uri = rel["object"] # Keep as literal
|
|
||||||
|
|
||||||
# Add the main relationship triple
|
# Add the main relationship triple
|
||||||
triples.append(Triple(
|
triples.append(Triple(
|
||||||
subject=subject_uri,
|
subject=subject_value,
|
||||||
predicate=predicate_uri,
|
predicate=predicate_value,
|
||||||
object=object_uri
|
object=object_value
|
||||||
))
|
))
|
||||||
|
|
||||||
# Add subject-of relationships to document
|
# Add subject-of relationships to document
|
||||||
if metadata.id:
|
if metadata.id:
|
||||||
triples.append(Triple(
|
triples.append(Triple(
|
||||||
subject=subject_uri,
|
subject=subject_value,
|
||||||
predicate=SUBJECT_OF,
|
predicate=Value(value=SUBJECT_OF, is_uri=True),
|
||||||
object=metadata.id
|
object=metadata.id,
|
||||||
))
|
))
|
||||||
|
|
||||||
triples.append(Triple(
|
triples.append(Triple(
|
||||||
subject=predicate_uri,
|
subject=predicate_value,
|
||||||
predicate=SUBJECT_OF,
|
predicate=Value(SUBJECT_OF, is_uri=True),
|
||||||
object=metadata.id
|
object=metadata.id,
|
||||||
))
|
))
|
||||||
|
|
||||||
if rel.get("object-entity", True):
|
if rel.get("object-entity", True):
|
||||||
triples.append(Triple(
|
triples.append(Triple(
|
||||||
subject=object_uri,
|
subject=object_value,
|
||||||
predicate=SUBJECT_OF,
|
predicate=Value(SUBJECT_OF, is_uri=True),
|
||||||
object=metadata.id
|
object=metadata.id,
|
||||||
))
|
))
|
||||||
|
|
||||||
return triples, entity_contexts
|
return triples, entity_contexts
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue