pub struct LanguageModelSession { /* private fields */ }Expand description
A stateful conversation with the on-device language model.
Sessions retain their conversation history; subsequent calls to
respond build on the previous turns.
§Examples
use foundation_models::LanguageModelSession;
let session = LanguageModelSession::new();
let answer = session.respond("Name three Norse gods.")?;
println!("{answer}");Implementations§
Source§impl LanguageModelSession
impl LanguageModelSession
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a session with the model’s default behaviour.
§Panics
Panics if FoundationModels is not available on this OS. Check
crate::SystemLanguageModel::is_available first if you need to
handle that gracefully.
Sourcepub fn with_instructions(instructions: &str) -> Self
pub fn with_instructions(instructions: &str) -> Self
Create a session with custom system instructions (“system prompt”).
§Panics
Panics if FoundationModels is not available, or if instructions
contains an interior NUL byte.
Sourcepub fn try_new(instructions: Option<&str>) -> Option<Self>
pub fn try_new(instructions: Option<&str>) -> Option<Self>
Fallible constructor. Returns None when FoundationModels is not
available (OS too old, model not enabled, etc.) or when instructions
contains an interior NUL byte.
Sourcepub fn respond_with(
&self,
prompt: &str,
options: GenerationOptions,
) -> Result<String, FMError>
pub fn respond_with( &self, prompt: &str, options: GenerationOptions, ) -> Result<String, FMError>
Sourcepub fn stream_with<F>(
&self,
prompt: &str,
options: GenerationOptions,
on_chunk: F,
) -> Result<(), FMError>
pub fn stream_with<F>( &self, prompt: &str, options: GenerationOptions, on_chunk: F, ) -> Result<(), FMError>
Trait Implementations§
Source§impl Debug for LanguageModelSession
impl Debug for LanguageModelSession
Source§impl Default for LanguageModelSession
impl Default for LanguageModelSession
Source§impl Drop for LanguageModelSession
impl Drop for LanguageModelSession
impl Send for LanguageModelSession
impl Sync for LanguageModelSession
Auto Trait Implementations§
impl Freeze for LanguageModelSession
impl RefUnwindSafe for LanguageModelSession
impl Unpin for LanguageModelSession
impl UnsafeUnpin for LanguageModelSession
impl UnwindSafe for LanguageModelSession
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more