Chat
@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
public final class Chat : SendableAn object that represents a back-and-forth chat with a model, capturing the history and saving the context in memory between each message sent.
-
The previous content from the chat that has been successfully sent and received from the model. This will be provided to the model for each message sent as context for the discussion.
Declaration
Swift
public var history: [ModelContent] { get set } -
Sends a message using the existing history of this chat as context. If successful, the message and response will be added to the history. If unsuccessful, history will remain unchanged.
Throws
AGenerateContentErrorif an error occurred.Declaration
Swift
public func sendMessage(_ parts: any PartsRepresentable...) async throws -> GenerateContentResponseParameters
partsThe new content to send as a single chat message.
Return Value
The model’s response if no error occurred.
-
Sends a message using the existing history of this chat as context. If successful, the message and response will be added to the history. If unsuccessful, history will remain unchanged.
Throws
AGenerateContentErrorif an error occurred.Declaration
Swift
public func sendMessage(_ content: [ModelContent]) async throws -> GenerateContentResponseParameters
contentThe new content to send as a single chat message.
Return Value
The model’s response if no error occurred.
-
Sends a message using the existing history of this chat as context. If successful, the message and response will be added to the history. If unsuccessful, history will remain unchanged.
Declaration
Swift
@available(macOS 12.0, *) public func sendMessageStream(_ parts: any PartsRepresentable...) throws -> AsyncThrowingStream<GenerateContentResponse, Error>Parameters
partsThe new content to send as a single chat message.
Return Value
A stream containing the model’s response or an error if an error occurred.
-
Sends a message using the existing history of this chat as context. If successful, the message and response will be added to the history. If unsuccessful, history will remain unchanged.
Declaration
Swift
@available(macOS 12.0, *) public func sendMessageStream(_ content: [ModelContent]) throws -> AsyncThrowingStream<GenerateContentResponse, Error>Parameters
contentThe new content to send as a single chat message.
Return Value
A stream containing the model’s response or an error if an error occurred.