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 type | Apple API | Notes |
|---|---|---|
AsyncSession::respond | LanguageModelSession.respond(to:) | Returns SessionResponse<String> |
AsyncSession::respond_generating | LanguageModelSession.respond(to:generating:) | Returns SessionResponse<GeneratedContent> |
AsyncAdapter::from_name | SystemLanguageModel.Adapter init(name:) | Returns Adapter |
AsyncAdapter::compatibility | SystemLanguageModel.Adapter.compatibility(for:) | Returns Vec<String> |
AsyncAdapter::compile | SystemLanguageModel.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§
- Adapter
Compatibility Future - Future returned by
AsyncAdapter::compatibility. - Adapter
Init Future - Future returned by
AsyncAdapter::from_name. - Async
Adapter - Namespace for async
Adapteroperations. - Async
Session - Async wrapper around
crate::LanguageModelSession. - Compile
Adapter Future - Future returned by
AsyncAdapter::compile. - Respond
Future - Future returned by
AsyncSession::respond. - Respond
Generating Future - Future returned by
AsyncSession::respond_generating.