Session

Struct Session 

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

A session that interacts with a language model.

A session maintains state between requests, allowing for multi-turn conversations. You can reuse the same session for multiple prompts or create a new one each time.

§Example

use fm_rs::{Session, SystemLanguageModel, GenerationOptions};

let model = SystemLanguageModel::new()?;
let session = Session::new(&model)?;

let response = session.respond("Hello!", &GenerationOptions::default())?;
println!("{}", response.content());

Implementations§

Source§

impl Session

Source

pub fn new(model: &SystemLanguageModel) -> Result<Self>

Creates a new session with the given model.

Source

pub fn with_instructions( model: &SystemLanguageModel, instructions: &str, ) -> Result<Self>

Creates a new session with instructions.

Instructions define the model’s behavior and role.

Source

pub fn with_tools( model: &SystemLanguageModel, tools: &[Arc<dyn Tool>], ) -> Result<Self>

Creates a new session with tools.

Tools allow the model to call external functions during generation.

Source

pub fn with_instructions_and_tools( model: &SystemLanguageModel, instructions: &str, tools: &[Arc<dyn Tool>], ) -> Result<Self>

Creates a new session with both instructions and tools.

Source

pub fn from_transcript( model: &SystemLanguageModel, transcript_json: &str, ) -> Result<Self>

Creates a session from a transcript JSON string.

This allows restoring a previous conversation. Note: Restored sessions do not have tools - use with_tools for new sessions.

Source

pub fn respond( &self, prompt: &str, options: &GenerationOptions, ) -> Result<Response>

Sends a prompt and waits for the complete response.

This method blocks until the model finishes generating.

Source

pub fn respond_with_timeout( &self, prompt: &str, options: &GenerationOptions, timeout: Duration, ) -> Result<Response>

Sends a prompt and waits for the complete response, with a timeout.

If timeout is zero, this behaves like respond.

Source

pub fn stream_response<F>( &self, prompt: &str, options: &GenerationOptions, on_chunk: F, ) -> Result<()>
where F: FnMut(&str) + Send + 'static,

Sends a prompt and streams the response.

The on_chunk callback is called for each text chunk as it arrives. This method blocks until streaming is complete.

§Example
use fm_rs::{Session, SystemLanguageModel, GenerationOptions};

let model = SystemLanguageModel::new()?;
let session = Session::new(&model)?;

session.stream_response("Tell me a story", &GenerationOptions::default(), |chunk| {
    print!("{}", chunk);
})?;
Source

pub fn cancel(&self)

Cancels an ongoing stream operation.

Source

pub fn is_responding(&self) -> bool

Checks if the session is currently generating a response.

Source

pub fn transcript_json(&self) -> Result<String>

Gets the session transcript as a JSON string.

This can be used to persist and restore conversations.

Source

pub fn context_usage(&self, limit: &ContextLimit) -> Result<ContextUsage>

Estimates current context usage based on the session transcript.

Source

pub fn ensure_context_within(&self, limit: &ContextLimit) -> Result<()>

Returns an error if the estimated context usage exceeds the configured limit.

Source

pub fn prewarm(&self, prompt_prefix: Option<&str>) -> Result<()>

Prewarms the model with an optional prompt prefix.

This can reduce latency for the first response.

Source

pub fn respond_json( &self, prompt: &str, schema: &Value, options: &GenerationOptions, ) -> Result<String>

Sends a prompt and returns a structured JSON response.

The schema is a JSON Schema that describes the expected output format. The model is instructed to produce JSON that matches the schema.

§Example
use fm_rs::{Session, SystemLanguageModel, GenerationOptions};
use serde::Deserialize;
use serde_json::json;

#[derive(Deserialize)]
struct Person {
    name: String,
    age: u32,
}

let model = SystemLanguageModel::new()?;
let session = Session::new(&model)?;

let schema = json!({
    "type": "object",
    "properties": {
        "name": { "type": "string" },
        "age": { "type": "integer" }
    },
    "required": ["name", "age"]
});

let json_str = session.respond_json(
    "Generate a fictional person",
    &schema,
    &GenerationOptions::default()
)?;

let person: Person = serde_json::from_str(&json_str)?;
Source

pub fn respond_structured<T: DeserializeOwned>( &self, prompt: &str, schema: &Value, options: &GenerationOptions, ) -> Result<T>

Sends a prompt and returns a deserialized structured response.

This is a convenience method that calls respond_json and deserializes the result into the specified type.

§Example
use fm_rs::{Session, SystemLanguageModel, GenerationOptions};
use serde::Deserialize;
use serde_json::json;

#[derive(Deserialize)]
struct Person {
    name: String,
    age: u32,
}

let model = SystemLanguageModel::new()?;
let session = Session::new(&model)?;

let schema = json!({
    "type": "object",
    "properties": {
        "name": { "type": "string" },
        "age": { "type": "integer" }
    },
    "required": ["name", "age"]
});

let person: Person = session.respond_structured(
    "Generate a fictional person",
    &schema,
    &GenerationOptions::default()
)?;
Source

pub fn respond_structured_gen<T>( &self, prompt: &str, options: &GenerationOptions, ) -> Result<T>

Sends a prompt and returns a deserialized structured response using a derived schema.

This uses the crate::Generable implementation to obtain the JSON schema.

Source

pub fn stream_json<F>( &self, prompt: &str, schema: &Value, options: &GenerationOptions, on_chunk: F, ) -> Result<()>
where F: FnMut(&str) + Send + 'static,

Streams a structured JSON response.

The on_chunk callback receives partial JSON as it’s generated. Note that partial chunks may not be valid JSON until streaming completes.

§Example
use fm_rs::{Session, SystemLanguageModel, GenerationOptions};
use serde_json::json;

let model = SystemLanguageModel::new()?;
let session = Session::new(&model)?;

let schema = json!({
    "type": "object",
    "properties": {
        "items": { "type": "array", "items": { "type": "string" } }
    }
});

session.stream_json(
    "List 5 programming languages",
    &schema,
    &GenerationOptions::default(),
    |chunk| {
        print!("{chunk}");
    }
)?;

Trait Implementations§

Source§

impl Drop for Session

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl Send for Session

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