跳到主要内容

openhands.agenthub.browsing_agent.prompt

Flags Objects

@dataclass
class Flags()

drop_ax_tree_first

This flag is no longer active TODO delete

asdict

def asdict()

Helper for JSON serializble requirement.

from_dict

@classmethod
def from_dict(flags_dict)

Helper for JSON serializable requirement.

PromptElement Objects

class PromptElement()

Base class for all prompt elements. Prompt elements can be hidden.

Prompt elements are used to build the prompt. Use flags to control which prompt elements are visible. We use class attributes as a convenient way to implement static prompts, but feel free to override them with instance attributes or @property decorator.

__init__

def __init__(visible: bool = True) -> None

Prompt element that can be hidden.

Parameters

visible : bool, optional Whether the prompt element should be visible, by default True. Can be a callable that returns a bool. This is useful when a specific flag changes during a shrink iteration.

prompt

@property
def prompt()

Avoid overriding this method. Override _prompt instead.

abstract_ex

@property
def abstract_ex()

Useful when this prompt element is requesting an answer from the llm. Provide an abstract example of the answer here. See Memory for an example.

Avoid overriding this method. Override _abstract_ex instead

concrete_ex

@property
def concrete_ex()

Useful when this prompt element is requesting an answer from the llm. Provide a concrete example of the answer here. See Memory for an example.

Avoid overriding this method. Override _concrete_ex instead

is_visible

@property
def is_visible()

Handle the case where visible is a callable.

Shrinkable Objects

class Shrinkable(PromptElement, abc.ABC)

shrink

@abc.abstractmethod
def shrink() -> None

Implement shrinking of this prompt element.

You need to recursively call all shrinkable elements that are part of this prompt. You can also implement a shrinking strategy for this prompt. Shrinking is can be called multiple times to progressively shrink the prompt until it fits max_tokens. Default max shrink iterations is 20.

Truncater Objects

class Truncater(Shrinkable)

A prompt element that can be truncated to fit the context length of the LLM. Of course, it will be great that we never have to use the functionality here to shrink() the prompt. Extend this class for prompt elements that can be truncated. Usually long observations such as AxTree or HTML.

fit_tokens

def fit_tokens(shrinkable: Shrinkable,
max_prompt_chars=None,
max_iterations=20)

Shrink a prompt element until it fits max_tokens.

Parameters

shrinkable : Shrinkable The prompt element to shrink. max_prompt_chars : int The maximum number of chars allowed. max_iterations : int, optional The maximum number of shrink iterations, by default 20. model_name : str, optional The name of the model used when tokenizing.

Returns:


str : the prompt after shrinking.

Observation Objects

class Observation(Shrinkable)

Observation of the current step.

Contains the html, the accessibility tree and the error logs.

diff

def diff(previous, new)

Return a string showing the difference between original and new.

If the difference is above diff_threshold, return the diff string.

History Objects

class History(Shrinkable)

shrink

def shrink()

Shrink individual steps