mirror of
https://github.com/trustgraph-ai/trustgraph.git
synced 2026-07-22 03:31:02 +02:00
Fighting agent manager
This commit is contained in:
parent
315f44b5d4
commit
78f3d30d38
3 changed files with 48 additions and 20 deletions
|
|
@ -4,9 +4,9 @@ from .. schema import AgentRequest, AgentResponse
|
||||||
from .. knowledge import Uri, Literal
|
from .. knowledge import Uri, Literal
|
||||||
|
|
||||||
class AgentClient(RequestResponse):
|
class AgentClient(RequestResponse):
|
||||||
async def request(self, question, plan=None, state=None,
|
async def invoke(self, recipient, question, plan=None, state=None,
|
||||||
history=[], timeout=300):
|
history=[], timeout=300):
|
||||||
|
|
||||||
resp = await self.request(
|
resp = await self.request(
|
||||||
AgentRequest(
|
AgentRequest(
|
||||||
question = question,
|
question = question,
|
||||||
|
|
@ -18,12 +18,10 @@ class AgentClient(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)
|
||||||
|
|
||||||
return resp
|
return resp.answer
|
||||||
|
|
||||||
class AgentClientSpec(RequestResponseSpec):
|
class AgentClientSpec(RequestResponseSpec):
|
||||||
def __init__(
|
def __init__(
|
||||||
|
|
|
||||||
|
|
@ -221,6 +221,11 @@ class Processor(AgentService):
|
||||||
|
|
||||||
print("Send final response...", flush=True)
|
print("Send final response...", flush=True)
|
||||||
|
|
||||||
|
if isinstance(act.final, str):
|
||||||
|
f = act.final
|
||||||
|
else:
|
||||||
|
f = json.dumps(act.final)
|
||||||
|
|
||||||
r = AgentResponse(
|
r = AgentResponse(
|
||||||
answer=act.final,
|
answer=act.final,
|
||||||
error=None,
|
error=None,
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,9 @@
|
||||||
|
import re
|
||||||
import json
|
import json
|
||||||
import urllib.parse
|
import urllib.parse
|
||||||
|
|
||||||
from ....schema import Chunk, Triple, Triples, Metadata, Value
|
from ....schema import Chunk, Triple, Triples, Metadata, Value
|
||||||
from ....schema import AgentRequest, AgentResponse, EntityContext, EntityContexts
|
from ....schema import EntityContext, EntityContexts
|
||||||
from ....schema import ConfigRequest, ConfigResponse
|
|
||||||
|
|
||||||
from ....rdf import TRUSTGRAPH_ENTITIES, RDF_LABEL, SUBJECT_OF, DEFINITION
|
from ....rdf import TRUSTGRAPH_ENTITIES, RDF_LABEL, SUBJECT_OF, DEFINITION
|
||||||
|
|
||||||
|
|
@ -74,7 +74,7 @@ class Processor(FlowProcessor):
|
||||||
|
|
||||||
async def on_prompt_config(self, config, version):
|
async def on_prompt_config(self, config, version):
|
||||||
|
|
||||||
print("Loading configuration version", version)
|
print("Loading configuration version", version, flush=True)
|
||||||
|
|
||||||
if self.config_key not in config:
|
if self.config_key not in config:
|
||||||
print(f"No key {self.config_key} in config", flush=True)
|
print(f"No key {self.config_key} in config", flush=True)
|
||||||
|
|
@ -108,6 +108,17 @@ class Processor(FlowProcessor):
|
||||||
ecs.entity_contexts = entity_contexts
|
ecs.entity_contexts = entity_contexts
|
||||||
pub.publish(ecs)
|
pub.publish(ecs)
|
||||||
|
|
||||||
|
def parse_json(self, text):
|
||||||
|
json_match = re.search(r'```(?:json)?(.*?)```', text, re.DOTALL)
|
||||||
|
|
||||||
|
if json_match:
|
||||||
|
json_str = json_match.group(1).strip()
|
||||||
|
else:
|
||||||
|
# If no delimiters, assume the entire output is JSON
|
||||||
|
json_str = text.strip()
|
||||||
|
|
||||||
|
return json.loads(json_str)
|
||||||
|
|
||||||
async def on_message(self, msg, consumer, flow):
|
async def on_message(self, msg, consumer, flow):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
@ -126,22 +137,36 @@ class Processor(FlowProcessor):
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
print("Prompt:", prompt)
|
print("Prompt:", prompt, flush=True)
|
||||||
|
|
||||||
|
async def handle(response):
|
||||||
|
|
||||||
|
print("Response:", response, flush=True)
|
||||||
|
|
||||||
|
print("Done?", response.answer is not None, flush=True)
|
||||||
|
|
||||||
|
if response.error is not None:
|
||||||
|
raise RuntimeError(response.error)
|
||||||
|
|
||||||
|
if response.answer is not None:
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
|
||||||
# Send to agent API
|
# Send to agent API
|
||||||
agent_response = await flow("agent-request").request(
|
agent_response = await flow("agent-request").invoke(
|
||||||
question=prompt
|
recipient = handle,
|
||||||
|
question = prompt
|
||||||
)
|
)
|
||||||
|
|
||||||
if agent_response.error:
|
|
||||||
raise Exception(f"Agent error: {agent_response.error}")
|
|
||||||
|
|
||||||
print("response:", agent_response)
|
if agent_response.error:
|
||||||
return
|
raise RuntimeError(f"Agent error: {agent_response.error}")
|
||||||
|
|
||||||
|
print("response:", agent_response, flush=True)
|
||||||
|
|
||||||
# Parse JSON response
|
# Parse JSON response
|
||||||
try:
|
try:
|
||||||
extraction_data = json.loads(agent_response.answer)
|
extraction_data = self.parse_json(agent_response.answer)
|
||||||
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}")
|
||||||
|
|
||||||
|
|
@ -152,17 +177,17 @@ class Processor(FlowProcessor):
|
||||||
|
|
||||||
# Emit outputs
|
# Emit outputs
|
||||||
if triples:
|
if triples:
|
||||||
self.emit_triples(flow.producer("triples"), msg.metadata, triples)
|
self.emit_triples(flow("triples"), msg.metadata, triples)
|
||||||
|
|
||||||
if entity_contexts:
|
if entity_contexts:
|
||||||
self.emit_entity_contexts(
|
self.emit_entity_contexts(
|
||||||
flow.producer("entity-contexts"),
|
flow("entity-contexts"),
|
||||||
msg.metadata,
|
msg.metadata,
|
||||||
entity_contexts
|
entity_contexts
|
||||||
)
|
)
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Error processing chunk: {e}")
|
print(f"Error processing chunk: {e}", flush=True)
|
||||||
raise
|
raise
|
||||||
|
|
||||||
def process_extraction_data(self, data, metadata):
|
def process_extraction_data(self, data, metadata):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue