Skip to main content

Why Use Function Calling?

Function calling lets you extend LLM capabilities by allowing models to invoke external functions and tools. This enables your applications to perform actions like fetching real-time data, interacting with APIs, or executing computations based on the model’s reasoning.

Prerequisites

  • An API key from Prem Studio
  • Python ≥ 3.8 or Node.js ≥ 18
  • premai or openai SDK installed

Supported Models

The list of supported models is constantly evolving. For the most up-to-date list of models that support function calling, please visit the models list page. Models that support function calling are clearly marked with a tools tag on the models page.

Generate Function Calls with Prem

This quick guide shows you how to implement a weather assistant that can fetch current weather information using function calling. You’ll define a weather function, let the model decide when to call it, and handle the tool responses. Why it’s useful: It enables your AI assistant to perform real-world actions and access live data — not just generate text responses. When to use it: Perfect for building AI assistants, chatbots, or automation tools that need to interact with external APIs, databases, or perform calculations based on user queries.
1

Setup Environment

2

Define Function Schema and Mock Implementation

3

Initialize Client

4

Send Initial Request with Tools

5

Handle Tool Calls and Execute Functions

The json_repair.loads() function automatically handles malformed JSON in function arguments, which is more robust than using JSON.parse() or json.loads() directly, as models sometimes generate slightly malformed JSON.

Full Copy-Paste Example

Pro Tips

  • We use the json-repair package to automatically handle malformed JSON in function arguments, making the parsing much more robust than using json.loads().
  • Always validate function arguments before executing functions to prevent errors or security issues.
  • Use descriptive function names and detailed descriptions to help the model understand when to call each function.
  • Set tool_choice: "auto" to let the model decide when to use functions, or set it to "none" to disable function calling for a specific request.
  • For production applications, implement proper error handling and logging for function executions.
  • Consider implementing rate limiting and authentication for external API calls within your functions.

Common Use-Cases

  • Building AI assistants that can fetch real-time data (weather, stock prices, news)
  • Creating chatbots that can interact with databases or APIs
  • Implementing AI agents that can perform calculations or data processing
  • Developing automation tools that can execute actions based on natural language commands

Tool Choice Options

You can control when and how the model uses functions with the tool_choice parameter:
  • "auto" (default): Model decides whether to call functions
  • "none": Model will not call any functions
  • {"type": "function", "function": {"name": "function_name"}}: Force the model to call a specific function