Contents

LanguageModelSession

An object that represents a session that interacts with a language model.

Declaration

final class LanguageModelSession

Mentioned in

Overview

A session is a single context that you use to generate content with, and maintains state between requests. You can reuse the existing instance or create a new one each time you call the model. When you create a session you can provide instructions that tells the model what its role is and provides guidance on how to respond.

let session = LanguageModelSession(instructions: """
    You are a motivational workout coach that provides quotes to inspire \
    and motivate athletes.
    """
)
let prompt = "Generate a motivational quote for my next workout."
let response = try await session.respond(to: prompt)

The framework records each call to the model in a Transcript that includes all prompts and responses. If your session exceeds the available context size, it throws LanguageModelError.contextSizeExceeded(_:). For more information on managing the context window size, see Managing the context window.

Use Instruments to analyze token consumption while your app is running and to look for opportunities to improve performance, like with prewarm(promptPrefix:). For more information on Instruments, see Analyzing the runtime performance of your Foundation Models app.

Topics

Creating a session

Creating a session with a dynamic profile

Preloading the model

Inspecting a session

Generating a response

Generating a response with metadata

Streaming a response

Streaming a response with metadata

Accessing the transcript

Generating feedback

Session properties

Errors

See Also

Sessions and prompts