Discover the Power of Function Calling
Imagine if AI didn’t just respond to your chats with generic responses, but could also retrieve data to make accurate and up-to-date statements. This is possible through the power of function calling.
What is Function Calling?
While traditional AI models excel at generating responses based on the data they’ve been trained on, they are inherently static - frozen in time the moment training ends.
Function calling solves this by allowing models to delegate queries back to their hosted app environment. When an AI receives a request that requires information beyond its knowledge it can get more information by using a function call before it proceeds.
It’s Not Executing Code, It’s Asking for Help
Despite what it seems at first, function calling does not allow an LLM to execute arbitrary code on its own. Instead, it’s about making the model aware of parts of the app that are ready to deliver a useful response.
How a Function Call Works in Practice
Let’s walk through a scenario where a user asks a chat-based finance app for Apple’s current stock price.
While the AI model likely knows that Apple's stock ticker is "AAPL", it has no way of knowing the real-time stock price - since that’s external data, outside of the model’s frozen training. This is where function calling comes into play.
Step 1: Teaching the Model Its Tools
Before a user chat is sent to the model, a system chat informs the model that the app environment has a tool called "get_current_stock_price" with details explaining how and why it may be used.
Step 2: User Prompt
At some point in the conversation, the user asks the AI: "What’s the current stock price for Apple?" The AI understands the question and recognizes that this is something it cannot answer directly since it requires live market data.
Step 3: The AI Proposes a Function Call
Instead of hallucinating an answer or providing outdated information, the AI takes stock of its available tools and generates a function call for the app to handle:
Note: The AI doesn’t execute this function. It sends an invisible chat back to the application, saying it needs more information from the response before it can reply to the user.
Step 4: The Application Executes the Function
The app then executes prewritten code, containing an HTTP request to a stock market api, and sends a formatted response back to the model.
Step 5: The AI Uses the Data to Reply
With this real-time data provided from the function call, the AI can now complete its response to the user.
In Summary
Function calling is a great way to provide AI models with the information they need to be helpful. The data could come from a third-party API or be queried from your own database, and the possibilities for what AI can do with this data are endless.
To Be Continued…
This week we discussed how function calling is used to retrieve real time data to generate helpful and accurate conversations.
The other incredible use for function calling is to empower AI models to trigger actions in our applications that get real work done, like sending emails, queueing up jobs, modifying database records and much more.
We’ll have a full exploration on that in a future Wednesday’s newsletter, so stay tuned.
Explore More
Want to dive deeper into this and other ways AI can elevate your web apps? Our AI-Driven Laravel course and newsletter covers this and so much more!
👉 Check Out the Course: aidrivenlaravel.com
If you’ve missed previous newsletters, we got you: aidrivenlaravel.com/newsletters
Thanks for being part of our community! We’re here to help you build smarter, AI-powered apps, and we can’t wait to share more with you next week.