Fm::Session
A session that interacts with the language model.
A session maintains conversation context between requests, enabling multi-turn conversations. Create a new session for each conversation or reuse for multi-turn dialogue.
model = Fm::SystemLanguageModel.new
session = Fm::Session.new(model)
response = session.respond("Hello!")
puts response.content
Constructors
Creates a session from a transcript JSON string.
Optionally restores instructions, tools, and adapters so the resumed session behaves identically to the original.
json = session.transcript_json
restored = Fm::Session.from_transcript(model, json,
instructions: "Be helpful.",
tools: [my_tool],
adapters: [my_adapter],
)
Creates a new session with optional instructions, tools, and/or adapters.
session = Fm::Session.new(model)
session = Fm::Session.new(model, instructions: "Be helpful.")
session = Fm::Session.new(model, tools: [my_tool])
session = Fm::Session.new(model, adapters: [my_adapter])
session = Fm::Session.new(model, instructions: "Be helpful.", tools: [my_tool], adapters: [my_adapter])
Instance methods
Prewarms the model with an optional prompt prefix.
Sends a prompt and waits for the complete response.
response = session.respond("What is the capital of France?")
puts response.content
Sends a prompt and waits for the response, with a timeout.
Sends a prompt and returns a structured JSON response matching a schema.
schema = %({"type":"object","properties":{"name":{"type":"string"}},"required":["name"]})
json = session.respond_json("Generate a person", schema)
Sends a prompt and returns a deserialized structured response.
The type T must include JSON::Serializable and Fm::Generable.
struct Person
include JSON::Serializable
include Fm::Generable
getter name : String
getter age : Int32
end
person = session.respond_structured(Person, "Generate a fictional person")
puts person.name
Sends a prompt and streams the response via a block.
The block receives each text chunk as it arrives from the model. This method blocks until streaming is complete.
session.stream("Tell me a story") do |chunk|
print chunk
end