Move max_calls_per_parent_child to agent config in the UI

This commit is contained in:
akhisud3195 2025-05-08 21:34:46 +05:30
parent d01248efb1
commit 4df6d832c2
10 changed files with 209 additions and 314 deletions

View file

@ -1,6 +1,5 @@
import traceback
from quart import Quart, request, jsonify, Response
from datetime import datetime
from functools import wraps
import os
import json
@ -9,7 +8,6 @@ from hypercorn.asyncio import serve
import asyncio
from src.graph.core import run_turn_streamed
from src.graph.tools import RAG_TOOL, CLOSE_CHAT_TOOL
from src.utils.common import read_json_from_file
app = Quart(__name__)
@ -17,12 +15,6 @@ master_config = read_json_from_file("./configs/default_config.json")
print("Master config:", master_config)
# Get environment variables with defaults
MAX_CALLS_PER_CHILD_AGENT = 1
try:
MAX_CALLS_PER_CHILD_AGENT = int(os.environ.get('MAX_CALLS_PER_CHILD_AGENT'))
except Exception as e:
print(f"Error getting MAX_CALLS_PER_CHILD_AGENT: {e}, using default of 1")
ENABLE_TRACING = False
try:
ENABLE_TRACING = os.environ.get('ENABLE_TRACING').lower() == 'true'
@ -103,9 +95,7 @@ async def chat():
tool_configs=data.get("tools", []),
prompt_configs=data.get("prompts", []),
start_turn_with_start_agent=master_config.get("start_turn_with_start_agent", False),
max_calls_per_child_agent=MAX_CALLS_PER_CHILD_AGENT,
state=data.get("state", {}),
additional_tool_configs=[RAG_TOOL, CLOSE_CHAT_TOOL],
complete_request=data,
enable_tracing=ENABLE_TRACING
):
@ -172,9 +162,7 @@ async def chat_stream():
tool_configs=request_data.get("tools", []),
prompt_configs=request_data.get("prompts", []),
start_turn_with_start_agent=master_config.get("start_turn_with_start_agent", False),
max_calls_per_child_agent=MAX_CALLS_PER_CHILD_AGENT,
state=request_data.get("state", {}),
additional_tool_configs=[RAG_TOOL, CLOSE_CHAT_TOOL],
complete_request=request_data,
enable_tracing=ENABLE_TRACING
):

View file

@ -93,9 +93,7 @@ async def run_turn_streamed(
tool_configs,
prompt_configs,
start_turn_with_start_agent,
max_calls_per_child_agent,
state={},
additional_tool_configs=[],
complete_request={},
enable_tracing=None
):
@ -156,7 +154,11 @@ async def run_turn_streamed(
# Initialize agents and get external tools
new_agents = get_agents(agent_configs=agent_configs, tool_configs=tool_configs, complete_request=complete_request)
new_agents = get_agents(
agent_configs=agent_configs,
tool_configs=tool_configs,
complete_request=complete_request
)
new_agents = add_child_transfer_related_instructions_to_agents(new_agents)
new_agents = add_openai_recommended_instructions_to_agents(new_agents)
last_agent_name = get_last_agent_name(
@ -226,7 +228,7 @@ async def run_turn_streamed(
# Check if we've already called this child agent too many times
parent_child_key = f"{current_agent.name}:{event.new_agent.name}"
current_count = child_call_counts.get(parent_child_key, 0)
if current_count >= max_calls_per_child_agent:
if current_count >= event.new_agent.max_calls_per_parent_agent:
print(f"Skipping transfer from {current_agent.name} to {event.new_agent.name} (max calls reached from parent to child)")
continue

View file

@ -176,6 +176,8 @@ def get_rag_tool(config: dict, complete_request: dict) -> FunctionTool:
return tool
else:
return None
DEFAULT_MAX_CALLS_PER_PARENT_AGENT = 3
def get_agents(agent_configs, tool_configs, complete_request):
"""
@ -242,9 +244,12 @@ def get_agents(agent_configs, tool_configs, complete_request):
# add the name and description to the agent instructions
agent_instructions = f"## Your Name\n{agent_config['name']}\n\n## Description\n{agent_config['description']}\n\n## Instructions\n{agent_config['instructions']}"
try:
# Identify the model
model_name = agent_config["model"] if agent_config["model"] else PROVIDER_DEFAULT_MODEL
print(f"Using model: {model_name}")
model=OpenAIChatCompletionsModel(model=model_name, openai_client=client) if client else agent_config["model"]
# Create the agent object
new_agent = NewAgent(
name=agent_config["name"],
instructions=agent_instructions,
@ -254,6 +259,13 @@ def get_agents(agent_configs, tool_configs, complete_request):
model_settings=ModelSettings(temperature=0.0)
)
# Set the max calls per parent agent
new_agent.max_calls_per_parent_agent = agent_config.get("maxCallsPerParentAgent", DEFAULT_MAX_CALLS_PER_PARENT_AGENT)
if not agent_config.get("maxCallsPerParentAgent", None):
print(f"WARNING: Max calls per parent agent not received for agent {new_agent.name}. Using rowboat_agents default of {DEFAULT_MAX_CALLS_PER_PARENT_AGENT}")
else:
print(f"Max calls per parent agent for agent {new_agent.name}: {new_agent.max_calls_per_parent_agent}")
# Handle the connected agents
new_agent_to_children[agent_config["name"]] = agent_config.get("connectedAgents", [])
new_agent_name_to_index[agent_config["name"]] = len(new_agents)
new_agents.append(new_agent)
@ -268,7 +280,7 @@ def get_agents(agent_configs, tool_configs, complete_request):
new_agent.handoffs = []
# Look up the agent's children from the old agent and create a list called handoffs in new_agent with pointers to the children in new_agents
new_agent.handoffs = [new_agents[new_agent_name_to_index[child]] for child in new_agent_to_children[new_agent.name]]
print("Returning created agents")
print("="*100)
return new_agents