File size: 2,641 Bytes
c577353
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
from sqlalchemy import create_engine, text
from smolagents import tool
import os
import pandas as pd

# Create database engine
engine = create_engine("sqlite:///freights.db")


@tool
def sql_query(query: str) -> str:
    """
    Allows you to perform SQL queries on the freights table. Returns a string representation of the result.
    The table is named 'freights'. Its description is as follows:
        Columns:
        - departure: DateTime (Date and time of departure)
        - origin_port_locode: String (Origin port code)
        - origin_port_name: String (Name of the origin port)
        - destination_port: String (Destination port code)
        - destination_port_name: String (Name of the destination port)
        - dv20rate: Float (Rate for 20ft container in USD)
        - dv40rate: Float (Rate for 40ft container in USD)
        - currency: String (Currency of the rates)
        - inserted_on: DateTime (Date when the rate was inserted)

    Args:
        query: The query to perform. This should be correct SQL.

    Returns:
        A string representation of the result of the query.
    """
    try:
        with engine.connect() as con:
            result = con.execute(text(query))
            rows = [dict(row._mapping) for row in result]

            if not rows:
                return "Aucun résultat trouvé."

            # Convert to markdown table
            headers = list(rows[0].keys())
            table = "| " + " | ".join(headers) + " |\n"
            table += "| " + " | ".join(["---" for _ in headers]) + " |\n"

            for row in rows:
                table += "| " + " | ".join(str(row[h]) for h in headers) + " |\n"

            return table

    except Exception as e:
        return f"Error executing query: {str(e)}"


@tool
def get_schema() -> str:
    """
    Returns the schema of the freights table.
    """
    return """
    Table: freights
    Columns:
    - departure: DateTime (Date and time of departure)
    - origin_port_locode: String (Origin port code)
    - origin_port_name: String (Name of the origin port)
    - destination_port: String (Destination port code)
    - destination_port_name: String (Name of the destination port)
    - dv20rate: Float (Rate for 20ft container in USD)
    - dv40rate: Float (Rate for 40ft container in USD)
    - currency: String (Currency of the rates)
    - inserted_on: DateTime (Date when the rate was inserted)
    """


@tool
def get_csv_as_dataframe() -> str:
    """
    Returns a string representation of the freights table as a CSV file.
    """
    df = pd.read_sql_table("freights", engine)
    return df.to_csv(index=False)