Skip to main content

Ai

Struct Ai 

Source
pub struct Ai(/* private fields */);
Expand description

Enables access to Workers AI functionality.

Implementations§

Source§

impl Ai

Source

pub async fn run<T: Serialize, U: DeserializeOwned>( &self, model: impl AsRef<str>, input: T, ) -> Result<U>

Execute a Workers AI operation using the specified model. Various forms of the input are documented in the Workers AI documentation.

Source

pub async fn run_bytes<T: Serialize>( &self, model: impl AsRef<str>, input: T, ) -> Result<ByteStream>

Execute a Workers AI operation that returns binary data as a ByteStream.

This method is designed for AI models that return raw bytes, such as:

  • Image generation models (e.g., Stable Diffusion)
  • Text-to-speech models
  • Any other model that returns binary output

The returned ByteStream implements Stream and can be:

  • Streamed directly to a [Response] using [Response::from_stream]
  • Collected into a Vec<u8> by iterating over the chunks
§Examples

This approach is more memory-efficient as it doesn’t buffer the entire response in memory:

use worker::*;
use serde::Serialize;

#[derive(Serialize)]
struct ImageGenRequest {
    prompt: String,
}

async fn generate_image(env: &Env) -> Result<Response> {
    let ai = env.ai("AI")?;
    let request = ImageGenRequest {
        prompt: "a beautiful sunset".to_string(),
    };
    let stream = ai.run_bytes(
        "@cf/stabilityai/stable-diffusion-xl-base-1.0",
        &request
    ).await?;

    // Stream directly to the response
    let mut response = Response::from_stream(stream)?;
    response.headers_mut().set("Content-Type", "image/png")?;
    Ok(response)
}
§Collecting into bytes

Use this approach if you need to inspect or modify the bytes before sending:

use worker::*;
use serde::Serialize;
use futures_util::StreamExt;

#[derive(Serialize)]
struct ImageGenRequest {
    prompt: String,
}

async fn generate_image(env: &Env) -> Result<Response> {
    let ai = env.ai("AI")?;
    let request = ImageGenRequest {
        prompt: "a beautiful sunset".to_string(),
    };
    let mut stream = ai.run_bytes(
        "@cf/stabilityai/stable-diffusion-xl-base-1.0",
        &request
    ).await?;

    // Collect all chunks into a Vec<u8>
    let mut bytes = Vec::new();
    while let Some(chunk) = stream.next().await {
        bytes.extend_from_slice(&chunk?);
    }

    let mut response = Response::from_bytes(bytes)?;
    response.headers_mut().set("Content-Type", "image/png")?;
    Ok(response)
}

Trait Implementations§

Source§

impl AsRef<JsValue> for Ai

Source§

fn as_ref(&self) -> &JsValue

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl Debug for Ai

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl EnvBinding for Ai

Source§

const TYPE_NAME: &'static str = "Ai"

Source§

fn get(val: JsValue) -> Result<Self>

Source§

impl From<Ai> for Ai

Source§

fn from(inner: AiSys) -> Self

Converts to this type from the input type.
Source§

impl From<Ai> for JsValue

Source§

fn from(database: Ai) -> Self

Converts to this type from the input type.
Source§

impl JsCast for Ai

Source§

fn instanceof(val: &JsValue) -> bool

Performs a dynamic instanceof check to see whether the JsValue provided is an instance of this type. Read more
Source§

fn unchecked_from_js(val: JsValue) -> Self

Performs a zero-cost unchecked conversion from a JsValue into an instance of Self Read more
Source§

fn unchecked_from_js_ref(val: &JsValue) -> &Self

Performs a zero-cost unchecked conversion from a &JsValue into an instance of &Self. Read more
Source§

fn has_type<T>(&self) -> bool
where T: JsCast,

Test whether this JS value has a type T. Read more
Source§

fn dyn_into<T>(self) -> Result<T, Self>
where T: JsCast,

Performs a dynamic cast (checked at runtime) of this value into the target type T. Read more
Source§

fn dyn_ref<T>(&self) -> Option<&T>
where T: JsCast,

Performs a dynamic cast (checked at runtime) of this value into the target type T. Read more
Source§

fn unchecked_into<T>(self) -> T
where T: JsCast,

Performs a zero-cost unchecked cast into the specified type. Read more
Source§

fn unchecked_ref<T>(&self) -> &T
where T: JsCast,

Performs a zero-cost unchecked cast into a reference to the specified type. Read more
Source§

fn is_instance_of<T>(&self) -> bool
where T: JsCast,

Test whether this JS value is an instance of the type T. Read more
Source§

fn is_type_of(val: &JsValue) -> bool

Performs a dynamic check to see whether the JsValue provided is a value of this type. Read more
Source§

impl Send for Ai

Source§

impl Sync for Ai

Auto Trait Implementations§

§

impl Freeze for Ai

§

impl RefUnwindSafe for Ai

§

impl Unpin for Ai

§

impl UnsafeUnpin for Ai

§

impl UnwindSafe for Ai

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T> TryFromJsValue for T
where T: JsCast,

Source§

fn try_from_js_value(val: JsValue) -> Result<T, JsValue>

Performs the conversion.
Source§

fn try_from_js_value_ref(val: &JsValue) -> Option<T>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<S, T> Upcast<T> for S
where T: UpcastFrom<S> + ?Sized, S: ?Sized,

Source§

fn upcast(&self) -> &T
where Self: ErasableGeneric, T: ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider ref type within the Wasm bindgen generics type system. Read more
Source§

fn upcast_into(self) -> T
where Self: Sized + ErasableGeneric, T: ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider type within the Wasm bindgen generics type system. Read more
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V