mirror of
https://github.com/trustgraph-ai/trustgraph.git
synced 2026-07-18 01:31:02 +02:00
Spec clients
This commit is contained in:
parent
c5cd70dacb
commit
93bb2adb9e
9 changed files with 140 additions and 56 deletions
|
|
@ -49,4 +49,3 @@ def run():
|
|||
|
||||
Processor.launch(default_ident, __doc__)
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ Output is entity plus embedding.
|
|||
from ... schema import EntityContexts, EntityEmbeddings, GraphEmbeddings
|
||||
from ... schema import EmbeddingsRequest, EmbeddingsResponse
|
||||
|
||||
from ... base import FlowProcessor, RequestResponseSpec, ConsumerSpec
|
||||
from ... base import FlowProcessor, EmbeddingsClientSpec, ConsumerSpec
|
||||
from ... base import ProducerSpec
|
||||
|
||||
default_ident = "graph-embeddings"
|
||||
|
|
@ -34,11 +34,9 @@ class Processor(FlowProcessor):
|
|||
)
|
||||
|
||||
self.register_specification(
|
||||
RequestResponseSpec(
|
||||
EmbeddingsClientSpec(
|
||||
request_name = "embeddings-request",
|
||||
request_schema = EmbeddingsRequest,
|
||||
response_name = "embeddings-response",
|
||||
response_schema = EmbeddingsResponse,
|
||||
)
|
||||
)
|
||||
|
||||
|
|
@ -60,10 +58,8 @@ class Processor(FlowProcessor):
|
|||
|
||||
for entity in v.entities:
|
||||
|
||||
resp = await flow("embeddings-request").request(
|
||||
EmbeddingsRequest(
|
||||
text = entity.context
|
||||
)
|
||||
resp = await flow("embeddings-request").embed(
|
||||
text = entity.context
|
||||
)
|
||||
|
||||
vectors = resp.vectors
|
||||
|
|
|
|||
|
|
@ -13,8 +13,8 @@ from .... schema import EntityContext, EntityContexts
|
|||
from .... schema import PromptRequest, PromptResponse
|
||||
from .... rdf import TRUSTGRAPH_ENTITIES, DEFINITION, RDF_LABEL, SUBJECT_OF
|
||||
|
||||
from .... base import FlowProcessor, RequestResponseSpec, ConsumerSpec
|
||||
from .... base import ProducerSpec
|
||||
from .... base import FlowProcessor, ConsumerSpec, ProducerSpec
|
||||
from .... base import PromptClientSpec
|
||||
|
||||
DEFINITION_VALUE = Value(value=DEFINITION, is_uri=True)
|
||||
RDF_LABEL_VALUE = Value(value=RDF_LABEL, is_uri=True)
|
||||
|
|
@ -43,11 +43,9 @@ class Processor(FlowProcessor):
|
|||
)
|
||||
|
||||
self.register_specification(
|
||||
RequestResponseSpec(
|
||||
PromptClientSpec(
|
||||
request_name = "prompt-request",
|
||||
request_schema = PromptRequest,
|
||||
response_name = "prompt-response",
|
||||
response_schema = PromptResponse,
|
||||
)
|
||||
)
|
||||
|
||||
|
|
@ -102,24 +100,14 @@ class Processor(FlowProcessor):
|
|||
|
||||
try:
|
||||
|
||||
resp = await flow("prompt-request").request(
|
||||
PromptRequest(
|
||||
id="extract-definitions",
|
||||
terms={
|
||||
"text": json.dumps(chunk)
|
||||
},
|
||||
)
|
||||
defs = await flow("prompt-request").extract_definitions(
|
||||
text = chunk
|
||||
)
|
||||
print("Response", resp, flush=True)
|
||||
|
||||
if resp.error is not None:
|
||||
print("Error:", resp.error.message, flush=True)
|
||||
raise RuntimeError(resp.error.message)
|
||||
print("Response", defs, flush=True)
|
||||
|
||||
if resp.object is None:
|
||||
raise RuntimeError("Expecting object in prompt response")
|
||||
|
||||
defs = json.loads(resp.object)
|
||||
if type(defs) != list:
|
||||
raise RuntimeError("Expecting array in prompt response")
|
||||
|
||||
except Exception as e:
|
||||
print("Prompt exception:", e, flush=True)
|
||||
|
|
|
|||
|
|
@ -13,8 +13,8 @@ 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
|
||||
from .... base import FlowProcessor, ConsumerSpec, ProducerSpec
|
||||
from .... base import PromptClientSpec
|
||||
|
||||
RDF_LABEL_VALUE = Value(value=RDF_LABEL, is_uri=True)
|
||||
SUBJECT_OF_VALUE = Value(value=SUBJECT_OF, is_uri=True)
|
||||
|
|
@ -42,11 +42,9 @@ class Processor(FlowProcessor):
|
|||
)
|
||||
|
||||
self.register_specification(
|
||||
RequestResponseSpec(
|
||||
PromptClientSpec(
|
||||
request_name = "prompt-request",
|
||||
request_schema = PromptRequest,
|
||||
response_name = "prompt-response",
|
||||
response_schema = PromptResponse,
|
||||
)
|
||||
)
|
||||
|
||||
|
|
@ -86,24 +84,14 @@ class Processor(FlowProcessor):
|
|||
|
||||
try:
|
||||
|
||||
resp = await flow("prompt-request").request(
|
||||
PromptRequest(
|
||||
id="extract-relationships",
|
||||
terms={
|
||||
"text": json.dumps(chunk)
|
||||
},
|
||||
)
|
||||
rels = await flow("prompt-request").extract_relationships(
|
||||
text = chunk
|
||||
)
|
||||
print("Response", resp, flush=True)
|
||||
|
||||
if resp.error is not None:
|
||||
print("Error:", resp.error.message, flush=True)
|
||||
raise RuntimeError(resp.error.message)
|
||||
print("Response", rels, flush=True)
|
||||
|
||||
if resp.object is None:
|
||||
raise RuntimeError("Expecting object in prompt response")
|
||||
|
||||
rels = json.loads(resp.object)
|
||||
if type(rels) != list:
|
||||
raise RuntimeError("Expecting array in prompt response")
|
||||
|
||||
except Exception as e:
|
||||
print("Prompt exception:", e, flush=True)
|
||||
|
|
@ -118,9 +106,9 @@ class Processor(FlowProcessor):
|
|||
|
||||
for rel in rels:
|
||||
|
||||
s = rel["s"]
|
||||
p = rel["p"]
|
||||
o = rel["o"]
|
||||
s = rel["subject"]
|
||||
p = rel["predicate"]
|
||||
o = rel["object"]
|
||||
|
||||
if s == "": continue
|
||||
if p == "": continue
|
||||
|
|
@ -136,7 +124,7 @@ class Processor(FlowProcessor):
|
|||
p_uri = self.to_uri(p)
|
||||
p_value = Value(value=str(p_uri), is_uri=True)
|
||||
|
||||
if rel["o_entity"]:
|
||||
if rel["object-entity"]:
|
||||
o_uri = self.to_uri(o)
|
||||
o_value = Value(value=str(o_uri), is_uri=True)
|
||||
else:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue