File size: 2,310 Bytes
c3b6999
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
53
54
55
56
57
58
59
60
61
62
63
64
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]