mirror of
https://github.com/trustgraph-ai/trustgraph.git
synced 2026-04-26 00:46:22 +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
|
|
@ -39,8 +39,9 @@ class BaseProcessor:
|
|||
|
||||
def __del__(self):
|
||||
|
||||
if self.client:
|
||||
self.client.close()
|
||||
if hasattr(self, "client"):
|
||||
if self.client:
|
||||
self.client.close()
|
||||
|
||||
@staticmethod
|
||||
def add_args(parser):
|
||||
|
|
|
|||
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
|
||||
|
|
|
|||
|
|
@ -8,4 +8,5 @@ from . topic import *
|
|||
from . graph import *
|
||||
from . retrieval import *
|
||||
from . metadata import *
|
||||
from . agent import *
|
||||
|
||||
|
|
|
|||
37
trustgraph-base/trustgraph/schema/agent.py
Normal file
37
trustgraph-base/trustgraph/schema/agent.py
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
|
||||
from pulsar.schema import Record, String, Array, Map
|
||||
|
||||
from . topic import topic
|
||||
from . types import Error
|
||||
|
||||
############################################################################
|
||||
|
||||
# Prompt services, abstract the prompt generation
|
||||
|
||||
class AgentStep(Record):
|
||||
thought = String()
|
||||
action = String()
|
||||
arguments = Map(String())
|
||||
observation = String()
|
||||
|
||||
class AgentRequest(Record):
|
||||
question = String()
|
||||
plan = String()
|
||||
state = String()
|
||||
history = Array(AgentStep())
|
||||
|
||||
class AgentResponse(Record):
|
||||
answer = String()
|
||||
error = Error()
|
||||
thought = String()
|
||||
observation = String()
|
||||
|
||||
agent_request_queue = topic(
|
||||
'agent', kind='non-persistent', namespace='request'
|
||||
)
|
||||
agent_response_queue = topic(
|
||||
'agent', kind='non-persistent', namespace='response'
|
||||
)
|
||||
|
||||
############################################################################
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue