Skip to main content

Tool Calling in Confidential AI

Tool calling, also known as function calling, enables AI models to interact with external systems by invoking predefined functions or APIs. This allows the model to access up-to-date information, perform specific actions, or integrate with other services, extending its capabilities beyond static knowledge. Here we provide a simple example of tool calling.

Define the Tool

Use following request to create a function named get_weather.
curl -s -X POST  'https://api.redpill.ai/v1/chat/completions' \
  -H 'Authorization: Bearer sk-xx' \
  -H 'Content-Type: application/json' \
  -d '{
  "messages": [
    {
      "content": "What is the weather like in New York?",
      "role": "user"
    }
  ],
  "tools": [
      {
          "type": "function",
          "function": {
              "name": "get_weather",
              "description": "Get the current weather in a specified city",
              "parameters": {
                  "type": "object",
                  "properties": {
                      "city": {"type": "string"},
                      "metric": {"type": "string", "enum": ["celsius", "fahrenheit"]}
                  },
                  "required": ["city"]
              }
          }
      }
  ],
  "tool_choice": "auto",
  "stream": false,
  "model": "phala/qwen3-coder"
}'
{
  "id": "chatcmpl-28f745c2b7ee44f2ba36a8b4b409c74a",
  "object": "chat.completion",
  "created": 1754381277,
  "model": "qwen/qwen3-coder",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": null,
        "refusal": null,
        "annotations": null,
        "audio": null,
        "function_call": null,
        "tool_calls": [
          {
            "id": "chatcmpl-tool-471f2c795ab64a27ab671a9e302c94db",
            "type": "function",
            "function": {
              "name": "get_weather",
              "arguments": "{\"city\": \"New York\", \"metric\": \"celsius\"}"
            }
          }
        ],
        "reasoning_content": null
      },
      "logprobs": null,
      "finish_reason": "tool_calls",
      "stop_reason": null
    }
  ],
  "service_tier": null,
  "system_fingerprint": null,
  "usage": {
    "prompt_tokens": 319,
    "total_tokens": 353,
    "completion_tokens": 34,
    "prompt_tokens_details": null
  },
  "prompt_logprobs": null,
  "kv_transfer_params": null
}
Extract the tool id from the response, and in this example it would be chatcmpl-tool-471f2c795ab64a27ab671a9e302c94db.

Invoke the Tool

Use the tool id extracted from previouse request to call the tool.
curl -s -X POST  'https://api.redpill.ai/v1/chat/completions' \
  -H 'Authorization: Bearer sk-xx' \
  -H 'Content-Type: application/json' \
  -d '{
    "messages": [
      {"role": "user", "content": "What is the weather like in New York?"},
      {
        "role": "assistant",
        "content": null,
        "tool_calls": [
          {
            "id": "chatcmpl-tool-471f2c795ab64a27ab671a9e302c94db",
            "type": "function",
            "function": {
              "name": "get_weather",
              "arguments": "{\"city\": \"New York\", \"metric\": \"celsius\"}"
            }
          }
        ]
      },
      {
        "role": "tool",
        "content": "{\"city\": \"New York\", \"temperature\": 15, \"description\": \"clear sky\", \"metric\": \"celsius\"}",
        "tool_call_id": "chatcmpl-tool-471f2c795ab64a27ab671a9e302c94db"
      }
    ],
    "tools": [
      {
          "type": "function",
          "function": {
              "name": "get_weather",
              "description": "Get the current weather in a specified city",
              "parameters": {
                  "type": "object",
                  "properties": {
                      "city": {"type": "string"},
                      "metric": {"type": "string", "enum": ["celsius", "fahrenheit"]}
                  },
                  "required": ["city"]
              }
          }
      }
    ],
    "tool_choice": "auto",
    "stream": false,
    "model": "phala/qwen3-coder"
  }'
{
  "id": "chatcmpl-a46eff3d335c42c39bbe4ea69fc97462",
  "object": "chat.completion",
  "created": 1754381325,
  "model": "qwen/qwen3-coder",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "The current weather in New York is clear sky with a temperature of 15°C.",
        "refusal": null,
        "annotations": null,
        "audio": null,
        "function_call": null,
        "tool_calls": [],
        "reasoning_content": null
      },
      "logprobs": null,
      "finish_reason": "stop",
      "stop_reason": null
    }
  ],
  "service_tier": null,
  "system_fingerprint": null,
  "usage": {
    "prompt_tokens": 394,
    "total_tokens": 413,
    "completion_tokens": 19,
    "prompt_tokens_details": null
  },
  "prompt_logprobs": null,
  "kv_transfer_params": null
}

Supported Models

  • phala/deepseek-chat-v3-0324
  • phala/qwen3-coder
  • phala/llama-3.3-70b-instruct
I