Metadata-Version: 2.4
Name: fastdecisions
Version: 0.0.2
Summary: Unified async Python client for decision models (Jev, Gemini, OpenAI)
Author-email: Nathan Cooper <nathanacooper@proton.me>
License-Expression: Apache-2.0
Project-URL: Repository, https://github.com/AnswerDotAI/fastdecisions
Project-URL: Documentation, https://AnswerDotAI.github.io/fastdecisions/
Keywords: decisions,llm,jev,gemini,openai
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Requires-Python: >=3.11
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: fastcore>=2.2.46
Requires-Dist: fastspec>=0.2.12
Requires-Dist: fasttransport>=0.0.5
Requires-Dist: aidialog>=0.0.44
Requires-Dist: httpx2>=2.16.2
Requires-Dist: pillow
Provides-Extra: dev
Requires-Dist: pytest; extra == "dev"
Requires-Dist: pycachy>=0.0.22; extra == "dev"
Dynamic: license-file

# fastdecisions

`fastdecisions` provides a single Jev-style async interface for decision models across TypeSafe Jev (`jev-1.14.0`), Google Gemini (`gemini-decision-1`), and OpenAI (`gpt-6-luna`). Pass shared evidence—text, JSON, [`aidialog.msg_parts.Msg`](https://AnswerDotAI.github.io/aidialog/msg_parts.html#msg) chat histories, or media—alongside batched `noul` (yes/no probability), `choice` (classification), `score` (rubric rating), or `box` (bounding box) questions, and swap providers with a single `model=` argument. Each provider adapter runs on `fastspec.OpenAPIClient` with offline bundled specs.

## Install

Run `pip install fastdecisions` or clone from GitHub and install locally (`pip install -e .`).

Set whichever provider key(s) you plan to use: `TYPESAFE_API_KEY` (TypeSafe Jev), `GEMINI_API_KEY` (Google Gemini), or `OPENAI_API_KEY` (OpenAI).

## Quickstart

``` python
from fastdecisions import adecide, decide, Decider, noul, choice, score, box, jev, gem_dec, luna
```

For a one-off yes/no check with a single provider (`TYPESAFE_API_KEY`), pass a bare string as `questions` and read `r.answer.noul`:

``` python
ticket = 'I was charged twice for my subscription this month and nobody has replied yet. Fix this today!'
r_one = await adecide(ticket, 'Is this message about billing?', model=jev)
f"p={r_one.answer.noul:.3f}, confidence={r_one.answer.confidence:.3f}"
```

    'p=0.990, confidence=0.990'

## Decisions across providers

Define questions once using Jev’s canonical schema—plain strings act as `noul` yes/no shorthand, `noul(..., true=..., false=...)` adds boundary criteria, `choice` accepts either a list of labels or a `{label: description}` dict, and `score` takes an ordered rubric—then swap `model=` across Jev, Gemini, and GPT-6 Luna:

``` python
qs = dict(billing='Is this message about billing?',
    prior_contact=noul('Has the customer contacted support about this before?',
        true='Mentions a prior attempt, ticket, or waiting for a reply', false='No sign of any previous contact'),
    tone=choice(['angry', 'calm', 'excited'], 'What is the tone of this message?'),
    urgency=score(['Can wait', 'Needs attention this week', 'Needs attention today'], 'How urgent is this message?'))

for m in (jev, gem_dec, luna):
    r = await adecide(ticket, qs, model=m)
    print(f"{m:18} billing={r.billing.noul:.2f}  prior={r.prior_contact.noul:.2f}  tone={r.tone.choice:7}  urgency={r.urgency.score:.2f}  cost=${r.cost:.6f}")
```

    jev-1.14.0         billing=0.98  prior=0.73  tone=angry    urgency=2.00  cost=$0.000018
    gemini-decision-1  billing=0.99  prior=0.92  tone=angry    urgency=1.88  cost=$0.000030
    gpt-6-luna         billing=1.00  prior=1.00  tone=angry    urgency=2.00  cost=$0.000057

Inspect a `Decision` bare in a notebook to render a summary table alongside collapsible raw JSON, or access answers by attribute (`r.tone.choice`) or key (`r['tone'].probabilities`):

``` python
r
```

| Question | Type | Value | Confidence |
|----|----|----|----|
| `billing` | `noul` | 1.000 | 1.000 |
| `prior_contact` | `noul` | 1.000 | 1.000 |
| `tone` | `choice` | `angry` | 1.000 |
| `urgency` | `score` | 2.000 | 1.000 |

<details>
<summary>Details</summary>

- model: `gpt-6-luna`
- tokens: 575 in / 0 out
- cost: \$0.000057
- `billing`: `NoulAnswer(noul=1.0, confidence=1.0, raw={'type': 'predicate', 'name': 'billing', 'probability': 1.0})`
- `prior_contact`: `NoulAnswer(noul=1.0, confidence=1.0, raw={'type': 'predicate', 'name': 'prior_contact', 'probability': 1.0})`
- `tone`: `ChoiceAnswer(choice='angry', confidence=1.0, probabilities={'angry': 1.0, 'calm': 0.0, 'excited': 0.0}, raw={'type': 'choice', 'name': 'tone', 'choice': 'angry', 'probabilities': [{'value': 'angry', 'probability': 1.0}, {'value': 'calm', 'probability': 0.0}, {'value': 'excited', 'probability': 0.0}], 'confidence': 1.0})`
- `urgency`: `ScoreAnswer(score=2.0, confidence=1.0, probabilities={'0': 0.0, '1': 0.0, '2': 1.0}, legend={'0': 'Can wait', '1': 'Needs attention this week', '2': 'Needs attention today'}, raw={'type': 'score', 'name': 'urgency', 'score': 2.0, 'probabilities': [{'value': 0, 'label': 'Can wait', 'probability': 0.0}, {'value': 1, 'label': 'Needs attention this week', 'probability': 0.0}, {'value': 2, 'label': 'Needs attention today', 'probability': 1.0}], 'confidence': 1.0})`

</details>

## Reusable `Decider` and batching

Bind a question schema and model once in a `Decider` to evaluate many items concurrently with `.batch(...)` while tracking cumulative token `usage` and USD `cost`:

``` python
triage = Decider(dict(billing='Is this about billing?', tone=choice(['angry', 'calm', 'excited'], 'Tone?')), model=jev)
tickets = ['You double-charged my card and I want an immediate refund!',
    'Love the new dark mode, great work team!',
    'Could you explain how to export a CSV from my dashboard?']
for t, res in zip(tickets, await triage.batch(tickets)): print(f"tone={res.tone.choice:7}  billing={res.billing.noul:.2f}  | {t}")
print(f"Total: {triage.usage.prompt_tokens} input tokens, ${triage.cost:.6f}")
```

    tone=angry    billing=0.98  | You double-charged my card and I want an immediate refund!
    tone=excited  billing=0.02  | Love the new dark mode, great work team!
    tone=calm     billing=0.10  | Could you explain how to export a CSV from my dashboard?
    Total: 951 input tokens, $0.000040

## Chat history

Pass [`aidialog.msg_parts.Msg`](https://AnswerDotAI.github.io/aidialog/msg_parts.html#msg) conversations directly as `state`. Each adapter strips internal `Thinking` blocks and converts the history into its provider format (JSON on Jev, structured JSON on Gemini, native Responses items on OpenAI):

``` python
from aidialog.msg_parts import Msg, Text, Thinking
```

``` python
history = [Msg('user', [Text('My parcel arrived smashed.')]),
    Msg('assistant', [Thinking('Check order status first'), Text('I am sorry to hear that. I have issued a full refund to your card.')]),
    Msg('user', [Text('Thanks, that solves it!')])]
for m in (jev, gem_dec, luna):
    r = await adecide(history, dict(resolved='Did the customer confirm their issue is resolved?'), model=m)
    print(f"{m:18} resolved={r.resolved.noul:.3f}")
```

    jev-1.14.0         resolved=0.980
    gemini-decision-1  resolved=0.981
    gpt-6-luna         resolved=1.000

## Vision and bounding boxes

Pass an `InputImage` directly as `state` across all three providers. On Jev, add a `box` question to locate normalized `[0, 1]` bounding coordinates (Gemini and OpenAI raise `NotImplementedError` for `box`):

``` python
from PIL import Image as PImg, ImageDraw
from fastcore.utils import img_bytes
from aidialog.msg_parts import InputImage, to_data_url
```

``` python
canvas = PImg.new('RGB', (320, 320), 'white')
ImageDraw.Draw(canvas).rectangle([80, 80, 240, 240], fill='red')
red_img = InputImage(to_data_url(img_bytes(canvas, fmt='PNG')), mime='image/png')

for m in (jev, gem_dec, luna):
    r = await adecide(red_img, dict(has_red='Does this image contain a red rectangle?',
        color=choice(['red', 'blue', 'green'], 'Main shape color?')), model=m, resolution='low')
    print(f"{m:18} has_red={r.has_red.noul:.2f}  color={r.color.choice}")

r_box = await adecide(red_img, dict(rect=box('Locate the red rectangle.')), model=jev)
r_box.rect
```

    jev-1.14.0         has_red=0.87  color=red
    gemini-decision-1  has_red=0.84  color=red
    gpt-6-luna         has_red=1.00  color=red

`BoxAnswer(top=0.17, left=0.23, bottom=0.90, right=0.75, confidence=0.730)`

## Provider capabilities

Provider-specific options pass through `adecide(..., **kwargs)` and are ignored by other adapters (for example `long_context=True` on Jev, `resolution='low'` on Gemini, and `prompt_cache_key` / `prompt_cache_options` on OpenAI). For synchronous code (scripts or notebooks), use the `decide(...)` wrapper.

| Capability | TypeSafe Jev (`jev-1.14.0`) | Google Gemini (`gemini-decision-1`) | OpenAI (`gpt-6-luna`) |
|----|----|----|----|
| `noul`, `choice`, `score` | Yes | Yes | Yes (`noul` criteria folded into `instructions`) |
| `box` bounding boxes | Yes | No (`NotImplementedError`) | No (`NotImplementedError`) |
| `Msg` chat history | JSON in `state` | Structured JSON + media | Native message + tool items |
| `InputImage` | Yes (PNG/JPEG, 280–1568 px) | Yes (plus `resolution`) | Yes (plus `detail`) |
| `InputAudio` / `InputVideo` / `InputFile` | No | Yes | No |
| Per-question refusals | Never | Whole-request block (`APIError`) | `RefusalAnswer` in `answers` |
