Skip to main content

LanguageModelSession

Struct LanguageModelSession 

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

A stateful conversation session backed by a LanguageModelSession.

The session automatically maintains a conversation transcript, so each successive call to respond_to has access to the full prior context (subject to the 4 096-token context window limit).

§Thread safety

LanguageModelSession is Send + Sync. Concurrent calls are forwarded to the underlying Swift session, which handles them via its internal async actor. Note however that concurrent calls will interleave entries in the transcript in an unspecified order; for predictable multi-turn behaviour call sequentially.

§Drop behaviour

Dropping a LanguageModelSession releases the caller’s session reference. In-flight generation futures and active ResponseStreams hold their own cloned handle reference until Swift invokes the completion callback, so cancellation cannot release the Swift session while the bridge still needs it.

Implementations§

Source§

impl LanguageModelSession

Source

pub fn builder() -> LanguageModelSessionBuilder

Starts building a session.

Source

pub fn new() -> Result<Self, Error>

Creates a new session with no system instructions.

§Errors

Returns Error::Unavailable when Apple Intelligence is unavailable.

Source

pub fn with_instructions<I>(instructions: I) -> Result<Self, Error>

Creates a new session with the given system instructions.

SystemInstructions act as a persistent system prompt that guides all subsequent responses in this session. They must come from developer code, never from user input, to prevent prompt-injection attacks.

§Errors

Returns Error::NullByte if the instructions cannot cross the C FFI boundary, or Error::Unavailable when Apple Intelligence is unavailable.

Source

pub fn with_tools<I>( instructions: I, tools: Vec<ToolDefinition>, ) -> Result<Self, Error>

Creates a session pre-loaded with the given tools.

The model will use these tools automatically when appropriate during respond calls. Tool names must be unique within the session.

§Errors

Returns Error::NullByte if the instructions or serialized tool metadata cannot cross the C FFI boundary, Error::Json if tool metadata serialization fails, or Error::Unavailable when Apple Intelligence is unavailable.

Source

pub async fn respond<P>(&self, prompt: P) -> Result<String, Error>
where P: TryInto<Prompt>, P::Error: Into<Error>,

Sends a prompt and returns the full response text.

The response is appended to this session’s transcript, so subsequent calls have access to prior context.

§Errors

Returns Error::NullByte for invalid prompt text, or Error::Generation when the model or bridge fails.

Source

pub async fn respond_to<P>(&self, prompt: P) -> Result<ResponseText, Error>
where P: TryInto<Prompt>, P::Error: Into<Error>,

Apple-style typed response method matching Swift’s respond(to:).

§Errors

Returns the same error variants as LanguageModelSession::respond.

Source

pub async fn complete<P>(&self, prompt: P) -> Result<ResponseText, Error>
where P: TryInto<Prompt>, P::Error: Into<Error>,

Sends a prompt and returns typed response text.

§Errors

Returns the same error variants as LanguageModelSession::respond_to.

Source

pub async fn generate<P>(&self, prompt: P) -> Result<GeneratedText, Error>
where P: TryInto<Prompt>, P::Error: Into<Error>,

MLX-style typed response alias.

§Errors

Returns the same error variants as LanguageModelSession::respond_to.

Source

pub async fn generate_text<P>(&self, prompt: P) -> Result<ResponseText, Error>
where P: TryInto<Prompt>, P::Error: Into<Error>,

Generates typed response text and appends the exchange to this session’s transcript.

§Errors

Returns the same error variants as LanguageModelSession::respond_to.

Source

pub async fn respond_with_options<P>( &self, prompt: P, options: &GenerationOptions, ) -> Result<String, Error>
where P: TryInto<Prompt>, P::Error: Into<Error>,

Like respond but allows tuning generation.

§Errors

Returns every error documented by LanguageModelSession::respond. It can also return Error::InvalidTemperature or Error::InvalidMaxTokens when options contains out-of-range values.

Source

pub async fn respond_to_with_options<P>( &self, prompt: P, options: &GenerationOptions, ) -> Result<ResponseText, Error>
where P: TryInto<Prompt>, P::Error: Into<Error>,

Apple-style typed response method with explicit generation options.

§Errors

Returns every error documented by LanguageModelSession::respond. It can also return Error::InvalidTemperature or Error::InvalidMaxTokens when options contains out-of-range values.

Source

pub async fn complete_with_options<P>( &self, prompt: P, options: &GenerationOptions, ) -> Result<ResponseText, Error>
where P: TryInto<Prompt>, P::Error: Into<Error>,

Like complete but allows tuning generation.

§Errors

Returns the same error variants as LanguageModelSession::respond_with_options.

Source

pub async fn generate_with_options<P>( &self, prompt: P, options: &GenerationOptions, ) -> Result<GeneratedText, Error>
where P: TryInto<Prompt>, P::Error: Into<Error>,

MLX-style typed response alias with explicit generation options.

§Errors

Returns the same error variants as LanguageModelSession::respond_to_with_options.

Source

pub async fn generate_text_with_options<P>( &self, prompt: P, options: &GenerationOptions, ) -> Result<ResponseText, Error>
where P: TryInto<Prompt>, P::Error: Into<Error>,

Generates typed response text with explicit generation options.

§Errors

Returns the same error variants as LanguageModelSession::respond_with_options.

Source

pub async fn respond_as<T, P>( &self, prompt: P, schema: &GenerationSchema, ) -> Result<T, Error>

Sends a prompt and deserialises the response into T using the provided schema.

The model generates output conforming to schema and this method deserialises it. Derive serde::Deserialize on T and ensure the field names match the schema property names exactly.

§Errors

Returns Error::NullByte for invalid prompt text, Error::Json if the schema or model response cannot be serialized or deserialized, or Error::Generation when the model or bridge fails.

Source

pub async fn respond_generating<T, P>( &self, prompt: P, schema: &GenerationSchema, ) -> Result<T, Error>

Apple-style guided-generation method for a dynamic GenerationSchema.

This mirrors Swift’s respond(to:generating:) terminology while deserializing the generated JSON into T.

§Errors

Returns every error documented by LanguageModelSession::respond_as.

Source

pub async fn generate_object<T, P>( &self, prompt: P, schema: &GenerationSchema, ) -> Result<T, Error>

Generates structured output and deserialises it into T.

§Errors

Returns the same error variants as LanguageModelSession::respond_as.

Source

pub async fn respond_as_with_options<T, P>( &self, prompt: P, schema: &GenerationSchema, options: &GenerationOptions, ) -> Result<T, Error>

Like respond_as but allows tuning generation.

§Errors

Returns every error documented by LanguageModelSession::respond_as. It can also return Error::InvalidTemperature or Error::InvalidMaxTokens when options contains out-of-range values.

Source

pub async fn respond_generating_with_options<T, P>( &self, prompt: P, schema: &GenerationSchema, options: &GenerationOptions, ) -> Result<T, Error>

Apple-style guided-generation method with explicit generation options.

§Errors

Returns every error documented by LanguageModelSession::respond_as_with_options.

Source

pub async fn generate_object_with_options<T, P>( &self, prompt: P, schema: &GenerationSchema, options: &GenerationOptions, ) -> Result<T, Error>

Generates structured output with explicit generation options.

§Errors

Returns the same error variants as LanguageModelSession::respond_as_with_options.

Source

pub fn stream<P>(&self, prompt: P) -> Result<ResponseStream, Error>
where P: TryInto<Prompt>, P::Error: Into<Error>,

Returns a ResponseStream that yields text chunks as the model generates them.

Each yielded chunk is an incremental snapshot of the response text. Drive the stream with your preferred async executor.

use aimx::LanguageModelSession;

let session = LanguageModelSession::new()?;
let stream = session.stream_response("Count to ten.")?;
§Errors

Returns Error::NullByte for invalid prompt text, or Error::Generation if stream startup fails. Individual stream items can also yield Error::Generation after the stream has been created.

Source

pub fn stream_response<P>(&self, prompt: P) -> Result<ResponseStream, Error>
where P: TryInto<Prompt>, P::Error: Into<Error>,

Apple-style streaming method matching Swift’s streamResponse(to:).

§Errors

Returns the same error variants as LanguageModelSession::stream.

Source

pub fn stream_generate<P>(&self, prompt: P) -> Result<ResponseStream, Error>
where P: TryInto<Prompt>, P::Error: Into<Error>,

MLX-style streaming alias matching stream_generate.

§Errors

Returns the same error variants as LanguageModelSession::stream_response.

Source

pub fn stream_text<P>(&self, prompt: P) -> Result<ResponseStream, Error>
where P: TryInto<Prompt>, P::Error: Into<Error>,

Streams response text with this session’s default generation options.

§Errors

Returns the same error variants as LanguageModelSession::stream_response.

Source

pub fn stream_with_options<P>( &self, prompt: P, options: &GenerationOptions, ) -> Result<ResponseStream, Error>
where P: TryInto<Prompt>, P::Error: Into<Error>,

Like stream but allows tuning generation.

§Errors

Returns every error documented by LanguageModelSession::stream. It can also return Error::InvalidTemperature or Error::InvalidMaxTokens when options contains out-of-range values.

Source

pub fn stream_response_with_options<P>( &self, prompt: P, options: &GenerationOptions, ) -> Result<ResponseStream, Error>
where P: TryInto<Prompt>, P::Error: Into<Error>,

Apple-style streaming method with explicit generation options.

§Errors

Returns every error documented by LanguageModelSession::stream. It can also return Error::InvalidTemperature or Error::InvalidMaxTokens when options contains out-of-range values.

Source

pub fn stream_generate_with_options<P>( &self, prompt: P, options: &GenerationOptions, ) -> Result<ResponseStream, Error>
where P: TryInto<Prompt>, P::Error: Into<Error>,

MLX-style streaming alias with explicit generation options.

§Errors

Returns every error documented by LanguageModelSession::stream_response_with_options.

Source

pub fn stream_text_with_options<P>( &self, prompt: P, options: &GenerationOptions, ) -> Result<ResponseStream, Error>
where P: TryInto<Prompt>, P::Error: Into<Error>,

Streams response text with explicit generation options.

§Errors

Returns the same error variants as LanguageModelSession::stream_with_options.

Trait Implementations§

Source§

impl Debug for LanguageModelSession

Source§

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

Formats the value using the given formatter. Read more
Source§

impl LanguageModel for LanguageModelSession

Source§

fn generate_text_with_options<P>( &self, prompt: P, options: GenerationOptions, ) -> impl Future<Output = Result<ResponseText, Error>> + '_
where P: TryInto<Prompt>, P::Error: Into<Error>,

Generates response text for a prompt with explicit generation options. Read more
Source§

fn stream_text_with_options<P>( &self, prompt: P, options: GenerationOptions, ) -> Result<ResponseStream, Error>
where P: TryInto<Prompt>, P::Error: Into<Error>,

Streams response text for a prompt with explicit generation options. Read more

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> CompletionModel for T
where T: LanguageModel,

Source§

fn completion<P>( &self, prompt: P, options: GenerationOptions, ) -> impl Future<Output = Result<ResponseText, Error>> + '_
where P: TryInto<Prompt>, P::Error: Into<Error>,

Generates response text for a prompt with explicit generation options. Read more
Source§

fn stream_completion<P>( &self, prompt: P, options: GenerationOptions, ) -> Result<ResponseStream, Error>
where P: TryInto<Prompt>, P::Error: Into<Error>,

Streams response text for a prompt with explicit generation options. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> GenerateText for T
where T: LanguageModel,

Source§

fn prompt<P>( &self, prompt: P, ) -> impl Future<Output = Result<ResponseText, Error>> + '_
where P: TryInto<Prompt>, P::Error: Into<Error>,

Sends a prompt with default generation options. Read more
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<T> WasmCompatSend for T
where T: Send,

Source§

impl<T> WasmCompatSync for T
where T: Sync,