mirror of
https://github.com/trustgraph-ai/trustgraph.git
synced 2026-07-22 03:31:02 +02:00
Graph RAG working I think
This commit is contained in:
parent
8c38dc30ff
commit
6be7b30633
5 changed files with 31 additions and 24 deletions
|
|
@ -3,7 +3,9 @@
|
||||||
import pulsar
|
import pulsar
|
||||||
from trustgraph.clients.triples_query_client import TriplesQueryClient
|
from trustgraph.clients.triples_query_client import TriplesQueryClient
|
||||||
|
|
||||||
tq = TriplesQueryClient(pulsar_host="pulsar://localhost:6650")
|
tq = TriplesQueryClient(
|
||||||
|
pulsar_host="pulsar://localhost:6650",
|
||||||
|
)
|
||||||
|
|
||||||
e = "http://trustgraph.ai/e/shuttle"
|
e = "http://trustgraph.ai/e/shuttle"
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -29,13 +29,15 @@ class PromptClient(RequestResponse):
|
||||||
async def extract_definitions(self, text, timeout=600):
|
async def extract_definitions(self, text, timeout=600):
|
||||||
return await self.prompt(
|
return await self.prompt(
|
||||||
id = "extract-definitions",
|
id = "extract-definitions",
|
||||||
variables = { "text": text }
|
variables = { "text": text },
|
||||||
|
timeout = timeout,
|
||||||
)
|
)
|
||||||
|
|
||||||
async def extract_relationships(self, text, timeout=600):
|
async def extract_relationships(self, text, timeout=600):
|
||||||
return await self.prompt(
|
return await self.prompt(
|
||||||
id = "extract-relationships",
|
id = "extract-relationships",
|
||||||
variables = { "text": text }
|
variables = { "text": text },
|
||||||
|
timeout = timeout,
|
||||||
)
|
)
|
||||||
|
|
||||||
async def kg_prompt(self, query, kg, timeout=600):
|
async def kg_prompt(self, query, kg, timeout=600):
|
||||||
|
|
@ -47,7 +49,8 @@ class PromptClient(RequestResponse):
|
||||||
{ "s": v[0], "p": v[1], "o": v[2] }
|
{ "s": v[0], "p": v[1], "o": v[2] }
|
||||||
for v in kg
|
for v in kg
|
||||||
]
|
]
|
||||||
}
|
},
|
||||||
|
timeout = timeout,
|
||||||
)
|
)
|
||||||
|
|
||||||
class PromptClientSpec(RequestResponseSpec):
|
class PromptClientSpec(RequestResponseSpec):
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,13 @@
|
||||||
|
|
||||||
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, Triple
|
from .. knowledge import Uri, Literal
|
||||||
|
|
||||||
|
class Triple:
|
||||||
|
def __init__(self, s, p, o):
|
||||||
|
self.s = s
|
||||||
|
self.p = p
|
||||||
|
self.o = o
|
||||||
|
|
||||||
def to_value(x):
|
def to_value(x):
|
||||||
if x.is_uri: return Uri(x.value)
|
if x.is_uri: return Uri(x.value)
|
||||||
|
|
@ -34,11 +40,13 @@ class TriplesClient(RequestResponse):
|
||||||
if resp.error:
|
if resp.error:
|
||||||
raise RuntimeError(resp.error.message)
|
raise RuntimeError(resp.error.message)
|
||||||
|
|
||||||
return [
|
triples = [
|
||||||
Triple(to_value(v.s), to_value(v.p), to_value(v.o))
|
Triple(to_value(v.s), to_value(v.p), to_value(v.o))
|
||||||
for v in resp.triples
|
for v in resp.triples
|
||||||
]
|
]
|
||||||
|
|
||||||
|
return triples
|
||||||
|
|
||||||
class TriplesClientSpec(RequestResponseSpec):
|
class TriplesClientSpec(RequestResponseSpec):
|
||||||
def __init__(
|
def __init__(
|
||||||
self, request_name, response_name,
|
self, request_name, response_name,
|
||||||
|
|
|
||||||
|
|
@ -23,10 +23,6 @@ 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
|
||||||
|
|
|
||||||
|
|
@ -39,15 +39,11 @@ 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,
|
||||||
)
|
)
|
||||||
|
|
||||||
print("ENT>", entities, flush=True)
|
|
||||||
|
|
||||||
entities = [
|
entities = [
|
||||||
str(e)
|
str(e)
|
||||||
for e in entities
|
for e in entities
|
||||||
|
|
@ -65,9 +61,9 @@ class Query:
|
||||||
if e in self.rag.label_cache:
|
if e in self.rag.label_cache:
|
||||||
return self.rag.label_cache[e]
|
return self.rag.label_cache[e]
|
||||||
|
|
||||||
res = await self.rag.triples_client.request(
|
res = await self.rag.triples_client.query(
|
||||||
user=self.user, collection=self.collection,
|
|
||||||
s=e, p=LABEL, o=None, limit=1,
|
s=e, p=LABEL, o=None, limit=1,
|
||||||
|
user=self.user, collection=self.collection,
|
||||||
)
|
)
|
||||||
|
|
||||||
if len(res) == 0:
|
if len(res) == 0:
|
||||||
|
|
@ -100,10 +96,10 @@ class Query:
|
||||||
if path_length > 1:
|
if path_length > 1:
|
||||||
await self.follow_edges(str(triple.o), 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.query(
|
||||||
user=self.user, collection=self.collection,
|
|
||||||
s=None, p=ent, o=None,
|
s=None, p=ent, o=None,
|
||||||
limit=self.triple_limit
|
limit=self.triple_limit,
|
||||||
|
user=self.user, collection=self.collection,
|
||||||
)
|
)
|
||||||
|
|
||||||
for triple in res:
|
for triple in res:
|
||||||
|
|
@ -111,18 +107,20 @@ class Query:
|
||||||
(str(triple.s), str(triple.p), str(triple.o))
|
(str(triple.s), str(triple.p), str(triple.o))
|
||||||
)
|
)
|
||||||
|
|
||||||
res = await self.rag.triples_client.request(
|
res = await self.rag.triples_client.query(
|
||||||
user=self.user, collection=self.collection,
|
|
||||||
s=None, p=None, o=ent,
|
s=None, p=None, o=ent,
|
||||||
limit=self.triple_limit,
|
limit=self.triple_limit,
|
||||||
|
user=self.user, collection=self.collection,
|
||||||
)
|
)
|
||||||
|
|
||||||
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.s.value, subgraph, path_length-1)
|
await self.follow_edges(
|
||||||
|
str(triple.s), subgraph, path_length-1
|
||||||
|
)
|
||||||
|
|
||||||
async def get_subgraph(self, query):
|
async def get_subgraph(self, query):
|
||||||
|
|
||||||
|
|
@ -212,7 +210,7 @@ class GraphRag:
|
||||||
print(kg)
|
print(kg)
|
||||||
print(query)
|
print(query)
|
||||||
|
|
||||||
resp = await self.prompt_client.request_kg_prompt(query, kg)
|
resp = await self.prompt_client.kg_prompt(query, kg)
|
||||||
|
|
||||||
if self.verbose:
|
if self.verbose:
|
||||||
print("Done", flush=True)
|
print("Done", flush=True)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue