Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,41 @@ python jsonmode.py --query "Please return a json object to represent Goku from t
- `--query`: Query to be used for function call inference (default: "I need the current stock price of Tesla (TSLA)").
- `--max_depth`: Maximum number of recursive iterations (default: 5).

## Available Functions

The repository includes the following built-in functions:

### Stock/Financial Functions
- `get_current_stock_price` - Get current stock price for a symbol
- `get_stock_fundamentals` - Get fundamental data (PE ratio, market cap, etc.)
- `get_financial_statements` - Get income statement, balance sheet, cash flow
- `get_key_financial_ratios` - Get key financial ratios
- `get_analyst_recommendations` - Get analyst recommendations
- `get_dividend_data` - Get dividend history
- `get_company_news` - Get company news and press releases
- `get_technical_indicators` - Get technical indicators
- `get_company_profile` - Get company profile and overview

### Utility Functions
- `code_interpreter` - Execute Python code
- `google_search_and_scrape` - Search Google and scrape results

### Weather Function
- `get_weather` - Get current weather for any location using wttr.in API

Example usage:
```bash
python functioncall.py --query "What's the weather like in Tokyo?"
```

Example output:
```json
{"location": "Tokyo", "temperature_c": "18", "temperature_f": "64", "condition": "Partly cloudy", "humidity": "72", "wind_speed_kmh": "11", "wind_direction": "NE", "feels_like_c": "18", "precipitation_mm": "0.0"}
```

## Adding Custom Functions

To add your own functions for the model to use, you can modify the `functions.py` script. This script contains various functions that retrieve stock-related information using the `yfinance` library.
To add your own functions for the model to use, you can modify the `functions.py` script. This script contains various functions that retrieve stock-related information using the `yfinance` library, as well as other utility functions like weather lookup.

Here's an example of how to add a new function:

Expand Down
55 changes: 54 additions & 1 deletion functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,58 @@ def get_company_profile(symbol: str) -> dict:
print(f"Error fetching company profile for {symbol}: {e}")
return {}

@tool
def get_weather(location: str) -> dict:
"""
Get current weather information for a given location using the wttr.in API.

Args:
location (str): The city name or location to get weather for (e.g., "London", "New York", "Tokyo").

Returns:
dict: A dictionary containing weather information.
Keys:
- 'location': The requested location.
- 'temperature_c': Current temperature in Celsius.
- 'temperature_f': Current temperature in Fahrenheit.
- 'condition': Weather condition description.
- 'humidity': Humidity percentage.
- 'wind_speed_kmh': Wind speed in km/h.
- 'wind_direction': Wind direction.
- 'feels_like_c': Feels like temperature in Celsius.
- 'precipitation_mm': Precipitation in millimeters.
"""
try:
# wttr.in API with JSON format
url = f"https://wttr.in/{location}?format=j1"
headers = {'User-Agent': 'Mozilla/5.0'}

response = requests.get(url, headers=headers, timeout=10)
response.raise_for_status()

data = response.json()
current = data['current_condition'][0]

weather_info = {
'location': location,
'temperature_c': current.get('temp_C', 'N/A'),
'temperature_f': current.get('temp_F', 'N/A'),
'condition': current.get('weatherDesc', [{}])[0].get('value', 'N/A'),
'humidity': current.get('humidity', 'N/A'),
'wind_speed_kmh': current.get('windspeedKmph', 'N/A'),
'wind_direction': current.get('winddir16Point', 'N/A'),
'feels_like_c': current.get('FeelsLikeC', 'N/A'),
'precipitation_mm': current.get('precipMM', 'N/A')
}

return weather_info
except requests.exceptions.RequestException as e:
print(f"Error fetching weather for {location}: {e}")
return {'error': f"Failed to fetch weather data: {str(e)}"}
except (KeyError, IndexError) as e:
print(f"Error parsing weather data for {location}: {e}")
return {'error': f"Failed to parse weather data: {str(e)}"}

def get_openai_tools() -> List[dict]:
functions = [
code_interpreter,
Expand All @@ -307,7 +359,8 @@ def get_openai_tools() -> List[dict]:
get_key_financial_ratios,
get_analyst_recommendations,
get_dividend_data,
get_technical_indicators
get_technical_indicators,
get_weather
]

tools = [convert_to_openai_tool(f) for f in functions]
Expand Down