Lumintroll's picture
Test agent working and tools plan created
c3b6999
from smolagents import AzureOpenAIServerModel, CodeAgent, ToolCallingAgent, tool, load_tool, DuckDuckGoSearchTool, WikipediaSearchTool, VisitWebpageTool
import pandas as pd
import os
from requests.exceptions import HTTPError
from dotenv import load_dotenv
from tempfile import TemporaryFile
import requests
from io import BytesIO
from typing import IO, Union
load_dotenv()
model = AzureOpenAIServerModel(
model_id = os.environ.get("AZURE_OPENAI_MODEL"),
azure_endpoint=os.environ.get("AZURE_OPENAI_ENDPOINT"),
api_key=os.environ.get("AZURE_OPENAI_API_KEY"),
api_version=os.environ.get("OPENAI_API_VERSION"),
max_tokens=4096
)
@tool
def get_remote_file(url: str) -> IO:
"""This tool downloads a file using the requests package, which is often successful in downloading a file when other methods meet a HTTP Error 403: Forbidden.
It returns IO which can be used as if it were a file in other fuctions which expect file data. The URL must be for the file itself, not a page.
Args:
url (str): Web address of file to download.
Returns:
IO: Bytes data
"""
# Send a GET request to the URL
response = requests.get(url)
# Check if the request was successful
if response.status_code == 200:
# Use BytesIO to read the content of the response as a binary stream
return BytesIO(response.content)
else:
print(f"Failed to retrieve the file. Status code: {response.status_code}")
@tool
def excel_reader(file_data: IO) -> pd.DataFrame:
"""
This tool returns a pandas dataframe from a file locally, from a URL or from bytes data.
Args:
file_data: A file location as a string (either a local file or url of xlsx file) or IO file data of an xlsx to read in as a dataframe.
If a file is forbidden to be accessed directly, another approach is to download the file data with get_remote_file as bytes and use that instead of a URL.
"""
return pd.read_excel(file_data, engine="openpyxl")
@tool
def string_reverser(text: str) -> str:
"""Reverses a string. This can be useful to try if initially a prompt or string seems uninelligable.
Args:
text (str): String that cannot be understood.
Returns:
str: Reversed string
"""
return text[::-1]