Final_Assignment / tools /attachments.py
tdziwok's picture
first commit
31af2b2
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}```"""