ChatDecisionModel

A DecisionModel that asks a ChatModel, so that choose, isYes, score and decisionEdge work with the model an app already has: Claude, OpenAI, Gemini, Ollama, or any other.

val decider = ChatDecisionModel(AnthropicChatModel(client, apiKey = key, model = "claude-haiku-5-5"))

decisionEdge(from = START, model = decider, instructions = "What does the customer want?", routes = routes, minConfidence = 0.6, fallback = escalate) { it.body }

Each call to decide is one call to the chat model, however many questions the request has. The model is asked how likely it finds each option, and the option it finds most likely is the answer.

  • The probabilities are the model's own estimate. A chat model is not built to measure them, so they are rougher than those of a decision model such as Jev (telar-typesafe), and a confidence of 0.9 is a guess, not a measurement. Try limits such as minConfidence on your own data before you rely on them.

  • confidence is computed as Jev computes it: how far the probability of the most likely option is above an even split, (highest - 1/n) / (1 - 1/n) for n options. It is 0 when every option is as likely and 1 when the model is sure, so a limit means the same with both models.

  • A small, fast model is usually enough. Picking an option is easier than writing an answer.

A failed call to the chat model, an answer cut off at its output limit, and an answer that is not the JSON it was asked for throw a DecisionModelException; for a failed call, the ChatModelException is its cause.

Parameters

model

the chat model that decides.

Constructors

Link copied to clipboard
constructor(model: ChatModel)

Functions

Link copied to clipboard
inline suspend fun <Option : Enum<Option>> DecisionModel.choose(state: String, instructions: String, describe: (Option) -> String? = { null }): Answer.Choice

Asks which constant of the enum Option fits state. The model sees the names of the constants, and for each one the text that describe returns. Read the constant from the answer with value.

suspend fun DecisionModel.choose(state: String, instructions: String, options: Map<String, String?>): Answer.Choice

Asks which of options fits state, for a node or an edge with one decision to make.

Link copied to clipboard
open suspend override fun decide(request: DecisionRequest): DecisionResponse

Returns an answer for each question of request, under the name the question has there.

Link copied to clipboard
suspend fun DecisionModel.isYes(state: String, instructions: String): Answer.YesNo

Asks a yes/no question about state. Read Answer.YesNo.isYes, or compare Answer.YesNo.probability with a line of your own.

Link copied to clipboard
suspend fun DecisionModel.score(state: String, instructions: String, levels: List<String>): Answer.Score

Asks for a rating of state on the scale of levels, described from the lowest to the highest.