diff --git a/trustgraph-base/trustgraph/base/triples_client.py b/trustgraph-base/trustgraph/base/triples_client.py index d9715c09..84e95ebe 100644 --- a/trustgraph-base/trustgraph/base/triples_client.py +++ b/trustgraph-base/trustgraph/base/triples_client.py @@ -22,11 +22,19 @@ def to_value(x): def from_value(x): - """Convert Uri or Literal to schema Term.""" + """Convert Uri, Literal, or string to schema Term.""" if x is None: return None if isinstance(x, Uri): return Term(type=IRI, iri=str(x)) + elif isinstance(x, Literal): + return Term(type=LITERAL, value=str(x)) + elif isinstance(x, str): + # Detect IRIs by common prefixes + if x.startswith("http://") or x.startswith("https://") or x.startswith("urn:"): + return Term(type=IRI, iri=x) + else: + return Term(type=LITERAL, value=x) else: return Term(type=LITERAL, value=str(x)) diff --git a/trustgraph-flow/trustgraph/retrieval/graph_rag/graph_rag.py b/trustgraph-flow/trustgraph/retrieval/graph_rag/graph_rag.py index 80042951..8dbeb41b 100644 --- a/trustgraph-flow/trustgraph/retrieval/graph_rag/graph_rag.py +++ b/trustgraph-flow/trustgraph/retrieval/graph_rag/graph_rag.py @@ -4,11 +4,25 @@ import logging import time from collections import OrderedDict +from ... schema import IRI, LITERAL + # Module logger logger = logging.getLogger(__name__) LABEL="http://www.w3.org/2000/01/rdf-schema#label" + +def term_to_string(term): + """Extract string value from a Term object.""" + if term is None: + return None + if term.type == IRI: + return term.iri + elif term.type == LITERAL: + return term.value + # Fallback + return term.iri or term.value or str(term) + class LRUCacheWithTTL: """LRU cache with TTL for label caching @@ -93,7 +107,7 @@ class Query: ) entities = [ - str(e.entity) + term_to_string(e.entity) for e in entity_matches ]