forked from All-Hands-AI/OpenHands
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
17 changed files
with
243 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
from openhands.agenthub.planner_agent.agent import PlannerAgent | ||
from openhands.controller.agent import Agent | ||
|
||
Agent.register('PlannerAgent', PlannerAgent) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
from openhands.agenthub.planner_agent.prompt import get_prompt_and_images | ||
from openhands.agenthub.planner_agent.response_parser import PlannerResponseParser | ||
from openhands.controller.agent import Agent | ||
from openhands.controller.state.state import State | ||
from openhands.core.config import AgentConfig | ||
from openhands.core.message import ImageContent, Message, TextContent | ||
from openhands.events.action import Action, AgentFinishAction | ||
from openhands.llm.llm import LLM | ||
|
||
|
||
class PlannerAgent(Agent): | ||
VERSION = '1.0' | ||
""" | ||
The planner agent utilizes a special prompting strategy to create long term plans for solving problems. | ||
The agent is given its previous action-observation pairs, current task, and hint based on last action taken at every step. | ||
""" | ||
response_parser = PlannerResponseParser() | ||
|
||
def __init__(self, llm: LLM, config: AgentConfig): | ||
"""Initialize the Planner Agent with an LLM | ||
Parameters: | ||
- llm (LLM): The llm to be used by this agent | ||
""" | ||
super().__init__(llm, config) | ||
|
||
def step(self, state: State) -> Action: | ||
"""Checks to see if current step is completed, returns AgentFinishAction if True. | ||
Otherwise, creates a plan prompt and sends to model for inference, returning the result as the next action. | ||
Parameters: | ||
- state (State): The current state given the previous actions and observations | ||
Returns: | ||
- AgentFinishAction: If the last state was 'completed', 'verified', or 'abandoned' | ||
- Action: The next action to take based on llm response | ||
""" | ||
if state.root_task.state in [ | ||
'completed', | ||
'verified', | ||
'abandoned', | ||
]: | ||
return AgentFinishAction() | ||
|
||
prompt, image_urls = get_prompt_and_images( | ||
state, self.llm.config.max_message_chars | ||
) | ||
content = [TextContent(text=prompt)] | ||
if self.llm.vision_is_active() and image_urls: | ||
content.append(ImageContent(image_urls=image_urls)) | ||
message = Message(role='user', content=content) | ||
resp = self.llm.completion(messages=self.llm.format_messages_for_llm(message)) | ||
return self.response_parser.parse(resp) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from openhands.controller.action_parser import ResponseParser | ||
from openhands.core.utils import json | ||
from openhands.events.action import ( | ||
Action, | ||
) | ||
from openhands.events.serialization.action import action_from_dict | ||
|
||
|
||
class PlannerResponseParser(ResponseParser): | ||
def __init__(self): | ||
super().__init__() | ||
|
||
def parse(self, response: str) -> Action: | ||
action_str = self.parse_response(response) | ||
return self.parse_action(action_str) | ||
|
||
def parse_response(self, response) -> str: | ||
# get the next action from the response | ||
return response['choices'][0]['message']['content'] | ||
|
||
def parse_action(self, action_str: str) -> Action: | ||
"""Parses a string to find an action within it | ||
Parameters: | ||
- response (str): The string to be parsed | ||
Returns: | ||
- Action: The action that was found in the response string | ||
""" | ||
# attempt to load the JSON dict from the response | ||
action_dict = json.loads(action_str) | ||
|
||
if 'content' in action_dict: | ||
# The LLM gets confused here. Might as well be robust | ||
action_dict['contents'] = action_dict.pop('content') | ||
|
||
return action_from_dict(action_dict) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,6 +49,7 @@ def read_llm_models(): | |
def read_llm_agents(): | ||
return [ | ||
'CodeActAgent', | ||
'PlannerAgent', | ||
] | ||
|
||
|
||
|
Oops, something went wrong.