VOOZH about

URL: https://thenewstack.io/getting-started-with-function-calling-in-llms/

⇱ LLM Function Calling: How to Get Started - The New Stack


TNS
SUBSCRIBE
Join our community of software engineering leaders and aspirational developers. Always stay in-the-know by getting the most important news and exclusive content delivered fresh to your inbox to learn more about at-scale software development.
REQUIRED
It seems that you've previously unsubscribed from our newsletter in the past. Click the button below to open the re-subscribe form in a new tab. When you're done, simply close that tab and continue with this form to complete your subscription.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.
Welcome and thank you for joining The New Stack community!
Please answer a few simple questions to help us deliver the news and resources you are interested in.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Great to meet you!
Tell us a bit about your job so we can cover the topics you find most relevant.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Welcome!

We’re so glad you’re here. You can expect all the best TNS content to arrive Monday through Friday to keep you on top of the news and at the top of your game.

What’s next?

Check your inbox for a confirmation email where you can adjust your preferences and even join additional groups.

Follow TNS on your favorite social media networks.

Become a TNS follower on LinkedIn.

Check out the latest featured and trending stories while you wait for your first TNS newsletter.

PREV
1 of 2
NEXT
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
Angular
0%
Astro
0%
Svelte
0%
Vue.js
0%
Other
0%
I only use React
0%
I don't use JavaScript
0%
Thanks for your opinion! Subscribe below to get the final results, published exclusively in our TNS Update newsletter:
NEW! Try Stackie AI
From clobbered drafts to real-time sync
Apr 14th 2026 10:00am, by David Moore
TypeScript 6.0 RC arrives as a bridge to a faster future
Mar 14th 2026 9:00am, by Darryl K. Taft
Mastra empowers web devs to build AI agents in TypeScript
Jan 28th 2026 11:00am, by Loraine Lawson
2024-08-09 06:19:04
LLM Function Calling: How to Get Started
sponsor-andela,sponsored-post-contributed,
AI / CI/CD / Large Language Models

LLM Function Calling: How to Get Started

A guide to walk you through the steps to implement function calling using OpenAI APIs to enhance LLMs' capabilities.
Aug 9th, 2024 6:19am by Oladimeji Sowole
👁 Featued image for: LLM Function Calling: How to Get Started
Image from charles taylor on Shutterstock
Andela sponsored this post.

Function calling is a powerful capability in large language models (LLMs) like GPT-4, allowing these models to interact seamlessly with external tools and APIs. This functionality enables LLMs to convert natural language into actionable API calls, making them more versatile and useful in real-world applications. For instance, when a user asks, “What is the weather like in Lagos?” an LLM equipped with function calling can transform this query into a function call to a weather API in Lagos, Nigeria, retrieving the current weather data there.

This integration is essential for building advanced conversational agents or chatbots that require real-time data or need to perform specific actions. Function calling allows developers to define various functions that the LLM can call based on the context and requirements of the conversation. These functions act as tools within your LLM application, enabling tasks such as data extraction, knowledge retrieval and API integration.

With function calling, developers can enhance LLMs’ capabilities, making them conversational, interactive and responsive to user needs. This guide will walk you through the steps to implement function calling using OpenAI APIs, providing a simple, practical example to illustrate the process to enhance the capabilities of our language model (LLM) application. This step-by-step guide with code snippets will demonstrate how to define and call functions dynamically based on user input. We’ll use a movie database to fetch movie details.

Prerequisites

  1. Python is installed on your machine.
  2. OpenAI API key.
  3. dotenv library to manage environment variables.

Setting Up the Environment

First, let’s set up our environment and install the necessary libraries.

pip install openai python-dotenv

Next, create a `.env` file in your project directory and add your OpenAI API key:

OPENAI_API_KEY=your_openai_api_key

Initialize OpenAI API

Load the API key from the `.env` file and set it up in your script.

Set OpenAI API Key

openai.api_key = os.getenv('OPENAI_API_KEY')

Define a Function to Get Movie Details

We’ll create a function that fetches movie details from a dummy movie database.

Define the Function of the API

Now, we’ll define this function as a tool for the OpenAI API to use.

Define a Helper Function to Get Completion

This function will handle the API requests and process the responses.

Example: Fetching Movie Details

Let’s create a conversation where the user asks for movie details and the LLM calls our function to get the necessary information.

Controlling Function Calling Behavior

You can control whether the model should call a function automatically.

Automatic Function Calling

The model decides on its own whether to call a function.

get_completion(messages, tools=tools, tool_choice="auto")

No Function Calling

If you want to force the model to not use any of the functions provided, the code snippet below gives an example of how to implement this.

get_completion(messages, tools=tools, tool_choice="none")

Forced Function Calling

To force the model to call a specific function, you can implement:

get_completion(messages, tools=tools, tool_choice={"type": "function", "function": {"name": "get_movie_details"}})

Handling Multiple Function Calls

The OpenAI API supports calling multiple functions in one turn. For example, fetching details for multiple movies:

Passing Function Results Back to the Model

You might want to pass the result obtained from your API back to the model for further processing.

Conclusion

In this tutorial, we explored how to use function calling with OpenAI API to dynamically fetch and use data based on user input. By defining functions and controlling their invocation, you can create more interactive and capable applications. This method can be applied to various use cases, such as querying databases, calling external APIs or performing calculations.

Feel free to extend this example to suit your specific needs and experiment with different functions and behaviors. Happy coding!

Andela provides the world’s largest private marketplace for global remote tech talent driven by an AI-powered platform to manage the complete contract hiring lifecycle. Andela helps companies scale teams & deliver projects faster via specialized areas: App Engineering, AI, Cloud, Data & Analytics.
Learn More
The latest from Andela
Hear more from our sponsor
TRENDING STORIES
Oladimeji Sowole is a member of the Andela Talent Network, a private marketplace for global tech talent.  A Data Scientist and Data Analyst with more than 6 years of professional experience building data visualizations with different tools and predictive models...
Read more from Oladimeji Sowole
Andela sponsored this post.
SHARE THIS STORY
TRENDING STORIES
TNS owner Insight Partners is an investor in: OpenAI.
SHARE THIS STORY
TRENDING STORIES
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.