import json
from together import Together
client = Together()
# Define all available tools for the travel assistant
tools = [
{
"type": "function",
"function": {
"name": "get_current_weather",
"description": "Get the current weather in a given location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA"
},
"unit": {
"type": "string",
"description": "The unit of temperature",
"enum": ["celsius", "fahrenheit"]
}
},
"required": ["location"]
}
}
},
{
"type": "function",
"function": {
"name": "get_restaurant_recommendations",
"description": "Get restaurant recommendations for a specific location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA"
},
"cuisine_type": {
"type": "string",
"description": "Type of cuisine preferred",
"enum": ["italian", "chinese", "mexican", "american", "french", "japanese", "any"]
},
"price_range": {
"type": "string",
"description": "Price range preference",
"enum": ["budget", "mid-range", "upscale", "any"]
}
},
"required": ["location"]
}
}
}
]
def get_current_weather(location, unit="fahrenheit"):
"""Get the weather for some location"""
if "chicago" in location.lower():
return json.dumps({"location": "Chicago", "temperature": "13", "unit": unit, "condition": "cold and snowy"})
elif "san francisco" in location.lower():
return json.dumps({"location": "San Francisco", "temperature": "65", "unit": unit, "condition": "mild and partly cloudy"})
elif "new york" in location.lower():
return json.dumps({"location": "New York", "temperature": "28", "unit": unit, "condition": "cold and windy"})
else:
return json.dumps({"location": location, "temperature": "unknown", "condition": "unknown"})
def get_restaurant_recommendations(location, cuisine_type="any", price_range="any"):
"""Get restaurant recommendations for a location"""
restaurants = {}
if "san francisco" in location.lower():
restaurants = {
"italian": ["Tony's Little Star Pizza", "Perbacco"],
"chinese": ["R&G Lounge", "Z&Y Restaurant"],
"american": ["Zuni Café", "House of Prime Rib"],
"seafood": ["Swan Oyster Depot", "Fisherman's Wharf restaurants"]
}
elif "chicago" in location.lower():
restaurants = {
"italian": ["Gibsons Italia", "Piccolo Sogno"],
"american": ["Alinea", "Girl & Goat"],
"pizza": ["Lou Malnati's", "Giordano's"],
"steakhouse": ["Gibsons Bar & Steakhouse"]
}
elif "new york" in location.lower():
restaurants = {
"italian": ["Carbone", "Don Angie"],
"american": ["The Spotted Pig", "Gramercy Tavern"],
"pizza": ["Joe's Pizza", "Prince Street Pizza"],
"fine_dining": ["Le Bernardin", "Eleven Madison Park"]
}
return json.dumps({"location": location, "cuisine_filter": cuisine_type, "price_filter": price_range, "restaurants": restaurants})
def handle_conversation_turn(messages, user_input):
"""Handle a single conversation turn with potential function calls"""
# 3. Add user input to messages
messages.append({"role": "user", "content": user_input})
# 4. Get model response with tools
response = client.chat.completions.create(
model="Qwen/Qwen3-Coder-480B-A35B-Instruct-FP8",
messages=messages,
tools=tools,
)
tool_calls = response.choices[0].message.tool_calls
if tool_calls:
# 5. Add assistant response with tool calls
messages.append({
"role": "assistant",
"content": response.choices[0].message.content or "",
"tool_calls": [tool_call.model_dump() for tool_call in tool_calls]
})
# 6. Execute each function call
for tool_call in tool_calls:
function_name = tool_call.function.name
function_args = json.loads(tool_call.function.arguments)
print(f"🔧 Calling {function_name} with args: {function_args}")
# Route to appropriate function
if function_name == "get_current_weather":
function_response = get_current_weather(
location=function_args.get("location"),
unit=function_args.get("unit", "fahrenheit")
)
elif function_name == "get_activity_suggestions":
function_response = get_activity_suggestions(
location=function_args.get("location"),
weather_condition=function_args.get("weather_condition"),
activity_type=function_args.get("activity_type", "both")
)
elif function_name == "get_restaurant_recommendations":
function_response = get_restaurant_recommendations(
location=function_args.get("location"),
cuisine_type=function_args.get("cuisine_type", "any"),
price_range=function_args.get("price_range", "any")
)
# 7. Add function response to messages
messages.append({
"tool_call_id": tool_call.id,
"role": "tool",
"name": function_name,
"content": function_response,
})
# 8. Get final response with function results
final_response = client.chat.completions.create(
model="Qwen/Qwen3-Coder-480B-A35B-Instruct-FP8",
messages=messages,
)
# 9. Add final assistant response to messages for context retention
messages.append({
"role": "assistant",
"content": final_response.choices[0].message.content
})
return final_response.choices[0].message.content
# Initialize conversation with system message
messages = [{
"role": "system",
"content": "You are a helpful travel planning assistant. You can access weather information and restaurant recommendations. Use the available tools to provide comprehensive travel advice based on the user's needs."
}]
# TURN 1: Initial weather request
print("TURN 1:")
print("User: What is the current temperature of New York, San Francisco and Chicago?")
response1 = handle_conversation_turn(messages, "What is the current temperature of New York, San Francisco and Chicago?")
print(f"Assistant: {response1}")
# TURN 2: Follow-up with activity and restaurant requests based on previous context
print("\nTURN 2:")
print("User: Based on the weather, which city would be best for outdoor activities? And can you find some restaurant recommendations for that city?")
response2 = handle_conversation_turn(messages, "Based on the weather, which city would be best for outdoor activities? And can you find some restaurant recommendations for that city?")
print(f"Assistant: {response2}")