File size: 1,279 Bytes
3a2c59e
7a87ce1
3a2c59e
8fe992b
7a87ce1
8fe992b
 
 
3a2c59e
7a87ce1
 
 
 
 
 
8fe992b
 
 
3a2c59e
 
 
 
 
 
 
8fe992b
3a2c59e
 
 
 
8fe992b
3a2c59e
 
 
8fe992b
3a2c59e
 
8fe992b
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import xml.etree.ElementTree as ET

import requests
from smolagents.tools import Tool


class VisitWebpageTool(Tool):
    name = "visit_webpage"
    description = "Visits a webpage at the given url and reads its content as a xml string. Use this to browse webpages."
    inputs = {
        "url": {
            "type": "string",
            "description": "The url of the webpage to visit.",
        }
    }
    output_type = "string"

    def forward(self, url: str) -> str:
        response = requests.get(url, timeout=20)
        response.raise_for_status()
        xml_content = response.text
        start_index = xml_content.find("<rss")
        if start_index == -1:
            raise ValueError("Invalid RSS feed: <rss> element not found")
        xml_content = xml_content[start_index:]

        try:
            root = ET.fromstring(xml_content)
        except ET.ParseError as e:
            raise ValueError(f"Error parsing XML: {e}") from e

        channel = root.find("channel")
        if channel is None:
            raise ValueError("Invalid RSS feed: <channel> element not found")

        titles = [item.findtext("title") for item in channel.findall("item")]
        return titles

    def __init__(self, *args, **kwargs):
        self.is_initialized = False