2024-11-10 11:44:01 +00:00
|
|
|
"""
|
|
|
|
|
Simple agent infrastructure broadly implements the ReAct flow.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import json
|
|
|
|
|
import re
|
|
|
|
|
import sys
|
2025-07-08 16:19:19 +01:00
|
|
|
import functools
|
2025-07-21 14:31:57 +01:00
|
|
|
import logging
|
|
|
|
|
|
|
|
|
|
logging.basicConfig(level=logging.DEBUG)
|
|
|
|
|
logger = logging.getLogger(__name__)
|
2024-11-10 11:44:01 +00:00
|
|
|
|
2025-04-22 20:21:38 +01:00
|
|
|
from ... base import AgentService, TextCompletionClientSpec, PromptClientSpec
|
2025-07-08 16:19:19 +01:00
|
|
|
from ... base import GraphRagClientSpec, ToolClientSpec
|
2025-04-22 20:21:38 +01:00
|
|
|
|
|
|
|
|
from ... schema import AgentRequest, AgentResponse, AgentStep, Error
|
2024-11-10 11:44:01 +00:00
|
|
|
|
2025-07-16 23:09:32 +01:00
|
|
|
from . tools import KnowledgeQueryImpl, TextCompletionImpl, McpToolImpl, PromptImpl
|
2024-11-10 11:44:01 +00:00
|
|
|
from . agent_manager import AgentManager
|
|
|
|
|
|
|
|
|
|
from . types import Final, Action, Tool, Argument
|
|
|
|
|
|
2025-04-22 20:21:38 +01:00
|
|
|
default_ident = "agent-manager"
|
|
|
|
|
default_max_iterations = 10
|
2024-11-10 11:44:01 +00:00
|
|
|
|
2025-04-22 20:21:38 +01:00
|
|
|
class Processor(AgentService):
|
2024-11-10 11:44:01 +00:00
|
|
|
|
|
|
|
|
def __init__(self, **params):
|
|
|
|
|
|
2025-04-22 20:21:38 +01:00
|
|
|
id = params.get("id")
|
|
|
|
|
|
2025-04-02 16:37:08 +01:00
|
|
|
self.max_iterations = int(
|
|
|
|
|
params.get("max_iterations", default_max_iterations)
|
|
|
|
|
)
|
2024-11-19 21:28:47 +00:00
|
|
|
|
2025-04-02 16:37:08 +01:00
|
|
|
self.config_key = params.get("config_type", "agent")
|
|
|
|
|
|
2024-11-10 11:44:01 +00:00
|
|
|
super(Processor, self).__init__(
|
|
|
|
|
**params | {
|
2025-04-22 20:21:38 +01:00
|
|
|
"id": id,
|
|
|
|
|
"max_iterations": self.max_iterations,
|
|
|
|
|
"config_type": self.config_key,
|
2024-11-10 11:44:01 +00:00
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
2025-04-22 20:21:38 +01:00
|
|
|
self.agent = AgentManager(
|
|
|
|
|
tools=[],
|
|
|
|
|
additional_context="",
|
2024-11-10 11:44:01 +00:00
|
|
|
)
|
|
|
|
|
|
2025-04-22 20:21:38 +01:00
|
|
|
self.config_handlers.append(self.on_tools_config)
|
|
|
|
|
|
|
|
|
|
self.register_specification(
|
|
|
|
|
TextCompletionClientSpec(
|
|
|
|
|
request_name = "text-completion-request",
|
|
|
|
|
response_name = "text-completion-response",
|
|
|
|
|
)
|
2024-11-10 11:44:01 +00:00
|
|
|
)
|
|
|
|
|
|
2025-04-22 20:21:38 +01:00
|
|
|
self.register_specification(
|
|
|
|
|
GraphRagClientSpec(
|
|
|
|
|
request_name = "graph-rag-request",
|
|
|
|
|
response_name = "graph-rag-response",
|
|
|
|
|
)
|
2024-11-10 11:44:01 +00:00
|
|
|
)
|
|
|
|
|
|
2025-04-22 20:21:38 +01:00
|
|
|
self.register_specification(
|
|
|
|
|
PromptClientSpec(
|
|
|
|
|
request_name = "prompt-request",
|
|
|
|
|
response_name = "prompt-response",
|
|
|
|
|
)
|
2024-11-10 11:44:01 +00:00
|
|
|
)
|
|
|
|
|
|
2025-07-08 16:19:19 +01:00
|
|
|
self.register_specification(
|
|
|
|
|
ToolClientSpec(
|
|
|
|
|
request_name = "mcp-tool-request",
|
|
|
|
|
response_name = "mcp-tool-response",
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
2025-04-22 20:21:38 +01:00
|
|
|
async def on_tools_config(self, config, version):
|
2025-04-02 16:37:08 +01:00
|
|
|
|
|
|
|
|
print("Loading configuration version", version)
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
|
|
tools = {}
|
|
|
|
|
|
2025-07-16 23:09:32 +01:00
|
|
|
# Load tool configurations from the new location
|
|
|
|
|
if "tool" in config:
|
|
|
|
|
for tool_id, tool_value in config["tool"].items():
|
|
|
|
|
data = json.loads(tool_value)
|
|
|
|
|
|
|
|
|
|
impl_id = data.get("type")
|
|
|
|
|
name = data.get("name")
|
|
|
|
|
|
|
|
|
|
# Create the appropriate implementation
|
|
|
|
|
if impl_id == "knowledge-query":
|
|
|
|
|
impl = functools.partial(
|
|
|
|
|
KnowledgeQueryImpl,
|
|
|
|
|
collection=data.get("collection")
|
|
|
|
|
)
|
|
|
|
|
arguments = KnowledgeQueryImpl.get_arguments()
|
|
|
|
|
elif impl_id == "text-completion":
|
|
|
|
|
impl = TextCompletionImpl
|
|
|
|
|
arguments = TextCompletionImpl.get_arguments()
|
|
|
|
|
elif impl_id == "mcp-tool":
|
|
|
|
|
impl = functools.partial(
|
|
|
|
|
McpToolImpl,
|
|
|
|
|
mcp_tool_id=data.get("mcp-tool")
|
|
|
|
|
)
|
|
|
|
|
arguments = McpToolImpl.get_arguments()
|
|
|
|
|
elif impl_id == "prompt":
|
|
|
|
|
# For prompt tools, arguments come from config
|
|
|
|
|
config_args = data.get("arguments", [])
|
|
|
|
|
arguments = [
|
|
|
|
|
Argument(
|
|
|
|
|
name=arg.get("name"),
|
|
|
|
|
type=arg.get("type"),
|
|
|
|
|
description=arg.get("description")
|
|
|
|
|
)
|
|
|
|
|
for arg in config_args
|
|
|
|
|
]
|
|
|
|
|
impl = functools.partial(
|
|
|
|
|
PromptImpl,
|
|
|
|
|
template_id=data.get("template"),
|
|
|
|
|
arguments=arguments
|
|
|
|
|
)
|
|
|
|
|
else:
|
|
|
|
|
raise RuntimeError(
|
|
|
|
|
f"Tool type {impl_id} not known"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
tools[name] = Tool(
|
|
|
|
|
name=name,
|
|
|
|
|
description=data.get("description"),
|
|
|
|
|
implementation=impl,
|
|
|
|
|
config=data, # Store full config for reference
|
|
|
|
|
arguments=arguments,
|
2025-04-02 16:37:08 +01:00
|
|
|
)
|
2025-07-16 23:09:32 +01:00
|
|
|
|
|
|
|
|
# Load additional context from agent config if it exists
|
|
|
|
|
additional = None
|
|
|
|
|
if self.config_key in config:
|
|
|
|
|
agent_config = config[self.config_key]
|
|
|
|
|
additional = agent_config.get("additional-context", None)
|
|
|
|
|
|
2025-04-02 16:37:08 +01:00
|
|
|
self.agent = AgentManager(
|
|
|
|
|
tools=tools,
|
|
|
|
|
additional_context=additional
|
|
|
|
|
)
|
|
|
|
|
|
2025-07-16 23:09:32 +01:00
|
|
|
print(f"Loaded {len(tools)} tools", flush=True)
|
|
|
|
|
print("Tool configuration reloaded.", flush=True)
|
2025-04-02 16:37:08 +01:00
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
|
2025-04-22 20:21:38 +01:00
|
|
|
print("on_tools_config Exception:", e, flush=True)
|
2025-04-02 16:37:08 +01:00
|
|
|
print("Configuration reload failed", flush=True)
|
2024-11-10 11:44:01 +00:00
|
|
|
|
2025-04-22 20:21:38 +01:00
|
|
|
async def agent_request(self, request, respond, next, flow):
|
2024-11-10 11:44:01 +00:00
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
2025-04-22 20:21:38 +01:00
|
|
|
if request.history:
|
2024-11-10 11:44:01 +00:00
|
|
|
history = [
|
|
|
|
|
Action(
|
|
|
|
|
thought=h.thought,
|
|
|
|
|
name=h.action,
|
|
|
|
|
arguments=h.arguments,
|
|
|
|
|
observation=h.observation
|
|
|
|
|
)
|
2025-04-22 20:21:38 +01:00
|
|
|
for h in request.history
|
2024-11-10 11:44:01 +00:00
|
|
|
]
|
|
|
|
|
else:
|
|
|
|
|
history = []
|
|
|
|
|
|
2025-04-22 20:21:38 +01:00
|
|
|
print(f"Question: {request.question}", flush=True)
|
2024-11-10 11:44:01 +00:00
|
|
|
|
2024-11-19 21:28:47 +00:00
|
|
|
if len(history) >= self.max_iterations:
|
2024-11-10 11:44:01 +00:00
|
|
|
raise RuntimeError("Too many agent iterations")
|
|
|
|
|
|
|
|
|
|
print(f"History: {history}", flush=True)
|
|
|
|
|
|
2025-02-15 12:25:26 +00:00
|
|
|
async def think(x):
|
2024-11-10 11:44:01 +00:00
|
|
|
|
|
|
|
|
print(f"Think: {x}", flush=True)
|
|
|
|
|
|
|
|
|
|
r = AgentResponse(
|
|
|
|
|
answer=None,
|
|
|
|
|
error=None,
|
|
|
|
|
thought=x,
|
|
|
|
|
observation=None,
|
|
|
|
|
)
|
|
|
|
|
|
2025-04-22 20:21:38 +01:00
|
|
|
await respond(r)
|
2024-11-10 11:44:01 +00:00
|
|
|
|
2025-02-15 12:25:26 +00:00
|
|
|
async def observe(x):
|
2024-11-10 11:44:01 +00:00
|
|
|
|
|
|
|
|
print(f"Observe: {x}", flush=True)
|
|
|
|
|
|
|
|
|
|
r = AgentResponse(
|
|
|
|
|
answer=None,
|
|
|
|
|
error=None,
|
|
|
|
|
thought=None,
|
|
|
|
|
observation=x,
|
|
|
|
|
)
|
|
|
|
|
|
2025-04-22 20:21:38 +01:00
|
|
|
await respond(r)
|
2024-11-10 11:44:01 +00:00
|
|
|
|
2025-07-08 16:19:19 +01:00
|
|
|
print("Call React", flush=True)
|
|
|
|
|
|
2025-04-22 20:21:38 +01:00
|
|
|
act = await self.agent.react(
|
|
|
|
|
question = request.question,
|
|
|
|
|
history = history,
|
|
|
|
|
think = think,
|
|
|
|
|
observe = observe,
|
|
|
|
|
context = flow,
|
|
|
|
|
)
|
2024-11-10 11:44:01 +00:00
|
|
|
|
|
|
|
|
print(f"Action: {act}", flush=True)
|
|
|
|
|
|
2025-04-22 20:21:38 +01:00
|
|
|
if isinstance(act, Final):
|
2024-11-10 11:44:01 +00:00
|
|
|
|
2025-04-22 20:21:38 +01:00
|
|
|
print("Send final response...", flush=True)
|
2024-11-10 11:44:01 +00:00
|
|
|
|
2025-07-21 14:31:57 +01:00
|
|
|
if isinstance(act.final, str):
|
|
|
|
|
f = act.final
|
|
|
|
|
else:
|
|
|
|
|
f = json.dumps(act.final)
|
|
|
|
|
|
2024-11-10 11:44:01 +00:00
|
|
|
r = AgentResponse(
|
|
|
|
|
answer=act.final,
|
|
|
|
|
error=None,
|
|
|
|
|
thought=None,
|
|
|
|
|
)
|
|
|
|
|
|
2025-04-22 20:21:38 +01:00
|
|
|
await respond(r)
|
2024-11-10 11:44:01 +00:00
|
|
|
|
|
|
|
|
print("Done.", flush=True)
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
2025-04-22 20:21:38 +01:00
|
|
|
print("Send next...", flush=True)
|
|
|
|
|
|
2024-11-10 11:44:01 +00:00
|
|
|
history.append(act)
|
|
|
|
|
|
|
|
|
|
r = AgentRequest(
|
2025-04-22 20:21:38 +01:00
|
|
|
question=request.question,
|
|
|
|
|
plan=request.plan,
|
|
|
|
|
state=request.state,
|
2024-11-10 11:44:01 +00:00
|
|
|
history=[
|
|
|
|
|
AgentStep(
|
|
|
|
|
thought=h.thought,
|
|
|
|
|
action=h.name,
|
|
|
|
|
arguments=h.arguments,
|
|
|
|
|
observation=h.observation
|
|
|
|
|
)
|
|
|
|
|
for h in history
|
|
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
|
2025-04-22 20:21:38 +01:00
|
|
|
await next(r)
|
2024-11-10 11:44:01 +00:00
|
|
|
|
|
|
|
|
print("Done.", flush=True)
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
|
2025-04-22 20:21:38 +01:00
|
|
|
print(f"agent_request Exception: {e}")
|
2024-11-10 11:44:01 +00:00
|
|
|
|
|
|
|
|
print("Send error response...", flush=True)
|
|
|
|
|
|
|
|
|
|
r = AgentResponse(
|
|
|
|
|
error=Error(
|
|
|
|
|
type = "agent-error",
|
|
|
|
|
message = str(e),
|
|
|
|
|
),
|
|
|
|
|
response=None,
|
|
|
|
|
)
|
|
|
|
|
|
2025-04-22 20:21:38 +01:00
|
|
|
await respond(r)
|
2024-11-10 11:44:01 +00:00
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def add_args(parser):
|
|
|
|
|
|
2025-04-22 20:21:38 +01:00
|
|
|
AgentService.add_args(parser)
|
2024-11-10 11:44:01 +00:00
|
|
|
|
2024-11-19 21:28:47 +00:00
|
|
|
parser.add_argument(
|
|
|
|
|
'--max-iterations',
|
|
|
|
|
default=default_max_iterations,
|
|
|
|
|
help=f'Maximum number of react iterations (default: {default_max_iterations})',
|
|
|
|
|
)
|
|
|
|
|
|
2025-04-02 16:37:08 +01:00
|
|
|
parser.add_argument(
|
|
|
|
|
'--config-type',
|
|
|
|
|
default="agent",
|
|
|
|
|
help=f'Configuration key for prompts (default: agent)',
|
|
|
|
|
)
|
|
|
|
|
|
2024-11-10 11:44:01 +00:00
|
|
|
def run():
|
2025-04-22 20:21:38 +01:00
|
|
|
Processor.launch(default_ident, __doc__)
|
2024-11-10 11:44:01 +00:00
|
|
|
|