|
|
LangChain with OpenAI Functions
Author: Venkata Sudhakar
LangChain integrates with OpenAI function calling to allow agents to reliably call Python functions with structured arguments. Instead of asking the LLM to return JSON and then parsing it, OpenAI function calling guarantees that the LLM will invoke a specific function with arguments matching a defined schema. LangChain wraps this into a clean tool interface that works the same way across OpenAI, Gemini, and Anthropic models. The key difference between function calling and regular prompting is determinism. Regular prompts ask the LLM to describe what to do. Function calling tells the LLM to actually do it - selecting the right function and providing structured arguments that your code can execute directly. This is the foundation of reliable agent behavior in production systems. The below example builds a ShopMax India product assistant using LangChain with OpenAI function calling, showing how the LLM selects and invokes the correct tool based on natural language input.
It gives the following output,
Here are smartphones under Rs 70000 at ShopMax India:
- OnePlus 12 - Rs 64999
For the OnePlus 12 at Rs 64999, the 12-month EMI is:
Rs 5417/month for 12 months (0% interest). Total: Rs 64999
The agent automatically chained two function calls - first searching for smartphones under Rs 70000, then calculating EMI for the cheapest result - without any explicit orchestration code. Function calling ensures arguments are typed and validated before your code runs. For ShopMax production systems, replace the mock data with real API calls to your product catalogue and pricing engine.
|
|