LanguageModelSession
An object that represents a session that interacts with a language model.
Declaration
final class LanguageModelSessionMentioned in
- Composing dynamic sessions with instructions and profiles
- Generating content and performing tasks with Foundation Models
- Adding server-side intelligence with Private Cloud Compute
- Analyzing images with multimodal prompting
- Categorizing and organizing data with content tags
- Generating Swift data structures with guided generation
- Improving the safety of generative model output
- Managing the context window
- Prompting an on-device foundation model
- Supporting languages and locales with Foundation Models
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
init(profile:history:)init(model:dynamicInstructions:history:)LanguageModelSession.DynamicProfileLanguageModelSession.DynamicProfileModifierLanguageModelSession.ConditionalDynamicProfileLanguageModelSession.DynamicProfileBuilderLanguageModelSession.DynamicProfileModifierContentLanguageModelSession.ModifiedDynamicProfileLanguageModelSession.Profile
Preloading the model
Inspecting a session
Generating a response
isRespondingrespond(options:prompt:)respond(generating:includeSchemaInPrompt:options:prompt:)respond(schema:includeSchemaInPrompt:options:prompt:)respond(to:options:)respond(to:generating:includeSchemaInPrompt:options:)respond(to:schema:includeSchemaInPrompt:options:)LanguageModelSession.Response
Generating a response with metadata
respond(options:contextOptions:metadata:prompt:)respond(generating:options:contextOptions:metadata:prompt:)respond(schema:options:contextOptions:metadata:prompt:)respond(to:options:contextOptions:metadata:)respond(to:generating:options:contextOptions:metadata:)respond(to:schema:options:contextOptions:metadata:)
Streaming a response
streamResponse(options:prompt:)streamResponse(generating:includeSchemaInPrompt:options:prompt:)streamResponse(schema:includeSchemaInPrompt:options:prompt:)streamResponse(to:options:)streamResponse(to:generating:includeSchemaInPrompt:options:)streamResponse(to:schema:includeSchemaInPrompt:options:)LanguageModelSession.ResponseStream
Streaming a response with metadata
streamResponse(options:contextOptions:metadata:prompt:)streamResponse(generating:options:contextOptions:metadata:prompt:)streamResponse(schema:options:contextOptions:metadata:prompt:)streamResponse(to:options:contextOptions:metadata:)streamResponse(to:generating:options:contextOptions:metadata:)streamResponse(to:schema:options:contextOptions:metadata:)
Accessing the transcript
Generating feedback
logFeedbackAttachment(sentiment:issues:desiredOutput:)logFeedbackAttachment(sentiment:issues:desiredResponseContent:)logFeedbackAttachment(sentiment:issues:desiredResponseText:)LanguageModelFeedback