Skip to main content

Provider

Struct Provider 

Source
pub struct Provider { /* private fields */ }
Expand description

Cloudflare Workers AI provider implementing anyllm::ChatProvider.

Wraps a worker::Ai binding to call AI models directly from within a Cloudflare Worker. No HTTP overhead: calls go through the Workers runtime’s internal AI binding.

§Construction

Obtain the worker::Ai binding from the Worker’s Env:

let ai = env.ai("AI")?;
let provider = Provider::new(ai);

The "AI" string must match the binding name in wrangler.toml:

[ai]
binding = "AI"

Implementations§

Source§

impl Provider

Source

pub fn new(ai: Ai) -> Self

Create a new provider from a worker::Ai binding.

Source

pub fn with_chat_capabilities( self, resolver: impl ChatCapabilityResolver, ) -> Self

Install a resolver consulted before the provider’s built-in capability logic.

Trait Implementations§

Source§

impl ChatProvider for Provider

Source§

type Stream = Pin<Box<dyn Stream<Item = Result<StreamEvent, Error>> + Send>>

Concrete stream type returned by chat_stream
Source§

async fn chat(&self, request: &ChatRequest) -> Result<ChatResponse>

Send a chat completion request and return the full response. Read more
Source§

async fn chat_stream(&self, request: &ChatRequest) -> Result<ChatStream>

Send a chat completion request and return a stream of chunks. Read more
Source§

fn chat_capability( &self, model: &str, capability: ChatCapability, ) -> CapabilitySupport

Returns support information for a provider/model capability query.
Source§

impl EmbeddingProvider for Provider

Source§

async fn embed(&self, request: &EmbeddingRequest) -> Result<EmbeddingResponse>

Send an embedding request and return ordered vectors. Read more
Source§

fn embedding_capability( &self, _model: &str, capability: EmbeddingCapability, ) -> CapabilitySupport

Returns support information for a provider/model embedding capability.
Source§

impl ExtractExt for Provider

Available on crate feature extract only.
Source§

fn extract<'a, T>( &'a self, request: &'a ChatRequest, ) -> impl Future<Output = Result<Extracted<T>, Error>> + Send + 'a
where T: JsonSchema + DeserializeOwned + Send + 'a,

Extract structured data from a chat request. Read more
Source§

impl ProviderIdentity for Provider

Source§

fn provider_name(&self) -> &'static str

Returns the provider identity name.

Auto Trait Implementations§

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> ChatProviderExt for T
where T: ChatProvider,

Source§

fn ask( &self, model: &str, message: impl Into<String>, ) -> impl Future<Output = Result<String, Error>> + Send

Quick one-shot text question. Read more
Source§

fn ask_with_system( &self, model: &str, system: impl Into<String>, message: impl Into<String>, ) -> impl Future<Output = Result<String, Error>> + Send

Quick one-shot text question with a system message. Read more
Source§

impl<T> EmbeddingProviderExt for T

Source§

fn embed_text( &self, model: &str, input: impl Into<String>, ) -> impl Future<Output = Result<Vec<f32>, Error>> + Send

Quick one-shot embedding for a single input. 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, 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, 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