Skip to content

WebSurferChat

fastagency.studio.models.agents.web_surfer_autogen.WebSurferChat #

WebSurferChat(
    name_prefix: str,
    llm_config: dict[str, Any],
    summarizer_llm_config: dict[str, Any],
    viewport_size: int,
    bing_api_key: Optional[str],
    max_consecutive_auto_reply: int = 30,
    max_links_to_click: int = 10,
    websurfer_kwargs: dict[str, Any] = {},
    assistant_kwargs: dict[str, Any] = {},
)

Create a new WebSurferChat instance.

PARAMETER DESCRIPTION
name_prefix

The name prefix of the inner AutoGen agents

TYPE: str

llm_config

The LLM configuration

TYPE: Dict[str, Any]

summarizer_llm_config

The summarizer LLM configuration

TYPE: Dict[str, Any]

viewport_size

The viewport size of the browser

TYPE: int

bing_api_key

The Bing API key for the browser

TYPE: Optional[str]

max_consecutive_auto_reply

The maximum consecutive auto reply. Defaults to 30.

TYPE: int DEFAULT: 30

max_links_to_click

The maximum links to click. Defaults to 10.

TYPE: int DEFAULT: 10

websurfer_kwargs

The keyword arguments for the websurfer. Defaults to {}.

TYPE: Dict[str, Any] DEFAULT: {}

assistant_kwargs

The keyword arguments for the assistant. Defaults to {}.

TYPE: Dict[str, Any] DEFAULT: {}

Source code in fastagency/studio/models/agents/web_surfer_autogen.py
def __init__(
    self,
    name_prefix: str,
    llm_config: dict[str, Any],
    summarizer_llm_config: dict[str, Any],
    viewport_size: int,
    bing_api_key: Optional[str],
    max_consecutive_auto_reply: int = 30,
    max_links_to_click: int = 10,
    websurfer_kwargs: dict[str, Any] = {},  # noqa: B006
    assistant_kwargs: dict[str, Any] = {},  # noqa: B006
):
    """Create a new WebSurferChat instance.

    Args:
        name_prefix (str): The name prefix of the inner AutoGen agents
        llm_config (Dict[str, Any]): The LLM configuration
        summarizer_llm_config (Dict[str, Any]): The summarizer LLM configuration
        viewport_size (int): The viewport size of the browser
        bing_api_key (Optional[str]): The Bing API key for the browser
        max_consecutive_auto_reply (int, optional): The maximum consecutive auto reply. Defaults to 30.
        max_links_to_click (int, optional): The maximum links to click. Defaults to 10.
        websurfer_kwargs (Dict[str, Any], optional): The keyword arguments for the websurfer. Defaults to {}.
        assistant_kwargs (Dict[str, Any], optional): The keyword arguments for the assistant. Defaults to {}.

    """
    self.name_prefix = name_prefix
    self.llm_config = llm_config
    self.summarizer_llm_config = summarizer_llm_config
    self.viewport_size = viewport_size
    self.bing_api_key = (
        bing_api_key if bing_api_key is not None else os.getenv("BING_API_KEY")
    )
    self.max_consecutive_auto_reply = max_consecutive_auto_reply
    self.max_links_to_click = max_links_to_click
    self.websurfer_kwargs = websurfer_kwargs
    self.assistant_kwargs = assistant_kwargs

    self.task = "not set yet"
    self.last_is_termination_msg_error = ""

    self.browser_config = {
        "viewport_size": self.viewport_size,
        "bing_api_key": self.bing_api_key,
        "request_kwargs": {
            "headers": {
                "User-Agent": "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36",
            }
        },
    }

    if "human_input_mode" in self.websurfer_kwargs:
        self.websurfer_kwargs.pop("human_input_mode")

    self.websurfer = AutoGenWebSurferAgent(
        name=f"{self.name_prefix}_inner_websurfer",
        llm_config=self.llm_config,
        summarizer_llm_config=self.summarizer_llm_config,
        browser_config=self.browser_config,
        human_input_mode="NEVER",
        is_termination_msg=self.is_termination_msg,
        **self.websurfer_kwargs,
    )

    if "human_input_mode" in self.assistant_kwargs:
        self.assistant_kwargs.pop("human_input_mode")

    self.assistant = AutoGenAssistantAgent(
        name=f"{self.name_prefix}_inner_assistant",
        llm_config=self.llm_config,
        human_input_mode="NEVER",
        system_message=self.system_message,
        max_consecutive_auto_reply=self.max_consecutive_auto_reply,
        # is_termination_msg=self.is_termination_msg,
        **self.assistant_kwargs,
    )

assistant instance-attribute #

assistant = AssistantAgent(
    name=f"{name_prefix}_inner_assistant",
    llm_config=llm_config,
    human_input_mode="NEVER",
    system_message=system_message,
    max_consecutive_auto_reply=max_consecutive_auto_reply,
    **assistant_kwargs,
)

assistant_kwargs instance-attribute #

assistant_kwargs = assistant_kwargs

bing_api_key instance-attribute #

bing_api_key = (
    bing_api_key
    if bing_api_key is not None
    else getenv("BING_API_KEY")
)

browser_config instance-attribute #

browser_config = {
    "viewport_size": viewport_size,
    "bing_api_key": bing_api_key,
    "request_kwargs": {
        "headers": {
            "User-Agent": "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36"
        }
    },
}

error_message property #

error_message: str

example_answer property #

example_answer: WebSurferAnswer

initial_message property #

initial_message: str

last_is_termination_msg_error instance-attribute #

last_is_termination_msg_error = ''

llm_config instance-attribute #

llm_config = llm_config

max_consecutive_auto_reply instance-attribute #

max_consecutive_auto_reply = max_consecutive_auto_reply
max_links_to_click = max_links_to_click

name_prefix instance-attribute #

name_prefix = name_prefix

summarizer_llm_config instance-attribute #

summarizer_llm_config = summarizer_llm_config

system_message property #

system_message: str

task instance-attribute #

task = 'not set yet'

viewport_size instance-attribute #

viewport_size = viewport_size

websurfer instance-attribute #

websurfer = WebSurferAgent(
    name=f"{name_prefix}_inner_websurfer",
    llm_config=llm_config,
    summarizer_llm_config=summarizer_llm_config,
    browser_config=browser_config,
    human_input_mode="NEVER",
    is_termination_msg=is_termination_msg,
    **websurfer_kwargs,
)

websurfer_kwargs instance-attribute #

websurfer_kwargs = websurfer_kwargs

continue_task_with_additional_instructions async #

continue_task_with_additional_instructions(
    message: str,
) -> str
Source code in fastagency/studio/models/agents/web_surfer_autogen.py
async def continue_task_with_additional_instructions(self, message: str) -> str:
    try:
        answer = await asyncify(self._chat_with_websurfer)(
            message=message,
            clear_history=False,
        )
    except Exception as e:
        return self._get_error_from_exception(message, e)

    return self.create_final_reply(message, answer)

create_final_reply #

create_final_reply(
    task: str, message: WebSurferAnswer
) -> str
Source code in fastagency/studio/models/agents/web_surfer_autogen.py
def create_final_reply(self, task: str, message: WebSurferAnswer) -> str:
    retval = (
        "We have successfully completed the task:\n\n"
        if message.is_successful
        else "We have failed to complete the task:\n\n"
    )
    retval += f"{task}\n\n"
    retval += f"Short answer: {message.short_answer}\n\n"
    retval += f"Explanation: {message.long_answer}\n\n"
    retval += "Visited links:\n"
    for link in message.visited_links:
        retval += f"  - {link}\n"

    return retval

create_new_task async #

create_new_task(task: str) -> str
Source code in fastagency/studio/models/agents/web_surfer_autogen.py
async def create_new_task(self, task: str) -> str:
    self.task = task
    try:
        answer = await asyncify(self._chat_with_websurfer)(
            message=self.initial_message,
            clear_history=True,
        )
    except Exception as e:
        return self._get_error_from_exception(task, e)

    return self.create_final_reply(task, answer)

is_termination_msg #

is_termination_msg(msg: dict[str, Any]) -> bool
Source code in fastagency/studio/models/agents/web_surfer_autogen.py
def is_termination_msg(self, msg: dict[str, Any]) -> bool:
    # print(f"is_termination_msg({msg=})")
    if (
        "content" in msg
        and msg["content"] is not None
        and "TERMINATE" in msg["content"]
    ):
        return True
    try:
        WebSurferAnswer.model_validate_json(msg["content"])
        return True
    except Exception as e:
        self.last_is_termination_msg_error = str(e)
        return False