Urgent.News

What's breaking now, across thousands of outlets.

AI

How AI Actually Calls an API? Tool Calling Explained from Scratch

In the previous post , we taught a model to read our documents. It could search a pile of files and answer from them, which was very useful. But I still couldn't ask it if it was going to rain, check a live price or even what today's date is. Because as we discussed this earlier, a foundation model on its own is frozen in time. Its knowledge stops at its training cutoff and it's locked in a box.…

In the previous article, we taught a model to read documents and answer questions based on their content. However, we still couldn't ask it about the weather, live prices, or the current date, as the model's knowledge is limited to its training data cutoff. Today, we'll explore how to give the model a way to access live data through tools.

The model doesn't run code independently; instead, it generates a structured request asking to call a specific tool with the necessary inputs. Your code then reads this request, executes the actual tool, and passes the result back to the model for further processing. The model acts as the decision-maker, while your code serves as the hands executing the tool and returning the results.

We'll use Amazon Bedrock and the Converse API to create a tool that fetches the current weather for a given city. The tool description includes a name, a plain-English description, and an input schema specifying the required arguments.

Here's the structure of the tool definition:

```python

WEATHER_TOOL = {

"toolSpec": {

"name": "get_weather",

"description": "Get the current weather for a single city.",

"inputSchema": {

"json": {

"type": "object",

"properties": {

"city": {

"type": "string",

"description": "A plain city name, e.g., Toronto or Paris."

}

},

"required": ["city"]

}

}

}

}

```

This tool description serves as a prompt for the model, determining when and how to use the tool. The actual weather data retrieval function in Python is as follows:

```python

import requests

WEATHER_CODES = {

0: "clear sky",

2: "partly cloudy",

3: "overcast",

61: "light rain",

63: "moderate rain"

}

def get_weather(city: str) -> dict:

geo = requests.get(

"https://geocoding-api.open-meteo.com/v1/search",

params={"name": city, "count": 1},

).json()["results"][0]

now = requests.get(

"https://api.open-meteo.com/v1/forecast",

params={

"latitude": geo["latitude"],

"longitude": geo["longitude"],

"current": "temperature_2m,weather_code,wind_speed_10m"

},

).json()["current"]

return {

"city": geo["name"],

"country": geo["country"]

}

```

Now, when the model needs to provide real-time weather information, it can request the `get_weather` tool with the city name as input. Your code intercepts this request, executes the `get_weather` function, and returns the weather data to the model. The model can then incorporate this information into its response, providing accurate and up-to-date information to the user.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in AI

More from Wednesday 16 September →