web_visitor

Module Contents

Classes

WebVisitor

Base class for all actions.

Functions

extract_last_json(text)

Extracts the last valid JSON object from a string.

lagent.actions.web_visitor.extract_last_json(text)

Extracts the last valid JSON object from a string. Handles Markdown code blocks (`json ... `) and raw JSON strings.

参数:

text (str) –

返回类型:

dict | None

class lagent.actions.web_visitor.WebVisitor(browse_tool, llm, max_browse_attempts=3, max_extract_attempts=3, sleep_interval=3, truncate_browse_response_length=None, tokenizer_path=None, name='visit')

Bases: lagent.actions.AsyncActionMixin, lagent.actions.BaseAction

Base class for all actions.

参数:
  • description (Optional[dict]) – The description of the action. Defaults to None.

  • parser (Type[BaseParser]) – The parser class to process the action’s inputs and outputs. Defaults to JsonParser.

  • browse_tool (lagent.actions.BaseAction | dict) –

  • llm (Any) –

  • max_browse_attempts (int) –

  • max_extract_attempts (int) –

  • sleep_interval (int) –

  • truncate_browse_response_length (int | None) –

  • tokenizer_path (str | None) –

  • name (str) –

示例

  • simple tool

class Bold(BaseAction):
    '''Make text bold'''

    def run(self, text: str):
        '''
        Args:
            text (str): input text

        Returns:
            str: bold text
        '''
        return '**' + text + '**'

action = Bold()
  • toolkit with multiple APIs

class Calculator(BaseAction):
    '''Calculator'''

    @tool_api
    def add(self, a, b):
        '''Add operation

        Args:
            a (int): augend
            b (int): addend

        Returns:
            int: sum
        '''
        return a + b

    @tool_api
    def sub(self, a, b):
        '''Subtraction operation

        Args:
            a (int): minuend
            b (int): subtrahend

        Returns:
            int: difference
        '''
        return a - b

action = Calculator()
EXTRACTION_PROMPT = Multiline-String
Show Value
"""Please process the following webpage content and user goal to extract relevant information:

## **Webpage Content**
{webpage_content}

## **User Goal**
{goal}

## **Task Guidelines**
1. **Content Scanning for Rationale**: Locate the **specific sections/data** directly related to the user's goal within the webpage content
2. **Key Extraction for Evidence**: Identify and extract the **most relevant information** from the content, you never miss any important information, output the **full original context** of the content as far as possible, it can be more than three paragraphs.
3. **Summary Output for Summary**: Organize into a concise paragraph with logical flow, prioritizing clarity and judge the contribution of the information to the goal.

**Final Output Format using JSON format has "rational", "evidence", "summary" fields**
"""
browse_tool
llm
max_browse_attempts = 3
max_extract_attempts = 3
sleep_interval = 3
truncate_browse_response_length = None
tokenizer = None
async run(url, goal)
参数:
  • url (str | List[str]) –

  • goal (str) –

返回类型:

str