Decision Models in Spring AI 2... Note
Spring

Decision Models in Spring AI 2.1: Yes, No, and How Sure

Applications often need to make typed decisions with predefined outcomes. While chat models can handle these tasks, converting their text replies into structured data adds complexity. Specialized decision models, like Jev, offer comparable accuracy with significantly lower latency and cost for classification and evaluation where input data is sufficient. Spring AI 2.1 introduces a DecisionModel API for these tasks, providing typed answers with probabilities directly. This API allows direct integration of model-based decisions into application control flow, eliminating the need for text generation and parsing.The article compares a local Ollama model with TypeSafe's Jev model across practical applications: guardrails and a judge. The DecisionModel API supports three question types: YesNoQuestion, ChoiceQuestion, and ScoreQuestion, each returning relevant data and probabilities. Probabilities are crucial for nuanced decision-making, enabling applications to distinguish between confident and uncertain responses for automated processing versus human review.The API is provider-independent, working with Ollama, OpenAI, and System One protocols. Guardrails, a natural application for yes/no questions, can screen messages for various hazards. Spring AI Decision Extensions provide a DecisionGuardrailAdvisor to evaluate user input and model output against predefined hazard "batteries". The advisor can pass, block, review, or support messages based on hazard scores and configurable thresholds.For instance, a message indicating self-harm can trigger a "SUPPORT" action, providing relevant resources. The interaction between probability and severity determines the action taken. Custom hazards can be defined, and advisor ordering is important, particularly for input screening to prevent blocked messages from being stored in session memory.