> ## Content Index
> Fetch the complete content index at: https://villagesql.com/blog/llms.txt
> Use this file to discover other available public pages before exploring further.

# Jev-Style Decision Models in MySQL with vsql-ai
- URL: https://villagesql.com/blog/jev-decision-models/
- Published: 2026-12-01T14:00:00.000Z
- Updated: 2026-12-01T14:00:02.000Z
- Description: Use probabilities from Gemini Decision, TypeSafe Jev, and OpenAI's Decisions API in MySQL with vsql-ai, and pick a classification threshold in SQL.
- Author: Adam Levin
- Tags: MySQL, AI, Open Source, Extension

Decision models such as TypeSafe's Jev, Google's Gemini Decision 1, and OpenAI's Decisions API answer a question about an input with a probability, which makes them useful for classifying rows in a database. Getting a probability score from these models takes one API call per input. The harder part is choosing the cutoff that counts as yes, and knowing that it's right for your data.

OpenAI's [guide](https://developers.openai.com/api/docs/guides/decisions) on this topic gives an answer: "Use labeled examples from your application to set thresholds for routing, filtering, or review." That means that to derive a threshold, you need some rows where you already know the right answer. Your application may already store these answers. For example, a support application may record which team resolved each ticket. Before you use a stored answer as a label, check two things: that it answers the same question that you ask the model, and that it is correct.

If you don't have such a column, you can label a sample of rows by hand and store each label in a column next to the input. Label at least 100 to 150 rows of each answer (more when an answer is rare). Either way, the labels should be in the same table as the data. The [vsql-ai](https://github.com/villagesql/vsql-ai) extension can then write a decision model's probabilities there too, so checking a threshold becomes a query.

vsql-ai is an extension that runs on [VillageSQL](https://villagesql.com), a drop-in replacement for MySQL that adds PostgreSQL-style extensions to MySQL. Its `ai_decision()` function lets a database call decision APIs directly from SQL. The rest of this post walks through an example of using this function.

## Asking a decision model from SQL

The example in this post is a support team that wants to send billing tickets to a billing queue and other tickets to a general queue. We wrote 20 short support tickets for it and labeled each one by hand, 10 as billing and 10 as other. That is enough to show the method, but too few to choose a real threshold. The tickets are in a `tickets` table, with the ticket text in a `body` column, our label in an `is_billing` column, and an empty JSON column, `answers`, for the model's response.

Setup takes two statements. First, you store the API key in the MySQL keyring, where vsql-ai 0.0.9 reads it. After that, everything refers to the key by name, so a query does not carry the key itself:

```sql
SELECT keyring_key_store('gemini_key', 'SECRET', '<your API key>');

```

Then, you register the model under a model ID, with the provider, the model name, the key's name, and the account that stored the key:

```sql
SET GLOBAL vsql_ai.models = '{
  "decide": {"provider": "google", "model": "gemini-decision-1",
             "key_id": "gemini_key", "key_owner": "root@localhost"}
}';

```

`ai_decision(model_id, input, questions)` then takes that model ID, the text to judge, and a JSON object of named questions. This post asks one yes/no question, which vsql-ai calls a `noul`. You can have more than one question in the JSON, though. The model doesn't know anything about your business. It judges each ticket against the `instructions` and `criteria` that you write, so their wording shapes every probability:

```sql
SET @questions = '{
  "is_billing": {"type": "noul", "instructions": "Is this message about billing?",
                 "criteria": {"true": "About charges, invoices, payments or refunds",
                              "false": "About anything else"}}
}';

```

This `UPDATE` asks the model about every ticket and stores each answer in the `answers` column:

```sql
UPDATE tickets SET answers = ai_decision('decide', body, @questions);

```

Each answer is JSON, keyed by question name. For ticket 3 ("Your app logged me out and now my card on file shows an error when I try to upgrade."), Gemini Decision 1 returned this:

```json
{"is_billing": {"noul": 0.8528589448473877, "type": "noul", "confidence": 0.8528589448473877}}

```

The `noul` value is the probability that the answer is yes.

## Choosing a threshold from labeled rows

With the answers stored, one query puts each ticket's text, our label, and Gemini Decision 1's probability side by side:

```sql
SELECT id, body, is_billing,
       ROUND(JSON_VALUE(answers, '$.is_billing.noul' RETURNING DOUBLE), 3) AS p_billing
FROM tickets ORDER BY id;

```

| id | Ticket                                                                                 | is\_billing | p\_billing |
| -- | -------------------------------------------------------------------------------------- | ----------- | ---------- |
| 1  | I was charged twice for my November invoice. Please refund the duplicate charge today. | 1           | 0.974      |
| 2  | How do I export my data to CSV?                                                        | 0           | 0.01       |
| 3  | Your app logged me out and now my card on file shows an error when I try to upgrade.   | 1           | 0.853      |
| 4  | Love the new dashboard, great work!                                                    | 0           | 0.024      |
| 5  | Can I get a receipt with our VAT number on it? Not urgent.                             | 1           | 0.97       |
| 6  | Production is down since the 2am deploy and every request returns 500.                 | 0           | 0.012      |
| 7  | Why did my bill go up this month? Nothing changed on our side.                         | 1           | 0.978      |
| 8  | The search box ignores accented characters.                                            | 0           | 0.03       |
| 9  | Please cancel our subscription at the end of the term.                                 | 1           | 0.771      |
| 10 | Can you add SSO with Okta?                                                             | 0           | 0.026      |
| 11 | We hit the plan limit on seats. What happens to the extra users?                       | 1           | 0.415      |
| 12 | The invoice PDF link in the email returns a 404.                                       | 1           | 0.925      |
| 13 | How much does the enterprise tier cost compared to what we pay now?                    | 1           | 0.923      |
| 14 | Our trial ended and the account is locked. We want to keep testing for a week.         | 0           | 0.177      |
| 15 | Password reset email never arrives.                                                    | 0           | 0.025      |
| 16 | Is there a discount for nonprofits?                                                    | 1           | 0.918      |
| 17 | The mobile app crashes when I open settings.                                           | 0           | 0.031      |
| 18 | Who do I talk to about moving our account to a different company entity?               | 0           | 0.28       |
| 19 | Our finance team needs last year's statements for the audit.                           | 1           | 0.406      |
| 20 | Dark mode makes the charts unreadable.                                                 | 0           | 0.026      |

Most tickets were clear. The middle of the range held the judgment calls. We labeled tickets 11 and 19 as billing, and the model gave them 0.415 and 0.406\. We labeled tickets 18 and 14 as other, and the model gave them 0.28 and 0.177.

The following query compares several thresholds against the labels:

```sql
SELECT t.threshold,
       SUM(p >= t.threshold AND is_billing)     AS true_pos,
       SUM(p >= t.threshold AND NOT is_billing) AS false_pos,
       SUM(p <  t.threshold AND is_billing)     AS missed,
       ROUND(SUM(p >= t.threshold AND is_billing)
             / NULLIF(SUM(p >= t.threshold), 0), 2) AS prec,
       ROUND(SUM(p >= t.threshold AND is_billing) / SUM(is_billing), 2) AS recall
FROM (SELECT is_billing,
             JSON_VALUE(answers, '$.is_billing.noul' RETURNING DOUBLE) AS p
      FROM tickets) s
CROSS JOIN (SELECT 0.1 AS threshold UNION ALL SELECT 0.3 UNION ALL SELECT 0.5
            UNION ALL SELECT 0.7 UNION ALL SELECT 0.9) t
GROUP BY t.threshold ORDER BY t.threshold;

```

With the result of:

```
threshold  true_pos  false_pos  missed  prec  recall
0.1        10        2          0       0.83  1.00
0.3        10        0          0       1.00  1.00
0.5        8         0          2       1.00  0.80
0.7        8         0          2       1.00  0.80
0.9        6         0          4       1.00  0.60

```

`prec` is precision, the share of tickets at or above the threshold that really are billing, and `recall` is the share of billing tickets that the threshold catches. The obvious default, 0.5, misses two of the 10 billing tickets. On these rows, 0.3 catches all of them and does not flag any "other" ticket.

Treat 0.3 as an example, not a magic number for your own data. We picked it and scored it on the exact same 20 rows. The labels are just our judgment calls, and the four tickets here are borderline enough that someone else might label them differently.

The sample is also too small. At 0.3, recall is 10 out of 10, which sounds perfect, except a 10-for-10 result still leaves room for a true miss rate as high as 26% (at 95% confidence). Catching 140 out of 140 drops that bound to about 2%. Meanwhile, the gap between 0.3 and 0.5 is just two tickets, which is well within random noise. With real traffic, split your labeled rows: pick the threshold on one part and check it on the rest. The query doesn't change at any size, so rerun it whenever you label more rows or reword a question.

Rows without a probability can also skew the table. A failed call returns `NULL` with a warning, and an OpenAI refusal doesn't carry a probability. Such a billing row counts as neither caught nor missed, but it still counts in the recall total, so recall reads lower and `missed` doesn't show why. Find those rows before you run the threshold query, and call the model again for them:

```sql
SELECT id FROM tickets
WHERE JSON_VALUE(answers, '$.is_billing.noul' RETURNING DOUBLE) IS NULL;

```

## Comparing two models on the same rows

A threshold is linked to the model that produced its probabilities. vsql-ai works with Google's `gemini-decision-1`, TypeSafe's `jev-latest` (or a versioned ID such as `jev-1.13.0`), and OpenAI's `gpt-6-luna`, and it returns the same JSON from all three, so trying another model is a one-line change to the registry. We ran the same 20 tickets through `gpt-6-luna`, changing only that line. Its yes/no probabilities were much less spread out: 13 of the 20 answers came back as 0 or 1 (when rounded to three decimal places), so the model was sure, or very nearly sure, about most tickets. It also ranked the borderline tickets differently. It scored ticket 14 (the trial lockout) at 0.81 and ticket 19 (the audit statements) at 0.02, where Gemini Decision 1 gave 0.177 and 0.406.

The same threshold query gives this result:

```
threshold  true_pos  false_pos  missed  prec  recall
0.1        9         1          1       0.90  0.90
0.3        9         1          1       0.90  0.90
0.5        9         1          1       0.90  0.90
0.7        9         1          1       0.90  0.90
0.9        6         0          4       1.00  0.60

```

The first four rows are the same because no ticket scored between 0.1 and 0.7\. At each of those thresholds, gpt-6-luna flags ticket 14, which is not a billing ticket, and misses ticket 19, which is. Raising the threshold to 0.9 drops ticket 14, but it also misses three more billing tickets. A threshold can only trade one kind of mistake for the other within a model's ranking. Before you switch the model, run the same labeled rows through the new model and compare the two tables.

## Routing on a probability

Once you have a threshold, a `CASE` turns a probability into a routing decision. Instead of forcing a binary yes/no on every ticket—especially when 20 rows aren't enough to fully trust 0.3—you can use two cutoffs to separate clear cases from uncertain ones. Tickets at 0.9 or above go straight to the billing queue, anything between 0.3 and 0.9 goes to a person for review, and the rest go to the general queue:

```sql
SELECT id, CASE
         WHEN JSON_VALUE(answers, '$.is_billing.noul' RETURNING DOUBLE) >= 0.9 THEN 'billing queue'
         WHEN JSON_VALUE(answers, '$.is_billing.noul' RETURNING DOUBLE) >= 0.3 THEN 'human review'
         ELSE 'general queue'
       END AS route
FROM tickets ORDER BY id;

```

On the 20 tickets, Gemini Decision 1's answers sent six to the billing queue, four (tickets 3, 9, 11, and 19) to human review, and 10 to the general queue.

## Trying it yourself

`ai_decision()` is new in vsql-ai 0.0.9\. vsql-ai uses VillageSQL preview capabilities, which are off by default, so turn them on before you install the extension:

```sql
SET PERSIST vsql_allow_preview_extensions = ON;
INSTALL EXTENSION vsql_ai;

```

Each account that calls `ai_decision()` also needs the `VSQL_AI_USER` privilege. The [README setup steps](https://github.com/villagesql/vsql-ai#setup) cover the keyring, the API key, and the model registry, and the [Decisions section of the README](https://github.com/villagesql/vsql-ai#decisions) has the full question and answer reference.