TemplateChat
public final class TemplateChat : Sendable[Public Preview] An ongoing conversation with a model backed by a server prompt template.
Warning
This API is a public preview and may be subject to change.
A TemplateChat manages conversation history on the client while utilizing the prompt,
system instructions, and configurations defined in a server prompt template. The template's
configurations apply to every turn automatically.
Function Calling in Chat
If the template defines function declarations in its frontmatter tools, the model may return
a FunctionCallPart in its response candidates. When this occurs, execute the local function
logic and supply the result as a FunctionResponsePart in a new message using one of the
sendMessage(_:) or sendMessageStream(_:) methods to continue the conversation.
For more details, see Multi-turn interactions with server prompt templates.
-
The previous content from the chat that has been successfully sent and received from the model.
This history is provided to the model for each subsequent message sent, and is injected into the server prompt template at the position marked by the
{{history}}tag.Declaration
Swift
public var history: [ModelContent] { get set } -
[Public Preview] Sends a message to the model and returns the response.
Warning
This API is a public preview and may be subject to change.
Throws
AGenerateContentErrorif the request failed, or anEncodingErrorifinputscannot be encoded.Declaration
Swift
public func sendMessage(_ content: [ModelContent]) async throws -> GenerateContentResponseParameters
contentThe message content to send to the model, including any
FunctionResponseParts responding to previous function calls; pass an empty array to send an empty turn.Return Value
The content generated by the model.
-
[Public Preview] Sends a message to the model and returns the response.
Warning
This API is a public preview and may be subject to change.
Calling this method with no arguments sends an empty turn, which runs the template's own prompt against the current history without adding any new content. This is typically how the first turn of a conversation is started when the template supplies the prompt itself:
// Template content: "What was the weather in {{city}} on {{date}}?\n{{history}}" let chat = model.startChat(templateID: "weather", inputs: ["city": "Boston", ...]) let response = try await chat.sendMessage()Throws
AGenerateContentErrorif the request failed, or anEncodingErrorifinputscannot be encoded.Declaration
Swift
public func sendMessage(_ parts: any PartsRepresentable...) async throws -> GenerateContentResponseParameters
partsThe new content to send as a single chat message; pass no arguments to send an empty turn.
Return Value
The content generated by the model.
-
[Public Preview] Sends a message to the model and returns the response as a stream of
GenerateContentResponses.Warning
This API is a public preview and may be subject to change.
Throws
AGenerateContentErrorif the request failed, or anEncodingErrorifinputscannot be encoded.Declaration
Swift
@available(macOS 12.0, watchOS 8.0, *) public func sendMessageStream(_ content: [ModelContent]) throws -> AsyncThrowingStream<GenerateContentResponse, Error>Parameters
contentThe message to send to the model, including any
FunctionResponseParts responding to previous function calls; pass an empty array to send an empty turn.Return Value
An
AsyncThrowingStreamthat yieldsGenerateContentResponseobjects. -
[Public Preview] Sends a message to the model and returns the response as a stream of
GenerateContentResponses.Warning
This API is a public preview and may be subject to change.
Calling this method with no arguments sends an empty turn, which runs the template's own prompt against the current history without adding any new content.
Throws
AGenerateContentErrorif the request failed, or anEncodingErrorifinputscannot be encoded.Declaration
Swift
@available(macOS 12.0, watchOS 8.0, *) public func sendMessageStream(_ parts: any PartsRepresentable...) throws -> AsyncThrowingStream<GenerateContentResponse, Error>Parameters
partsThe new content to send as a single chat message; pass no arguments to send an empty turn.
Return Value
An
AsyncThrowingStreamthat yieldsGenerateContentResponseobjects.