Skip to main content
Function calling, also known as tool calling, enables models to return structured data specifying a function name and its arguments. This feature is ideal for integrating dynamic, real-world behaviors into your applications by allowing the model to request the execution of predefined functions. To use this feature, define your available functions and pass them as an array to the tools parameter. When appropriate, the model will include a tool_calls array in its response, detailing which function to call and with what arguments. Your application can then parse this data, execute the corresponding function, and return the result to the model for further processing.

Example: A Multi-Turn Weather Bot

This example demonstrates a complete workflow where a model uses a get_weather function to retrieve real-time data. The process involves three main steps:
  1. First API Call: The user asks for the weather. The model receives the prompt and tool definitions and responds with a request to call the get_weather function with the appropriate arguments.
  2. Tool Execution: Your application parses the model’s response, extracts the function name and arguments, and executes your local get_weather function.
  3. Second API Call: Your application sends the result of the get_weather function back to the model, which then formulates a natural language response.

Complete Runnable Scripts

The following scripts for Python, cURL, and JavaScript demonstrate the entire flow.
  • cURL
  • Python - OpenAI
  • Python - Gravix SDK
  • JavaScript
  • JavaScript - Gravix SDK
# cURL Example for the Weather Bot
# This example demonstrates the API calls without actual function execution.

# First API Call - Simulate user asking for the weather
curl -X POST https://api.gravixlayer.com/v1/inference \
-H "Content-Type: application/json" \
-H "Authorization: Bearer your_api_key_here" \
-d '{
  "model": "meta-llama/llama-3.1-8b-instruct",
  "messages": [{"role": "user", "content": "What'\''s the weather like in Paris, France today?"}],
  "tools": [{
      "type": "function",
      "function": {
          "name": "get_weather",
          "description": "Get the current temperature in Celsius for a given latitude and longitude.",
          "parameters": {
              "type": "object",
              "properties": {
                  "latitude": {"type": "number"},
                  "longitude": {"type": "number"}
              },
              "required": ["latitude", "longitude"]
          }
      }
  }],
  "tool_choice": "auto"
}'

# Second API Call - Simulate model responding with a function call
curl -X POST https://api.gravixlayer.com/v1/inference \
-H "Content-Type: application/json" \
-H "Authorization: Bearer your_api_key_here" \
-d '{
  "model": "meta-llama/llama-3.1-8b-instruct",
  "messages": [
      {"role": "user", "content": "What'\''s the weather like in Paris, France today?"},
      {"role": "assistant", "content": "The current temperature in Paris is approximately 18.2 degrees Celsius."}
  ],
  "tools": [],
  "tool_choice": "auto"
}'

Final Response

After the second API call, the model will produce a natural language response similar to this:
The current temperature in Paris is approximately 18.2 degrees Celsius.

Function Schema Reference

Functions are defined in the tools parameter. Each function schema informs the model about the tool’s purpose and expected arguments.
FieldTypeDescription
namestringThe exact name of the function to be called in your code (e.g., get_weather). Must consist of a-z, A-Z, 0-9, or _.
descriptionstringA clear, detailed description of what the function does. This is crucial for helping the model decide when to use the tool.
parametersobjectA standard JSON Schema object defining the function’s input arguments.
I