I want my LLM to be able to fetch real-time data from our internal inventory REST API. How do I register a Java function as a tool for the model in Spring AI? Is there a way to let the model decide when to call the function based on the user's prompt without manual intervention?
3 answers
Function calling in Spring AI is handled by defining a java.util.function.Function as a @Bean. You then provide a description of what the function does using the @Description annotation or via the bean name. When you call the AI model, you pass the bean name in the PromptOptions. The framework automatically generates the JSON schema for the tool and sends it to the LLM. If the LLM determines it needs that data (e.g., "What is the stock for item X?"), it returns a tool call request, which Spring AI executes locally before sending the result back to the LLM.
Does this support complex POJOs as input parameters, or am I limited to simple strings? I need to pass a structured filter object to my inventory service.
It's essentially a way to give your AI "hands." It turns a static LLM into an agentic system that can interact with your existing Spring Boot services seamlessly.
Well put, Monica. It's the most powerful feature for enterprise apps where you need the AI to actually "do" things like update records or check live status.
Kevin, it absolutely supports complex POJOs! Spring AI uses Jackson to serialize and deserialize the function arguments. As long as your input record or class has proper getters/setters or is a Java Record, the LLM will generate a JSON object matching that structure. Just make sure your parameter descriptions are very clear so the LLM knows exactly what values to put into each field of your object.