File size: 1,641 Bytes
31af2b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
42
43
44
45
46
47
48
49
50
51
52
import requests
from smolagents import tool

@tool
def get_attachment_type(attachment_url: str) -> str:
    """
    Inspect the contents of the attachment url, to determine what the filetype is.

    Args:
        attachment_url (str): The url of an attachment

    Returns:
        str: the mimetype of the file
    """

    # some security:
    if "https://agents-course-unit4-scoring.hf.space" not in attachment_url:
        return "the requested URL is not whitelisted, refusing to fetch data"

    resp = requests.head(attachment_url)
    if resp.status_code != 200:
        return f"failed to fetch the requested attachment: (status={resp.status_code})\n{resp.text}"
    return resp.headers["content-type"]


@tool
def get_text_attachment(attachment_url: str) -> str:
    """
    get the contents of the attachment. works best for plain-text files (e.g. txt, md, etc.)

    Args:
        attachment_url (str): The URL of the attachment file

    Returns:
        str: the contents of the file
    """

    # some security:
    if "https://agents-course-unit4-scoring.hf.space" not in attachment_url:
        return "the requested URL is not whitelisted, refusing to fetch data"

    resp = requests.get(attachment_url)
    if resp.status_code != 200:
        return f"failed to fetch the requested image: (status={resp.status_code})\n{resp.text}"
    mime = resp.headers.get("content-type")

    if not mime.startswith("text/"):
        return f"the requested file is not plain/text - the `{mime}` format is not supported by this tool."

    # TODO add truncation for large files

    return f"""File contents: \n```\n{resp.text}```"""