mirror of
https://github.com/trustgraph-ai/trustgraph.git
synced 2026-07-22 03:31:02 +02:00
Fixing query stuff
This commit is contained in:
parent
179285e186
commit
8c38dc30ff
8 changed files with 27 additions and 33 deletions
|
|
@ -4,8 +4,8 @@ from .. schema import GraphEmbeddingsRequest, GraphEmbeddingsResponse
|
||||||
from .. knowledge import Uri, Literal
|
from .. knowledge import Uri, Literal
|
||||||
|
|
||||||
def to_value(x):
|
def to_value(x):
|
||||||
if x.e: return Uri(x.v)
|
if x.is_uri: return Uri(x.value)
|
||||||
return Literal(x.v)
|
return Literal(x.value)
|
||||||
|
|
||||||
class GraphEmbeddingsClient(RequestResponse):
|
class GraphEmbeddingsClient(RequestResponse):
|
||||||
async def query(self, vectors, limit=20, user="trustgraph",
|
async def query(self, vectors, limit=20, user="trustgraph",
|
||||||
|
|
@ -21,6 +21,8 @@ class GraphEmbeddingsClient(RequestResponse):
|
||||||
timeout=timeout
|
timeout=timeout
|
||||||
)
|
)
|
||||||
|
|
||||||
|
print(resp, flush=True)
|
||||||
|
|
||||||
if resp.error:
|
if resp.error:
|
||||||
raise RuntimeError(resp.error.message)
|
raise RuntimeError(resp.error.message)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -49,7 +49,7 @@ class GraphEmbeddingsQueryService(FlowProcessor):
|
||||||
|
|
||||||
print(f"Handling input {id}...", flush=True)
|
print(f"Handling input {id}...", flush=True)
|
||||||
|
|
||||||
entities = self.query_graph_embeddings(request)
|
entities = await self.query_graph_embeddings(request)
|
||||||
|
|
||||||
print("Send response...", flush=True)
|
print("Send response...", flush=True)
|
||||||
r = GraphEmbeddingsResponse(entities=entities, error=None)
|
r = GraphEmbeddingsResponse(entities=entities, error=None)
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,11 @@
|
||||||
|
|
||||||
from . request_response_spec import RequestResponse, RequestResponseSpec
|
from . request_response_spec import RequestResponse, RequestResponseSpec
|
||||||
from .. schema import TriplesQueryRequest, TriplesQueryResponse, Value
|
from .. schema import TriplesQueryRequest, TriplesQueryResponse, Value
|
||||||
from .. knowledge import Uri, Literal
|
from .. knowledge import Uri, Literal, Triple
|
||||||
|
|
||||||
def to_value(x):
|
def to_value(x):
|
||||||
if x.e: return Uri(x.v)
|
if x.is_uri: return Uri(x.value)
|
||||||
return Literal(x.v)
|
return Literal(x.value)
|
||||||
|
|
||||||
def from_value(x):
|
def from_value(x):
|
||||||
if x is None: return None
|
if x is None: return None
|
||||||
|
|
@ -35,7 +35,7 @@ class TriplesClient(RequestResponse):
|
||||||
raise RuntimeError(resp.error.message)
|
raise RuntimeError(resp.error.message)
|
||||||
|
|
||||||
return [
|
return [
|
||||||
to_value(v)
|
Triple(to_value(v.s), to_value(v.p), to_value(v.o))
|
||||||
for v in resp.triples
|
for v in resp.triples
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,7 @@ class TriplesQueryService(FlowProcessor):
|
||||||
|
|
||||||
id = params.get("id")
|
id = params.get("id")
|
||||||
|
|
||||||
super(TriplesStoreService, self).__init__(**params | { "id": id })
|
super(TriplesQueryService, self).__init__(**params | { "id": id })
|
||||||
|
|
||||||
self.register_specification(
|
self.register_specification(
|
||||||
ConsumerSpec(
|
ConsumerSpec(
|
||||||
|
|
@ -47,7 +47,7 @@ class TriplesQueryService(FlowProcessor):
|
||||||
|
|
||||||
print(f"Handling input {id}...", flush=True)
|
print(f"Handling input {id}...", flush=True)
|
||||||
|
|
||||||
triples = self.query_triples(request)
|
triples = await self.query_triples(request)
|
||||||
|
|
||||||
print("Send response...", flush=True)
|
print("Send response...", flush=True)
|
||||||
r = TriplesQueryResponse(triples=triples, error=None)
|
r = TriplesQueryResponse(triples=triples, error=None)
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,10 @@ URL = 'https://schema.org/url'
|
||||||
IDENTIFIER = 'https://schema.org/identifier'
|
IDENTIFIER = 'https://schema.org/identifier'
|
||||||
KEYWORD = 'https://schema.org/keywords'
|
KEYWORD = 'https://schema.org/keywords'
|
||||||
|
|
||||||
|
class Triple:
|
||||||
|
def __init__(self, s, p, o):
|
||||||
|
self.s, self.p, self.o = s, p, o
|
||||||
|
|
||||||
class Uri(str):
|
class Uri(str):
|
||||||
def is_uri(self): return True
|
def is_uri(self): return True
|
||||||
def is_literal(self): return False
|
def is_literal(self): return False
|
||||||
|
|
|
||||||
|
|
@ -44,21 +44,14 @@ class Processor(GraphEmbeddingsQueryService):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
||||||
v = msg.value()
|
|
||||||
|
|
||||||
# Sender-produced ID
|
|
||||||
id = msg.properties()["id"]
|
|
||||||
|
|
||||||
print(f"Handling input {id}...", flush=True)
|
|
||||||
|
|
||||||
entity_set = set()
|
entity_set = set()
|
||||||
entities = []
|
entities = []
|
||||||
|
|
||||||
for vec in v.vectors:
|
for vec in msg.vectors:
|
||||||
|
|
||||||
dim = len(vec)
|
dim = len(vec)
|
||||||
collection = (
|
collection = (
|
||||||
"t_" + v.user + "_" + v.collection + "_" +
|
"t_" + msg.user + "_" + msg.collection + "_" +
|
||||||
str(dim)
|
str(dim)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -67,7 +60,7 @@ class Processor(GraphEmbeddingsQueryService):
|
||||||
search_result = self.qdrant.query_points(
|
search_result = self.qdrant.query_points(
|
||||||
collection_name=collection,
|
collection_name=collection,
|
||||||
query=vec,
|
query=vec,
|
||||||
limit=v.limit * 2,
|
limit=msg.limit * 2,
|
||||||
with_payload=True,
|
with_payload=True,
|
||||||
).points
|
).points
|
||||||
|
|
||||||
|
|
@ -80,10 +73,10 @@ class Processor(GraphEmbeddingsQueryService):
|
||||||
entities.append(ent)
|
entities.append(ent)
|
||||||
|
|
||||||
# Keep adding entities until limit
|
# Keep adding entities until limit
|
||||||
if len(entity_set) >= v.limit: break
|
if len(entity_set) >= msg.limit: break
|
||||||
|
|
||||||
# Keep adding entities until limit
|
# Keep adding entities until limit
|
||||||
if len(entity_set) >= v.limit: break
|
if len(entity_set) >= msg.limit: break
|
||||||
|
|
||||||
ents2 = []
|
ents2 = []
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -59,11 +59,6 @@ class Processor(TriplesQueryService):
|
||||||
)
|
)
|
||||||
self.table = table
|
self.table = table
|
||||||
|
|
||||||
# Sender-produced ID
|
|
||||||
id = msg.properties()["id"]
|
|
||||||
|
|
||||||
print(f"Handling input {id}...", flush=True)
|
|
||||||
|
|
||||||
triples = []
|
triples = []
|
||||||
|
|
||||||
if query.s is not None:
|
if query.s is not None:
|
||||||
|
|
@ -138,8 +133,6 @@ class Processor(TriplesQueryService):
|
||||||
|
|
||||||
return triples
|
return triples
|
||||||
|
|
||||||
print("Done.", flush=True)
|
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|
||||||
print(f"Exception: {e}")
|
print(f"Exception: {e}")
|
||||||
|
|
|
||||||
|
|
@ -39,6 +39,8 @@ class Query:
|
||||||
if self.verbose:
|
if self.verbose:
|
||||||
print("Get entities...", flush=True)
|
print("Get entities...", flush=True)
|
||||||
|
|
||||||
|
print("HERE>", query, flush=True)
|
||||||
|
|
||||||
entities = await self.rag.graph_embeddings_client.query(
|
entities = await self.rag.graph_embeddings_client.query(
|
||||||
vectors=vectors, limit=self.entity_limit,
|
vectors=vectors, limit=self.entity_limit,
|
||||||
user=self.user, collection=self.collection,
|
user=self.user, collection=self.collection,
|
||||||
|
|
@ -47,7 +49,7 @@ class Query:
|
||||||
print("ENT>", entities, flush=True)
|
print("ENT>", entities, flush=True)
|
||||||
|
|
||||||
entities = [
|
entities = [
|
||||||
e.value
|
str(e)
|
||||||
for e in entities
|
for e in entities
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
@ -72,7 +74,7 @@ class Query:
|
||||||
self.rag.label_cache[e] = e
|
self.rag.label_cache[e] = e
|
||||||
return e
|
return e
|
||||||
|
|
||||||
self.rag.label_cache[e] = res[0].o.value
|
self.rag.label_cache[e] = str(res[0].o)
|
||||||
return self.rag.label_cache[e]
|
return self.rag.label_cache[e]
|
||||||
|
|
||||||
async def follow_edges(self, ent, subgraph, path_length):
|
async def follow_edges(self, ent, subgraph, path_length):
|
||||||
|
|
@ -93,10 +95,10 @@ class Query:
|
||||||
|
|
||||||
for triple in res:
|
for triple in res:
|
||||||
subgraph.add(
|
subgraph.add(
|
||||||
(triple.s.value, triple.p.value, triple.o.value)
|
(str(triple.s), str(triple.p), str(triple.o))
|
||||||
)
|
)
|
||||||
if path_length > 1:
|
if path_length > 1:
|
||||||
await self.follow_edges(triple.o.value, subgraph, path_length-1)
|
await self.follow_edges(str(triple.o), subgraph, path_length-1)
|
||||||
|
|
||||||
res = await self.rag.triples_client.request(
|
res = await self.rag.triples_client.request(
|
||||||
user=self.user, collection=self.collection,
|
user=self.user, collection=self.collection,
|
||||||
|
|
@ -106,7 +108,7 @@ class Query:
|
||||||
|
|
||||||
for triple in res:
|
for triple in res:
|
||||||
subgraph.add(
|
subgraph.add(
|
||||||
(triple.s.value, triple.p.value, triple.o.value)
|
(str(triple.s), str(triple.p), str(triple.o))
|
||||||
)
|
)
|
||||||
|
|
||||||
res = await self.rag.triples_client.request(
|
res = await self.rag.triples_client.request(
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue