|
| 1 | +# Copyright (c) 2025 Oracle and/or its affiliates. |
| 2 | +# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/ |
| 3 | + |
| 4 | +from openai.types.responses.function_tool_param import FunctionToolParam |
| 5 | + |
| 6 | + |
| 7 | +def get_stock_price(symbol: str) -> dict: |
| 8 | + """Get the current stock price for a given symbol.""" |
| 9 | + print(f"Fetching stock price for {symbol}...") |
| 10 | + |
| 11 | + # Mock stock data |
| 12 | + return { |
| 13 | + "symbol": symbol.upper(), |
| 14 | + "price": 175.42 if symbol.upper() == "AAPL" else 198.76, |
| 15 | + "currency": "USD", |
| 16 | + "change": 2.34 if symbol.upper() == "AAPL" else -1.23, |
| 17 | + "change_percent": 1.35 if symbol.upper() == "AAPL" else -0.62, |
| 18 | + "last_updated": "2025-06-26T15:00:00Z", |
| 19 | + } |
| 20 | + |
| 21 | + |
| 22 | +def get_current_weather(location: str, unit: str = "fahrenheit") -> dict: |
| 23 | + """Get current weather for a given location. |
| 24 | +
|
| 25 | + Args: |
| 26 | + location: The city and state, e.g., San Francisco, CA |
| 27 | + unit: The unit of temperature (celsius or fahrenheit) |
| 28 | +
|
| 29 | + Returns: |
| 30 | + dict: Weather information |
| 31 | + """ |
| 32 | + # In a real application, you would call a weather API here |
| 33 | + # This is a mock implementation for demonstration |
| 34 | + print(f"Fetching weather for {location} in {unit}...") |
| 35 | + |
| 36 | + # Mock weather data |
| 37 | + weather_data = { |
| 38 | + "location": location, |
| 39 | + "temperature": "72", |
| 40 | + "unit": "fahrenheit", |
| 41 | + "forecast": ["sunny", "windy"], |
| 42 | + "humidity": "65%", |
| 43 | + "description": "Sunny with a gentle breeze", |
| 44 | + } |
| 45 | + |
| 46 | + return weather_data |
| 47 | + |
| 48 | + |
| 49 | +def recommend_clothing(temperature, unit="fahrenheit"): |
| 50 | + """ |
| 51 | + Returns clothing recommendations based on input temperature and unit. |
| 52 | +
|
| 53 | + Parameters: |
| 54 | + temperature (float or int): The temperature value to base the recommendation on. |
| 55 | + unit (str, optional): The unit of the temperature value. |
| 56 | + Can be 'fahrenheit' or 'celsius'. Defaults to 'fahrenheit'. |
| 57 | + """ |
| 58 | + # Convert to Fahrenheit if input is in Celsius |
| 59 | + if unit.lower() == "celsius": |
| 60 | + temperature = temperature * 9 / 5 + 32 |
| 61 | + |
| 62 | + if temperature >= 80: |
| 63 | + return "It's hot! Wear shorts and a t-shirt." |
| 64 | + elif temperature >= 65: |
| 65 | + return "It's warm. A short-sleeve shirt and pants are fine." |
| 66 | + elif temperature >= 50: |
| 67 | + return "It's a bit chilly. Wear a light jacket or sweater." |
| 68 | + elif temperature >= 32: |
| 69 | + return "It's cold. Wear a coat, sweater, and possibly a hat." |
| 70 | + else: |
| 71 | + return "It's freezing! Dress warmly in layers, including a winter coat, gloves, and a hat." |
| 72 | + |
| 73 | + |
| 74 | +fc_tools = [ |
| 75 | + FunctionToolParam( |
| 76 | + name="get_current_weather", |
| 77 | + strict=True, |
| 78 | + parameters={ |
| 79 | + "type": "object", |
| 80 | + "properties": { |
| 81 | + "location": { |
| 82 | + "type": "string", |
| 83 | + "description": "City and country e.g. Bogotá, Colombia", |
| 84 | + } |
| 85 | + }, |
| 86 | + "required": ["location"], |
| 87 | + "additionalProperties": False, |
| 88 | + }, |
| 89 | + type="function", |
| 90 | + description="Get current weather for a given location.", |
| 91 | + ), |
| 92 | + FunctionToolParam( |
| 93 | + name="recommend_clothing", |
| 94 | + strict=True, |
| 95 | + parameters={ |
| 96 | + "type": "object", |
| 97 | + "properties": { |
| 98 | + "temperature": { |
| 99 | + "type": "integer", |
| 100 | + "description": "The temperature value to base the recommendation on.", |
| 101 | + } |
| 102 | + }, |
| 103 | + "required": ["temperature"], |
| 104 | + "additionalProperties": False, |
| 105 | + }, |
| 106 | + type="function", |
| 107 | + description="Returns clothing recommendations based on input temperature and unit.", |
| 108 | + ), |
| 109 | +] |
| 110 | + |
| 111 | + |
| 112 | +def execute_function_call(function_name, function_args): |
| 113 | + # Call the function |
| 114 | + if function_name == "get_current_weather": |
| 115 | + return get_current_weather(**function_args) |
| 116 | + elif function_name == "recommend_clothing": |
| 117 | + return recommend_clothing(**function_args) |
| 118 | + else: |
| 119 | + return None |
0 commit comments