Spaces:
Running
Running
Update sql_data.py
Browse files- sql_data.py +76 -0
sql_data.py
CHANGED
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from sqlalchemy import create_engine, text
|
2 |
+
from smolagents import tool
|
3 |
+
import pandas as pd
|
4 |
+
|
5 |
+
# Create database engine
|
6 |
+
engine = create_engine("sqlite:///freights.db")
|
7 |
+
|
8 |
+
@tool
|
9 |
+
def sql_query(query: str) -> str:
|
10 |
+
"""
|
11 |
+
Allows you to perform SQL queries on the freights table. Returns a string representation of the result.
|
12 |
+
The table is named 'freights'. Its description is as follows:
|
13 |
+
Columns:
|
14 |
+
- departure: DateTime (Date and time of departure)
|
15 |
+
- origin_port_locode: String (Origin port code)
|
16 |
+
- origin_port_name: String (Name of the origin port)
|
17 |
+
- destination_port: String (Destination port code)
|
18 |
+
- destination_port_name: String (Name of the destination port)
|
19 |
+
- dv20rate: Float (Rate for 20ft container in USD)
|
20 |
+
- dv40rate: Float (Rate for 40ft container in USD)
|
21 |
+
- currency: String (Currency of the rates)
|
22 |
+
- inserted_on: DateTime (Date when the rate was inserted)
|
23 |
+
Args:
|
24 |
+
query: The query to perform. This should be correct SQL.
|
25 |
+
Returns:
|
26 |
+
A string representation of the result of the query.
|
27 |
+
"""
|
28 |
+
try:
|
29 |
+
with engine.connect() as con:
|
30 |
+
result = con.execute(text(query))
|
31 |
+
rows = [dict(row._mapping) for row in result]
|
32 |
+
|
33 |
+
if not rows:
|
34 |
+
return "Aucun résultat trouvé."
|
35 |
+
|
36 |
+
# Convert to markdown table
|
37 |
+
headers = list(rows[0].keys())
|
38 |
+
table = "| " + " | ".join(headers) + " |\n"
|
39 |
+
table += "| " + " | ".join(["---" for _ in headers]) + " |\n"
|
40 |
+
|
41 |
+
for row in rows:
|
42 |
+
table += "| " + " | ".join(str(row[h]) for h in headers) + " |\n"
|
43 |
+
|
44 |
+
return table
|
45 |
+
|
46 |
+
except Exception as e:
|
47 |
+
return f"Error executing query: {str(e)}"
|
48 |
+
|
49 |
+
|
50 |
+
@tool
|
51 |
+
def get_schema() -> str:
|
52 |
+
"""
|
53 |
+
Returns the schema of the freights table.
|
54 |
+
"""
|
55 |
+
return """
|
56 |
+
Table: freights
|
57 |
+
Columns:
|
58 |
+
- departure: DateTime (Date and time of departure)
|
59 |
+
- origin_port_locode: String (Origin port code)
|
60 |
+
- origin_port_name: String (Name of the origin port)
|
61 |
+
- destination_port: String (Destination port code)
|
62 |
+
- destination_port_name: String (Name of the destination port)
|
63 |
+
- dv20rate: Float (Rate for 20ft container in USD)
|
64 |
+
- dv40rate: Float (Rate for 40ft container in USD)
|
65 |
+
- currency: String (Currency of the rates)
|
66 |
+
- inserted_on: DateTime (Date when the rate was inserted)
|
67 |
+
"""
|
68 |
+
|
69 |
+
|
70 |
+
@tool
|
71 |
+
def get_csv_as_dataframe() -> str:
|
72 |
+
"""
|
73 |
+
Returns a string representation of the freights table as a CSV file.
|
74 |
+
"""
|
75 |
+
df = pd.read_sql_table("freights", engine)
|
76 |
+
return df.to_csv(index=False)
|