GitHub

decision_model

API reference for decision_model.

Cloudflare's Clef as a DecisionModel (Effect's CloudflareDecisionModel)::

program.pipe(fx.provide(decision_model.model("clef-flash").layer)).pipe(
    fx.provide(client.layer_config())
)

Each decision is sent as a named question (choice, score with the levels' descriptions, noul); a score's probabilities come back by level index and are keyed by the levels' labels. Images are sent as base64 data with their media type, or as data: URLs; a remote URL fails with InvalidUserInputError (Clef does not fetch it). Probabilities are rounded to four decimals, so their drift is accepted.

Types

TypeDefinitionDescription
Modeltype Model = Literal['clef', 'clef-flash']

Model names (any other string is accepted too).

Functions

questions

questions(decisions) -> dict[str, W.Question]

The request's questions, by decision name.

ParameterTypeDescription
decisionsMapping[str, D.NonLocateDecision]

Returns dict[str, W.Question]

def questions(decisions: Mapping[str, D.NonLocateDecision]) -> dict[str, W.Question]:
    """The request's questions, by decision name."""
    return {key: _question(decision) for key, decision in decisions.items()}

make

make(*, model) -> Effect[DM.Service, Never, CloudflareClient]

A DecisionModel asking model through the CloudflareClient.

ParameterTypeDescription
modelModel | str

Returns Effect[DM.Service, Never, CloudflareClient]

def make(*, model: Model | str) -> Effect[DM.Service, Never, CloudflareClient]:
    """A ``DecisionModel`` asking ``model`` through the ``CloudflareClient``."""

    def build(client: ClientService) -> Effect[DM.Service]:
        def decide(
            options: DM.ProviderOptions[D.NonLocateDecision],
        ) -> Effect[DM.ProviderResponse, E.AiError]:
            images = options.images
            if images is not None and any(
                isinstance(i.image.data, Url) and i.image.data.protocol != "data:" for i in images
            ):
                return fx.fail(_remote_url_error())
            request: W.DecisionsRequest = {
                "model": model,
                "state": options.state if images is None else _label_images(options.state, images),
                "questions": questions(options.decisions),
            }
            if images is not None:
                request["images"] = [_image(image.image) for image in images]
            return client.create_decisions(request).map(
                lambda response: DM.ProviderResponse(
                    N.provider_answers(options.decisions, response["answers"]),
                    response["usage"]["input_tokens"],
                    response["usage"]["output_tokens"],
                )
            )

        return DM.make(decide=decide, probability_precision=4, supports_images=True)

    return CloudflareClient().flat_map(build)

layer

layer(*, model) -> L.Layer[DM.DecisionModel, Never, CloudflareClient]

DecisionModel over the CloudflareClient.

ParameterTypeDescription
modelModel | str

Returns L.Layer[DM.DecisionModel, Never, CloudflareClient]

def layer(*, model: Model | str) -> L.Layer[DM.DecisionModel, Never, CloudflareClient]:
    """``DecisionModel`` over the ``CloudflareClient``."""
    return L.effect(DM.DecisionModel, make(model=model))

model

model(model) -> M.Model[DM.DecisionModel, CloudflareClient]

The decision model as a Model (provider "cloudflare").

ParameterTypeDescription
modelModel | str

Returns M.Model[DM.DecisionModel, CloudflareClient]

def model(model: Model | str) -> M.Model[DM.DecisionModel, CloudflareClient]:
    """The decision model as a ``Model`` (provider ``"cloudflare"``)."""
    return M.make("cloudflare", model, layer(model=model))