Last active
April 1, 2025 12:24
-
-
Save nuhmanpk/7fef4cf4e0632cfb7eb3977507be0490 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
| // 1️⃣ Import the OpenAI SDK | |
| import { OpenAI } from "openai"; | |
| // 2️⃣ Initialize the OpenAI client | |
| const openai = new OpenAI(); | |
| // 3️⃣ Define our two tools: | |
| // a. get_coordinates: To find the latitude and longitude for a given city. | |
| // b. get_weather: To get the current temperature using the coordinates. | |
| const tools = [ | |
| { | |
| "type": "function", | |
| "name": "get_coordinates", | |
| "description": "Find latitude and longitude of a city.", | |
| "parameters": { | |
| "type": "object", | |
| "properties": { | |
| "location": { | |
| "type": "string", | |
| "description": "City and country e.g. Bogotá, Colombia" | |
| } | |
| }, | |
| "required": ["location"], | |
| "additionalProperties": false | |
| } | |
| }, | |
| { | |
| "type": "function", | |
| "name": "get_weather", | |
| "description": "Get current temperature for a given location.", | |
| "parameters": { | |
| "type": "object", | |
| "properties": { | |
| "latitude": { | |
| "type": "number", | |
| "description": "Latitude of the location" | |
| }, | |
| "longitude": { | |
| "type": "number", | |
| "description": "Longitude of the location" | |
| } | |
| }, | |
| "required": ["latitude", "longitude"], | |
| "additionalProperties": false | |
| } | |
| } | |
| ]; | |
| // 4️⃣ Start by passing the initial prompt to the LLM | |
| const userPrompt = "What's the weather in Calicut, kereala?"; | |
| const initialResponse = await openai.responses.create({ | |
| model: "gpt-4o", | |
| input: [{ role: "user", content: userPrompt }], | |
| tools, | |
| }); | |
| console.log("Initial LLM Response:", initialResponse.output); | |
| /* | |
| Expected response example: | |
| [{ | |
| "type": "function_call", | |
| "name": "get_coordinates", | |
| "arguments": "{\"location\":\"Calicut, kereala\"}" | |
| }] | |
| */ | |
| // 5️⃣ Extract the coordinates tool call details from the LLM response. | |
| const coordinatesToolCall = initialResponse.output[0]; | |
| const coordArgs = JSON.parse(coordinatesToolCall.arguments); | |
| // 6️⃣ Define a function to call the coordinate API (using OpenStreetMap's Nominatim API) | |
| async function getCoordinates(location) { | |
| 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) | |
| }; | |
| } | |
| // 7️⃣ Fetch the coordinates for the provided location. | |
| const { latitude, longitude } = await getCoordinates(coordArgs.location); | |
| console.log(`Coordinates for ${coordArgs.location}: Lat ${latitude}, Lng ${longitude}`); | |
| // 8️⃣ Now that we have coordinates, pass them back to the LLM so that it can prepare a call to the weather API. | |
| const weatherResponse = await openai.responses.create({ | |
| model: "gpt-4o", | |
| input: [{ role: "user", content: `What's the weather like at coordinates: latitude ${latitude} and longitude ${longitude}?` }], | |
| tools, | |
| }); | |
| console.log("Weather API LLM Response:", weatherResponse.output); | |
| /* | |
| Expected weather tool response example: | |
| [{ | |
| "type": "function_call", | |
| "name": "get_weather", | |
| "arguments": "{\"latitude\":11.2588, \"longitude\":75.7804}" | |
| }] | |
| */ | |
| // 9️⃣ Extract the weather tool call details. | |
| const weatherToolCall = weatherResponse.output[0]; | |
| const weatherArgs = JSON.parse(weatherToolCall.arguments); | |
| // 🔟 Define a function to call the weather API (using Open-Meteo API) | |
| async function getWeather(latitude, longitude) { | |
| 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; | |
| } | |
| // 1️⃣1️⃣ Fetch the current temperature using the weather API. | |
| const temperature = await getWeather(weatherArgs.latitude, weatherArgs.longitude); | |
| // 1️⃣2️⃣ Log the final result, which is then passed back to the LLM. | |
| console.log(`Current temperature in ${coordArgs.location}: ${temperature}°C`); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment