Cloudflare - v1.3.0
    Preparing search index...

    Class AIBinding<E>

    Accessor for the Workers AI binding (ai in wrangler.json).

    Which model it runs, which gateway it routes through, and which binding it reads are all decided by the configuration layer — this class receives the settled values and has no idea whether they came from the defaults or from config/AIConfig.ts.

    const ai = new AIBinding() // settings resolved from the configuration layer
    const result = await ai.run(ctx.env, { prompt: 'ping' })

    Bayu Dwiyan Satria

    1.0.0

    1.0.0

    Type Parameters

    Hierarchy (View Summary)

    Index
    gateway?: string

    AI Gateway id used to route inference, when configured.

    model: string

    Model used when a call does not name one.

    settings: () => AISettings

    This accessor’s settings, resolved on first read.

    • get name(): string

      The binding name this instance resolves.

      Returns string

      The binding name as declared in wrangler.json.

    • Returns an AI Gateway handle for lower-level gateway operations.

      Parameters

      • env: E

        The Worker environment.

      • OptionalgatewayId: string

        Gateway id. Falls back to the configured gateway.

      Returns AiGateway

      The AI Gateway instance.

      Error When no gateway id is supplied and none is configured.

    • Checks whether the binding is available on the given environment.

      Use this to degrade gracefully when a binding is optional.

      Parameters

      • env: E

        The Worker environment.

      Returns boolean

      true when the binding is present.

    • Returns the AI Gateway log id of the most recent inference call.

      Useful for correlating a response with its gateway log entry.

      Parameters

      • env: E

        The Worker environment.

      Returns string

      The log id, or null when the last call was not routed through a gateway.

    • Runs an inference request against a Workers AI model.

      Type Parameters

      • TResult = any

      Parameters

      • env: E

        The Worker environment.

      • inputs: any

        Model inputs, shaped by the model being called.

      • Optionalmodel: string

        Model to run. Falls back to the configured model.

      Returns Promise<TResult>

      The model output.

      MissingBindingError When the AI binding is absent.

    • Resolves the binding without throwing.

      Parameters

      • env: E

        The Worker environment.

      Returns Ai<AiModels>

      The resolved binding, or null when it is not configured.