mirror of
https://github.com/trustgraph-ai/trustgraph.git
synced 2026-05-20 04:45:12 +02:00
Feature/agent manager (#146)
* Agent schema * Agent working through client * Add agent-manager-react command line * test-agent test script * Add tg-invoke-agent CLI
This commit is contained in:
parent
5140f8834d
commit
36cdeab588
19 changed files with 968 additions and 3 deletions
64
trustgraph-base/trustgraph/clients/agent_client.py
Normal file
64
trustgraph-base/trustgraph/clients/agent_client.py
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
|
||||
import _pulsar
|
||||
|
||||
from .. schema import AgentRequest, AgentResponse
|
||||
from .. schema import agent_request_queue
|
||||
from .. schema import agent_response_queue
|
||||
from . base import BaseClient
|
||||
|
||||
# Ugly
|
||||
ERROR=_pulsar.LoggerLevel.Error
|
||||
WARN=_pulsar.LoggerLevel.Warn
|
||||
INFO=_pulsar.LoggerLevel.Info
|
||||
DEBUG=_pulsar.LoggerLevel.Debug
|
||||
|
||||
class AgentClient(BaseClient):
|
||||
|
||||
def __init__(
|
||||
self, log_level=ERROR,
|
||||
subscriber=None,
|
||||
input_queue=None,
|
||||
output_queue=None,
|
||||
pulsar_host="pulsar://pulsar:6650",
|
||||
):
|
||||
|
||||
if input_queue is None: input_queue = agent_request_queue
|
||||
if output_queue is None: output_queue = agent_response_queue
|
||||
|
||||
super(AgentClient, self).__init__(
|
||||
log_level=log_level,
|
||||
subscriber=subscriber,
|
||||
input_queue=input_queue,
|
||||
output_queue=output_queue,
|
||||
pulsar_host=pulsar_host,
|
||||
input_schema=AgentRequest,
|
||||
output_schema=AgentResponse,
|
||||
)
|
||||
|
||||
def request(
|
||||
self,
|
||||
question,
|
||||
think=None,
|
||||
observe=None,
|
||||
timeout=300
|
||||
):
|
||||
|
||||
def inspect(x):
|
||||
|
||||
if x.thought and think:
|
||||
think(x.thought)
|
||||
return
|
||||
|
||||
if x.observation and observe:
|
||||
observe(x.observation)
|
||||
return
|
||||
|
||||
if x.answer:
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
return self.call(
|
||||
question=question, inspect=inspect, timeout=timeout
|
||||
).answer
|
||||
|
||||
|
|
@ -59,10 +59,14 @@ class BaseClient:
|
|||
def call(self, **args):
|
||||
|
||||
timeout = args.get("timeout", DEFAULT_TIMEOUT)
|
||||
inspect = args.get("inspect", lambda x: True)
|
||||
|
||||
if "timeout" in args:
|
||||
del args["timeout"]
|
||||
|
||||
if "inspect" in args:
|
||||
del args["inspect"]
|
||||
|
||||
id = str(uuid.uuid4())
|
||||
|
||||
r = self.input_schema(**args)
|
||||
|
|
@ -103,6 +107,10 @@ class BaseClient:
|
|||
f"{value.error.type}: {value.error.message}"
|
||||
)
|
||||
|
||||
complete = inspect(value)
|
||||
|
||||
if not complete: continue
|
||||
|
||||
resp = msg.value()
|
||||
self.consumer.acknowledge(msg)
|
||||
return resp
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue