Spaces:
Runtime error
Runtime error
esemsc-jcm24
commited on
Commit
·
38b4c24
1
Parent(s):
9132b7e
add tyty files
Browse files- tools/mood_to_need.py +17 -3
- tools/need_to_destination.py +35 -60
- tools/test_mood_to_destination.py +21 -0
tools/mood_to_need.py
CHANGED
@@ -1,6 +1,6 @@
|
|
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 |
"""
|
@@ -22,6 +22,7 @@ class MoodToNeedTool(Tool):
|
|
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:
|
@@ -44,4 +45,17 @@ class MoodToNeedTool(Tool):
|
|
44 |
f'Need:'
|
45 |
)
|
46 |
response = self.model(prompt)
|
47 |
-
return response.strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
from smolagents.tools import Tool
|
2 |
+
from anthropic import Anthropic, HUMAN_PROMPT, AI_PROMPT
|
3 |
+
import os
|
4 |
|
5 |
class MoodToNeedTool(Tool):
|
6 |
"""
|
|
|
22 |
Args:
|
23 |
model: A callable language model with a __call__(str) -> str interface.
|
24 |
"""
|
25 |
+
super().__init__()
|
26 |
self.model = model
|
27 |
|
28 |
def forward(self, mood: str) -> str:
|
|
|
45 |
f'Need:'
|
46 |
)
|
47 |
response = self.model(prompt)
|
48 |
+
return response.strip()
|
49 |
+
|
50 |
+
client = Anthropic(api_key=os.environ["ANTHROPIC_KEY"])
|
51 |
+
|
52 |
+
def claude_mood_to_need_model(prompt: str) -> str:
|
53 |
+
message = client.messages.create(
|
54 |
+
model="claude-3-opus-20240229",
|
55 |
+
max_tokens=1024,
|
56 |
+
temperature=0.7,
|
57 |
+
messages=[
|
58 |
+
{"role": "user", "content": prompt}
|
59 |
+
]
|
60 |
+
)
|
61 |
+
return message.content[0].text
|
tools/need_to_destination.py
CHANGED
@@ -1,4 +1,6 @@
|
|
1 |
from smolagents.tools import Tool
|
|
|
|
|
2 |
import json
|
3 |
|
4 |
class NeedToDestinationTool(Tool):
|
@@ -16,32 +18,32 @@ class NeedToDestinationTool(Tool):
|
|
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 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
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())
|
@@ -50,42 +52,15 @@ DO NOT add explanations, only return raw JSON.
|
|
50 |
|
51 |
return destinations
|
52 |
|
53 |
-
|
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 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
|
|
|
|
|
|
|
1 |
from smolagents.tools import Tool
|
2 |
+
from anthropic import Anthropic
|
3 |
+
import os
|
4 |
import json
|
5 |
|
6 |
class NeedToDestinationTool(Tool):
|
|
|
18 |
|
19 |
def forward(self, need: str) -> list[dict]:
|
20 |
prompt = f"""
|
21 |
+
You are a travel agent AI.
|
22 |
|
23 |
+
Based on the user's need: "{need}",
|
24 |
+
suggest 2-3 travel destinations with round-trip flight information.
|
25 |
|
26 |
+
Return the output as valid JSON in the following format:
|
27 |
|
28 |
+
[
|
29 |
+
{{
|
30 |
+
"destination": "DestinationName",
|
31 |
+
"departure": {{
|
32 |
+
"date": "YYYY-MM-DD",
|
33 |
+
"from_airport": "{self.departure_airport}",
|
34 |
+
"to_airport": "XXX"
|
35 |
+
}},
|
36 |
+
"return": {{
|
37 |
+
"date": "YYYY-MM-DD",
|
38 |
+
"from_airport": "XXX",
|
39 |
+
"to_airport": "{self.departure_airport}"
|
40 |
+
}}
|
41 |
+
}},
|
42 |
+
...
|
43 |
+
]
|
44 |
|
45 |
+
DO NOT add explanations, only return raw JSON.
|
46 |
+
"""
|
47 |
result = self.model(prompt)
|
48 |
try:
|
49 |
destinations = json.loads(result.strip())
|
|
|
52 |
|
53 |
return destinations
|
54 |
|
55 |
+
client = Anthropic(api_key=os.environ["ANTHROPIC_KEY"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
|
57 |
+
def claude_need_to_destination_model(prompt: str) -> str:
|
58 |
+
message = client.messages.create(
|
59 |
+
model="claude-3-opus-20240229",
|
60 |
+
max_tokens=1024,
|
61 |
+
temperature=0.7,
|
62 |
+
messages=[
|
63 |
+
{"role": "user", "content": prompt}
|
64 |
+
]
|
65 |
+
)
|
66 |
+
return message.content[0].text
|
tools/test_mood_to_destination.py
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from tools.mood_to_need import MoodToNeedTool, claude_mood_to_need_model
|
2 |
+
from tools.need_to_destination import NeedToDestinationTool, claude_need_to_destination_model
|
3 |
+
import json
|
4 |
+
|
5 |
+
mood_tool = MoodToNeedTool(model=claude_mood_to_need_model)
|
6 |
+
mood = "I'm feeling stuck in a routine and need change"
|
7 |
+
# mood = "I feel overwhelmed and burned out."
|
8 |
+
# mood = "I just got out of a break up"
|
9 |
+
|
10 |
+
need = mood_tool(mood=mood)
|
11 |
+
print(f"Mood: {mood}")
|
12 |
+
print("Need:", need)
|
13 |
+
|
14 |
+
destination_tool = NeedToDestinationTool(model=claude_need_to_destination_model, departure_airport="CDG")
|
15 |
+
try:
|
16 |
+
destinations = destination_tool(need=need)
|
17 |
+
print("\n→ Suggested Destinations:")
|
18 |
+
for dest in destinations:
|
19 |
+
print(json.dumps(dest, indent=2))
|
20 |
+
except ValueError as e:
|
21 |
+
print(f"Error parsing Claude output: {e}")
|