Skip to main content

AsyncSession

Struct AsyncSession 

Source
pub struct AsyncSession<'s> { /* private fields */ }
Available on crate feature async only.
Expand description

Async wrapper around crate::LanguageModelSession.

Borrows the session for its lifetime; the session itself must outlive all in-flight futures.

§Examples

use foundation_models::{LanguageModelSession, SystemLanguageModel};
use foundation_models::async_api::AsyncSession;

if !SystemLanguageModel::is_available() { return Ok(()); }
pollster::block_on(async {
    let session = LanguageModelSession::new();
    let reply = AsyncSession::new(&session).respond("Hi!")?.await?;
    println!("{}", reply.content);
    Ok::<(), Box<dyn std::error::Error>>(())
})

Implementations§

Source§

impl<'s> AsyncSession<'s>

Source

pub fn new(session: &'s LanguageModelSession) -> Self

Wrap a crate::LanguageModelSession for async use.

Source

pub fn respond(&self, prompt: impl ToPrompt) -> Result<RespondFuture, FMError>

Async version of LanguageModelSession.respond(to:).

Corresponds to the Swift async throws method LanguageModelSession.respond(to:).

§Errors

Returns an FMError if the model is unavailable or generation fails.

Source

pub fn respond_with_options( &self, prompt: impl ToPrompt, options: GenerationOptions, ) -> Result<RespondFuture, FMError>

Async version of LanguageModelSession.respond(to:) with GenerationOptions.

§Errors

Returns an FMError if the model is unavailable or generation fails.

Source

pub fn respond_generating( &self, prompt: impl ToPrompt, schema: &GenerationSchema, include_schema_in_prompt: bool, options: GenerationOptions, ) -> Result<RespondGeneratingFuture, FMError>

Async version of LanguageModelSession.respond(to:generating:).

Generates a structured GeneratedContent response according to schema. Corresponds to the Swift async throws method LanguageModelSession.respond(to:generating:).

§Errors

Returns an FMError if the model is unavailable or generation fails.

Trait Implementations§

Source§

impl Debug for AsyncSession<'_>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'s> Freeze for AsyncSession<'s>

§

impl<'s> !RefUnwindSafe for AsyncSession<'s>

§

impl<'s> Send for AsyncSession<'s>

§

impl<'s> Sync for AsyncSession<'s>

§

impl<'s> Unpin for AsyncSession<'s>

§

impl<'s> UnsafeUnpin for AsyncSession<'s>

§

impl<'s> !UnwindSafe for AsyncSession<'s>

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, 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.