Created
May 10, 2025 09:07
-
-
Save nuhmanpk/0b6aaf739b538452ef82bfec2237f11a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import 'dotenv/config'; | |
| import { GoogleGenAI, Type } from '@google/genai'; | |
| const ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY }); | |
| const getCoordinatesFunction = { | |
| name: 'get_coordinates', | |
| description: 'Find latitude and longitude of a city.', | |
| parameters: { | |
| type: Type.OBJECT, | |
| properties: { | |
| location: { | |
| type: Type.STRING, | |
| description: 'City and country (e.g., Calicut, Kerala)', | |
| }, | |
| }, | |
| required: ['location'], | |
| }, | |
| }; | |
| const getWeatherFunction = { | |
| name: 'get_weather', | |
| description: 'Get current temperature for a given location.', | |
| parameters: { | |
| type: Type.OBJECT, | |
| properties: { | |
| latitude: { | |
| type: Type.NUMBER, | |
| description: 'Latitude of the location', | |
| }, | |
| longitude: { | |
| type: Type.NUMBER, | |
| description: 'Longitude of the location', | |
| }, | |
| }, | |
| required: ['latitude', 'longitude'], | |
| }, | |
| }; | |
| async function getCoordinates(location: string) { | |
| const response = await fetch( | |
| `https://nominatim.openstreetmap.org/search?q=${encodeURIComponent(location)}&format=json` | |
| ); | |
| const data = await response.json(); | |
| if (data.length === 0) { | |
| throw new Error('Location not found'); | |
| } | |
| return { | |
| latitude: parseFloat(data[0].lat), | |
| longitude: parseFloat(data[0].lon), | |
| }; | |
| } | |
| async function getWeather(latitude: number, longitude: number) { | |
| const response = await fetch( | |
| `https://api.open-meteo.com/v1/forecast?latitude=${latitude}&longitude=${longitude}¤t=temperature_2m` | |
| ); | |
| const data = await response.json(); | |
| return data.current.temperature_2m; | |
| } | |
| async function getWeatherInCity(userPrompt: string) { | |
| const response = await ai.models.generateContent({ | |
| model: 'gemini-2.0-flash', | |
| contents: userPrompt, | |
| config: { | |
| tools: [ | |
| { functionDeclarations: [getCoordinatesFunction] }, | |
| { functionDeclarations: [getWeatherFunction] }, | |
| ], | |
| }, | |
| }); | |
| if (response.functionCalls && response.functionCalls.length > 0) { | |
| const coordinatesCall = response.functionCalls[0]; | |
| // Directly use coordinatesCall.args if it's already an object | |
| const { location } = coordinatesCall.args; | |
| const { latitude, longitude } = await getCoordinates(location); | |
| const weatherResponse = await ai.models.generateContent({ | |
| model: 'gemini-2.0-flash', | |
| contents: `What's the weather like at coordinates: latitude ${latitude} and longitude ${longitude}?`, | |
| config: { | |
| tools: [ | |
| { functionDeclarations: [getWeatherFunction] }, | |
| ], | |
| }, | |
| }); | |
| if (weatherResponse.functionCalls && weatherResponse.functionCalls.length > 0) { | |
| const weatherCall = weatherResponse.functionCalls[0]; | |
| const weatherArgs = weatherCall.args; | |
| const temperature = await getWeather(weatherArgs.latitude, weatherArgs.longitude); | |
| console.log(`Current temperature in ${location}: ${temperature}°C`); | |
| } | |
| } | |
| } | |
| const userPrompt = "What's the weather in malappuram, Kerala?"; | |
| getWeatherInCity(userPrompt); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment