Compare commits

...

2 Commits

Author SHA1 Message Date
openhands
227e2cc83c Revert delegate agent to use its own LLM config 2025-03-21 16:13:21 +00:00
openhands
ed2c0a5ecf Create LLM outside agent controller and pass it in directly 2025-03-21 16:11:00 +00:00
3 changed files with 20 additions and 11 deletions

View File

@@ -114,7 +114,7 @@ class AgentController:
"""Initializes a new instance of the AgentController class.
Args:
agent: The agent instance to control.
agent: The agent instance to control. The agent should already have an initialized LLM.
event_stream: The event stream to publish events to.
max_iterations: The maximum number of iterations the agent can run.
max_budget_per_task: The maximum budget (in USD) allowed per task, beyond which the agent will stop.
@@ -602,6 +602,7 @@ class AgentController:
agent_cls: Type[Agent] = Agent.get_cls(action.agent)
agent_config = self.agent_configs.get(action.agent, self.agent.config)
llm_config = self.agent_to_llm_config.get(action.agent, self.agent.llm.config)
# Create a new LLM instance for the delegate with its own config
llm = LLM(config=llm_config, retry_listener=self._notify_on_llm_retry)
delegate_agent = agent_cls(llm=llm, config=agent_config)
state = State(

View File

@@ -166,12 +166,17 @@ def create_memory(
return memory
def create_agent(config: AppConfig) -> Agent:
def create_agent(config: AppConfig, llm: LLM | None = None) -> Agent:
agent_cls: Type[Agent] = Agent.get_cls(config.default_agent)
agent_config = config.get_agent_config(config.default_agent)
llm_config = config.get_llm_config_from_agent(config.default_agent)
# Create LLM if not provided
if llm is None:
llm_config = config.get_llm_config_from_agent(config.default_agent)
llm = LLM(config=llm_config)
agent = agent_cls(
llm=LLM(config=llm_config),
llm=llm,
config=agent_config,
)
@@ -197,6 +202,7 @@ def create_controller(
except Exception as e:
logger.debug(f'Cannot restore agent state: {e}')
# The agent already has an initialized LLM
controller = AgentController(
agent=agent,
max_iterations=config.max_iterations,

View File

@@ -139,9 +139,7 @@ class AgentSession:
if git_provider_tokens:
provider_handler = ProviderHandler(provider_tokens=git_provider_tokens)
await provider_handler.set_event_stream_secrets(
self.event_stream
)
await provider_handler.set_event_stream_secrets(self.event_stream)
if not self._closed:
if initial_message:
@@ -243,12 +241,15 @@ class AgentSession:
headless_mode=False,
attach_to_existing=False,
git_provider_tokens=git_provider_tokens,
user_id=self.user_id
user_id=self.user_id,
)
else:
provider_handler = ProviderHandler(provider_tokens=git_provider_tokens or cast(PROVIDER_TOKEN_TYPE, MappingProxyType({})))
provider_handler = ProviderHandler(
provider_tokens=git_provider_tokens
or cast(PROVIDER_TOKEN_TYPE, MappingProxyType({}))
)
env_vars = await provider_handler.get_env_vars(expose_secrets=True)
self.runtime = runtime_cls(
config=config,
event_stream=self.event_stream,
@@ -257,7 +258,7 @@ class AgentSession:
status_callback=self._status_callback,
headless_mode=False,
attach_to_existing=False,
env_vars=env_vars
env_vars=env_vars,
)
# FIXME: this sleep is a terrible hack.
@@ -329,6 +330,7 @@ class AgentSession:
)
self.logger.debug(msg)
# The agent already has an initialized LLM
controller = AgentController(
sid=self.sid,
event_stream=self.event_stream,