Spaces:
Runtime error
Runtime error
cgirard-sez
commited on
Commit
·
cbddf51
1
Parent(s):
904af29
add tools for modetoneed and needtodestination
Browse files- .env +2 -1
- tools/mood_to_need.py +47 -0
- tools/need_to_destination.py +91 -0
.env
CHANGED
@@ -1,2 +1,3 @@
|
|
1 |
SERPAPI_API_KEY="5f1ba2f2179fafad997e2514ccaa634be88cc9d7c5d0382e9e5b528ba0e434c0O"
|
2 |
-
OPENWEATHER_API_KEY="e6945bba120fdad7ef7421c5cdbe731c"
|
|
|
|
1 |
SERPAPI_API_KEY="5f1ba2f2179fafad997e2514ccaa634be88cc9d7c5d0382e9e5b528ba0e434c0O"
|
2 |
+
OPENWEATHER_API_KEY="e6945bba120fdad7ef7421c5cdbe731c"
|
3 |
+
ANTHROPIC_KEY=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
|
tools/mood_to_need.py
ADDED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Tool to map a user's mood to a vacation need."""
|
2 |
+
|
3 |
+
from smolagents.tools import Tool
|
4 |
+
|
5 |
+
class MoodToNeedTool(Tool):
|
6 |
+
"""
|
7 |
+
A tool that converts user mood descriptions into vacation needs using an LLM.
|
8 |
+
|
9 |
+
Attributes:
|
10 |
+
model: A callable language model used to generate the output.
|
11 |
+
"""
|
12 |
+
name = "MoodToNeed"
|
13 |
+
inputs = {
|
14 |
+
"mood": {"type": "string", "description": "User's mood as text"},
|
15 |
+
}
|
16 |
+
output_type = "string"
|
17 |
+
|
18 |
+
description = "Converts user mood into a travel-related need."
|
19 |
+
|
20 |
+
def __init__(self, model: callable) -> None:
|
21 |
+
"""
|
22 |
+
Args:
|
23 |
+
model: A callable language model with a __call__(str) -> str interface.
|
24 |
+
"""
|
25 |
+
self.model = model
|
26 |
+
|
27 |
+
def forward(self, mood: str) -> str:
|
28 |
+
"""
|
29 |
+
Generates a vacation need from a user mood string.
|
30 |
+
|
31 |
+
Args:
|
32 |
+
mood: A string describing the user's emotional state.
|
33 |
+
|
34 |
+
Returns:
|
35 |
+
A short string describing the travel-related need.
|
36 |
+
"""
|
37 |
+
prompt = (
|
38 |
+
f"Given the user's mood, suggest a travel need.\n"
|
39 |
+
f'Mood: "{mood}"\n'
|
40 |
+
f'Return only the need, no explanation.\n'
|
41 |
+
f'Example:\n'
|
42 |
+
f'Mood: "I am exhausted" → Need: "A calm wellness retreat"\n'
|
43 |
+
f'Mood: "{mood}"\n'
|
44 |
+
f'Need:'
|
45 |
+
)
|
46 |
+
response = self.model(prompt)
|
47 |
+
return response.strip()
|
tools/need_to_destination.py
ADDED
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from smolagents.tools import Tool
|
2 |
+
import json
|
3 |
+
|
4 |
+
class NeedToDestinationTool(Tool):
|
5 |
+
name = "NeedToDestination"
|
6 |
+
inputs = {
|
7 |
+
"need": {"type": "string", "description": "User's travel need as text"},
|
8 |
+
}
|
9 |
+
output_type = "array"
|
10 |
+
description = "Suggests destinations and flight info based on user need."
|
11 |
+
|
12 |
+
def __init__(self, model: callable, departure_airport: str = "CDG") -> None:
|
13 |
+
super().__init__()
|
14 |
+
self.model = model
|
15 |
+
self.departure_airport = departure_airport
|
16 |
+
|
17 |
+
def forward(self, need: str) -> list[dict]:
|
18 |
+
prompt = f"""
|
19 |
+
You are a travel agent AI.
|
20 |
+
|
21 |
+
Based on the user's need: "{need}",
|
22 |
+
suggest 2-3 travel destinations with round-trip flight information.
|
23 |
+
|
24 |
+
Return the output as valid JSON in the following format:
|
25 |
+
|
26 |
+
[
|
27 |
+
{{
|
28 |
+
"destination": "DestinationName",
|
29 |
+
"departure": {{
|
30 |
+
"date": "YYYY-MM-DD",
|
31 |
+
"from_airport": "{self.departure_airport}",
|
32 |
+
"to_airport": "XXX"
|
33 |
+
}},
|
34 |
+
"return": {{
|
35 |
+
"date": "YYYY-MM-DD",
|
36 |
+
"from_airport": "XXX",
|
37 |
+
"to_airport": "{self.departure_airport}"
|
38 |
+
}}
|
39 |
+
}},
|
40 |
+
...
|
41 |
+
]
|
42 |
+
|
43 |
+
DO NOT add explanations, only return raw JSON.
|
44 |
+
"""
|
45 |
+
result = self.model(prompt)
|
46 |
+
try:
|
47 |
+
destinations = json.loads(result.strip())
|
48 |
+
except json.JSONDecodeError:
|
49 |
+
raise ValueError("Could not parse LLM output to JSON.")
|
50 |
+
|
51 |
+
return destinations
|
52 |
+
|
53 |
+
def mock_model(prompt: str) -> str:
|
54 |
+
return """
|
55 |
+
[
|
56 |
+
{
|
57 |
+
"destination": "Nice",
|
58 |
+
"departure": {
|
59 |
+
"date": "2025-07-01",
|
60 |
+
"from_airport": "CDG",
|
61 |
+
"to_airport": "NCE"
|
62 |
+
},
|
63 |
+
"return": {
|
64 |
+
"date": "2025-07-07",
|
65 |
+
"from_airport": "NCE",
|
66 |
+
"to_airport": "CDG"
|
67 |
+
}
|
68 |
+
},
|
69 |
+
{
|
70 |
+
"destination": "Barcelona",
|
71 |
+
"departure": {
|
72 |
+
"date": "2025-07-02",
|
73 |
+
"from_airport": "CDG",
|
74 |
+
"to_airport": "BCN"
|
75 |
+
},
|
76 |
+
"return": {
|
77 |
+
"date": "2025-07-08",
|
78 |
+
"from_airport": "BCN",
|
79 |
+
"to_airport": "CDG"
|
80 |
+
}
|
81 |
+
}
|
82 |
+
]
|
83 |
+
"""
|
84 |
+
|
85 |
+
if __name__ == "__main__":
|
86 |
+
tool = NeedToDestinationTool(model=mock_model)
|
87 |
+
test_need = "A relaxing beach holiday"
|
88 |
+
destinations = tool(need=test_need)
|
89 |
+
print("Destinations returned by the tool:")
|
90 |
+
for dest in destinations:
|
91 |
+
print(dest)
|