Skip to main content

Module async_api

Module async_api 

Source
Available on crate feature async only.
Expand description

Executor-agnostic async API for FoundationModels (Tier 1).

Enabled with the async Cargo feature. Works with any async runtime (Tokio, async-std, smol, pollster, …) because it uses only std types internally.

§Wrapped Apple APIs

Rust typeApple APINotes
AsyncSession::respondLanguageModelSession.respond(to:)Returns SessionResponse<String>
AsyncSession::respond_generatingLanguageModelSession.respond(to:generating:)Returns SessionResponse<GeneratedContent>
AsyncAdapter::from_nameSystemLanguageModel.Adapter init(name:)Returns Adapter
AsyncAdapter::compatibilitySystemLanguageModel.Adapter.compatibility(for:)Returns Vec<String>
AsyncAdapter::compileSystemLanguageModel.Adapter.compile()Returns ()

§Tier 2 note

LanguageModelSession.streamResponse(to:) is an AsyncSequence — a multi-fire stream, not a one-shot future. It is deferred to Tier 2 (stream pattern). Use crate::LanguageModelSession::stream for synchronous streaming in the meantime.

§Example

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

if !SystemLanguageModel::is_available() {
    eprintln!("SKIP: FoundationModels unavailable");
    return Ok(());
}
pollster::block_on(async {
    let session = LanguageModelSession::new();
    let async_session = AsyncSession::new(&session);
    let reply = async_session.respond("Name three Norse gods.")?.await?;
    println!("{}", reply.content);
    Ok::<(), Box<dyn std::error::Error>>(())
})

Structs§

AdapterCompatibilityFuture
Future returned by AsyncAdapter::compatibility.
AdapterInitFuture
Future returned by AsyncAdapter::from_name.
AsyncAdapter
Namespace for async Adapter operations.
AsyncSession
Async wrapper around crate::LanguageModelSession.
CompileAdapterFuture
Future returned by AsyncAdapter::compile.
RespondFuture
Future returned by AsyncSession::respond.
RespondGeneratingFuture
Future returned by AsyncSession::respond_generating.