Using Jev AI with Vary's AI library

The AI library introduction covers review and drafting. Here, examples/ai-triage uses Jev to recommend a priority and a team for a support message. The app leaves the ticket alone.

Consider a shared support inbox. "Checkout failed while taking payment from a customer" might need urgent attention, and it might belong with billing or engineering. The triage code asks Jev about urgency and team, then returns a recommendation. Uncertain answers go to a person.

Jev is configured through the TypeSafe adapter in Vary's ai library. The AI library guide covers the imports; the semantic AI guide covers ai using syntax.

Where the capabilities come from

The app chooses the TypeSafe adapter. Its decisions constructor supplies a Judge for yes/no questions and a Router for choosing among descriptions. The model name tells the provider which model to call; it does not create either capability.

The triage app supplies no Generator. Its feels expression uses the Judge; by meaning uses the Router.

Configure the model

The gateway profile names typesafe-ai/jev, but the triage app takes its model from AI_JUDGE_MODEL. In main.vary, the settings sit together:

let {
    key = system.env("AI_GATEWAY_API_KEY") ?: ""
    model = system.env("AI_JUDGE_MODEL") ?: ""
    endpoint = system.env("AI_JUDGE_ENDPOINT") ?: gateway_profile().endpoint
}

The gateway profile supplies the default endpoint. It supplies no key and makes no promise that the endpoint accepts the chosen model. With a key, main.vary calls live_context from src/setup.vary:

from ai.decision_context import decision_context
from ai.adapters.typesafe.decisions import decisions as typesafe_decisions
from ai.context import AiContext
from ai.types import AiError

def live_context(key: Str, model: Str, endpoint: Str) -> Result[AiContext, AiError] {
    let provider = typesafe_decisions(key, model, endpoint, 3)?
    return decision_context(provider, 2, 10000)
}

typesafe_decisions checks the key, model, endpoint, and route limit without calling the provider. decision_context gives the Judge and Router one budget: two attempts across a 10,000 ms run. Each request also has a 10,000 ms timeout, capped by the time left in the run. There is no automatic retry. The default thresholds are 0.8 for judgment, 0.75 for routing probability, and 0.65 for routing confidence; both loop limits are one. The AI library guide shows how to change them. A setup error passes back through ? as AiError.

Follow the triage code

triage_issue takes a message and an AiContext. It returns a TriageReport or an AiError. These are the report's types from src/domain.vary:

enum Priority {
    Immediate
    Normal
    HumanReview
}

enum Team {
    Billing
    Technical
    General
    HumanReview
}

data TriageReport {
    priority: Priority
    team: Team
}

Here is the decision code from src/workflow.vary:

def triage_issue(issue: Str, context: AiContext) -> Result[TriageReport, AiError] {
    mut priority = Priority.Normal
    mut team = Team.General
    ai using context {
        if issue feels "requires immediate attention" {
            priority = Priority.Immediate
        } uncertain {
            priority = Priority.HumanReview
        }

        match issue by meaning {
            case "a billing or payment problem" { team = Team.Billing }
            case "a software defect or technical failure" { team = Team.Technical }
            case _ { team = Team.General }
        } uncertain {
            team = Team.HumanReview
        }
        return Ok(TriageReport(priority, team))
    }
}

The Judge decides whether the message needs immediate attention. A yes sets Immediate; an uncertain answer sets HumanReview. Then the Router chooses billing, technical, or the general fallback. Routing uncertainty also goes to HumanReview. Both calls spend from the same two-attempt budget. A provider failure returns AiError.

Finally, main.vary supplies the example message and prints the result:

match triage_issue("Checkout failed while taking payment from a customer.", context) {
    case Ok(report) {
        print(render_report(report, mode, selected_model))
        return 0
    }
    case Err(error) {
        print("triage failed: " + str(error))
        process.exit(1)
        return 1
    }
}

render_report turns the typed values into words for the output. The program recommends where the ticket should go.

The code keeps the two questions separate: urgency and team. Priority and Team name the allowed answers, and each decision has an uncertainty path. The returned report is a recommendation for the rest of the support system to inspect.