Package dyntabs.ai

Class DecisionBuilder

java.lang.Object
dyntabs.ai.DecisionBuilder

public class DecisionBuilder extends Object
Asks a small, cheap model a closed question about some text and gets back a probability — not prose.

What this replaces

Routing, triage, classification and gating are usually done with a full chat completion and a structured-output prompt. That is slow, costs a whole generation, and returns a word when what you needed was a number you can threshold. A decision call returns the number:


 // one question, no names, no response object to unpack
 if (EasyAI.decide(ticketText).isYes("Is the customer completely blocked?", 0.80)) {
     pager.wake();
 }

 String team = EasyAI.decide(ticketText)
                     .choose("Which team should handle this?", "billing", "technical", "sales");
 

Several questions, one call

The cost win is asking everything at once. Name each question, then read the answers back by that name:


 Decisions d = EasyAI.decide(ticketText)
     .yesNo("urgent",   "Is the customer completely blocked?")
     .choice("team",    "Which team should handle this?", "billing", "technical", "sales")
     .scale("severity", "How severe is the business impact?", "low", "medium", "high")
     .ask();

 if (d.isYes("urgent", 0.80)) pager.wake();
 route(d.choice("team"));
 d.margin("team").ifPresent(m -> { if (m < 0.15) queueForHuman(); });
 

Why this fits the rest of EasyAI

It is the same principle as EasyAI.flow(): the deterministic spine stays in Java, the model is only a sensor. A decision slots straight into a flow's condition, so a pipeline can branch on a measured probability while remaining auditable — the model supplies the reading, your Java supplies the threshold:


 EasyAI.flow()
     .step("read", ctx -> ticketText)
     .stepIf("escalate",
             ctx -> EasyAI.decide(ctx.inputText()).isYes("Is the customer angry?", 0.75),
             ctx -> escalationService.open(ctx))
     .run(ticketText);
 

Analogy: a thermometer, not a doctor. It gives you 38.7 °C. What counts as a fever, and what you do about it, stays with the person who has to answer for the decision.

See Also:
  • Method Details

    • withConfig

      public DecisionBuilder withConfig(DecisionConfig config)
      Replace the whole configuration instead of overriding single values.
      Parameters:
      config - fully built decision settings; null restores properties-based defaults
      Returns:
      this builder
    • withModel

      public DecisionBuilder withModel(String modelName)
      Name the decision model for this call (default DecisionConfig.DEFAULT_MODEL).
      Parameters:
      modelName - the provider's decision-model id
      Returns:
      this builder
    • withProvider

      public DecisionBuilder withProvider(String provider)
      Override the provider for this call.
      Parameters:
      provider - provider id (currently "openai")
      Returns:
      this builder
    • withApiKey

      public DecisionBuilder withApiKey(String apiKey)
      Override the API key for this call (otherwise easyai.decision.api-key, then easyai.api-key).
      Parameters:
      apiKey - the provider API key
      Returns:
      this builder
    • withBaseUrl

      public DecisionBuilder withBaseUrl(String baseUrl)
      Override the base URL for this call. Read DecisionConfig first: decisions use a dedicated endpoint that OpenAI-compatible chat providers do not implement.
      Parameters:
      baseUrl - the endpoint to call
      Returns:
      this builder
    • withEventListener

      public DecisionBuilder withEventListener(EasyAIListener eventListener)
      Register a listener for the live EasyAIEvent stream, so a decision appears in your activity timeline like every other EasyAI operation, under EasyAIEvent.Source.DECISION.

      The event detail carries the numbers and the threshold, not just the verdict — which is what turns a timeline into an audit trail.

      Parameters:
      eventListener - the listener (may be null to disable events)
      Returns:
      this builder
    • yesNo

      public DecisionBuilder yesNo(String name, String question)
      Add a yes/no question.
      Parameters:
      name - the name to read the answer back by
      question - the question, phrased so that "yes" is unambiguous
      Returns:
      this builder
    • yesNo

      public DecisionBuilder yesNo(String name, String question, String yesWhen, String noWhen)
      Add a yes/no question, spelling out what counts as yes and what counts as no.

      Worth the extra two arguments when the boundary is the hard part — "urgent" means different things to a support desk and to a payments team, and saying so beats hoping the model guesses your definition.

      Parameters:
      name - the name to read the answer back by
      question - the question
      yesWhen - what makes the answer yes
      noWhen - what makes the answer no
      Returns:
      this builder
    • choice

      public DecisionBuilder choice(String name, String question, String... options)
      Add a single-choice question.
      Parameters:
      name - the name to read the answer back by
      question - the question
      options - at least two options; the answer comes back as one of these strings
      Returns:
      this builder
    • choice

      public DecisionBuilder choice(String name, String question, List<String> options)
      Add a single-choice question from a list.
      Parameters:
      name - the name to read the answer back by
      question - the question
      options - at least two options
      Returns:
      this builder
    • choice

      public DecisionBuilder choice(String name, String question, Map<String,String> optionDescriptions)
      Add a single-choice question whose options carry descriptions.

      Use this when the option names are codes your system understands but a model would not — "L2_NET" means nothing on its own, "L2_NET" -> "network/connectivity faults" does.

      Parameters:
      name - the name to read the answer back by
      question - the question
      optionDescriptions - option → what it means; at least two entries
      Returns:
      this builder
    • scale

      public DecisionBuilder scale(String name, String question, String... levels)
      Add an ordered-scale question.

      The answer is a probability-weighted position across the levels, so it can land between two of them — which is usually the honest answer.

      Parameters:
      name - the name to read the answer back by
      question - the question
      levels - at least two levels, in order from lowest to highest
      Returns:
      this builder
    • ask

      public Decisions ask()
      Send every question added so far as one call.
      Returns:
      the answers
      Throws:
      IllegalArgumentException - if no question was added
    • isYes

      public boolean isYes(String question, double threshold)
      Ask one yes/no question and decide at your threshold — the shortest useful form.
      Parameters:
      question - the question
      threshold - the probability at or above which this counts as "yes" (0.0–1.0)
      Returns:
      true if the reported probability reaches threshold
    • probability

      public double probability(String question)
      Ask one yes/no question and get the raw probability, deciding yourself.
      Parameters:
      question - the question
      Returns:
      the probability of "yes", 0.0–1.0
    • choose

      public String choose(String question, String... options)
      Ask one choice question and get the selected option.
      Parameters:
      question - the question
      options - at least two options
      Returns:
      the chosen option, exactly as spelled here
    • rate

      public double rate(String question, String... levels)
      Ask one scale question and get the weighted position across the levels.
      Parameters:
      question - the question
      levels - at least two levels, in order from lowest to highest
      Returns:
      the weighted index, from 0.0 to levels.length - 1