Class ChatSession (1.23.0)

public final class ChatSession

Represents a conversation between the user and the model.

Note: this class is NOT thread-safe.

Inheritance

java.lang.Object > ChatSession

Constructors

ChatSession(GenerativeModel model)

public ChatSession(GenerativeModel model)

Creates a new chat session given a GenerativeModel instance. Configurations of the chat (e.g., GenerationConfig) inherits from the model.

Parameter
Name Description
model GenerativeModel

Methods

getHistory()

public ImmutableList getHistory()

Returns the history of the conversation.

Returns
Type Description
com.google.common.collect.ImmutableList<Content>

a history of the conversation as an immutable list of Content.

sendMessage(Content content)

public GenerateContentResponse sendMessage(Content content)

Sends a message to the model and returns a response.

Parameter
Name Description
content Content

the content to be sent.

Returns
Type Description
GenerateContentResponse

a response.

Exceptions
Type Description
IOException

sendMessage(String text)

public GenerateContentResponse sendMessage(String text)

Sends a message to the model and returns a response.

Parameter
Name Description
text String

the message to be sent.

Returns
Type Description
GenerateContentResponse

a response.

Exceptions
Type Description
IOException

sendMessageStream(Content content)

public ResponseStream sendMessageStream(Content content)

Sends a message to the model and returns a stream of responses.

Parameter
Name Description
content Content

the content to be sent.

Returns
Type Description
ResponseStream<GenerateContentResponse>

an iterable in which each element is a GenerateContentResponse. Can be converted to stream by stream() method.

Exceptions
Type Description
IOException

sendMessageStream(String text)

public ResponseStream sendMessageStream(String text)

Sends a message to the model and returns a stream of responses.

Parameter
Name Description
text String

the message to be sent.

Returns
Type Description
ResponseStream<GenerateContentResponse>

an iterable in which each element is a GenerateContentResponse. Can be converted to stream by stream() method.

Exceptions
Type Description
IOException

setHistory(List history)

public void setHistory(List history)

Sets the history to a list of Content.

Parameter
Name Description
history List<Content>

A list of Content containing interleaving conversation between "user" and "model".

withAutomaticFunctionCallingResponder(AutomaticFunctionCallingResponder automaticFunctionCallingResponder)

public ChatSession withAutomaticFunctionCallingResponder(AutomaticFunctionCallingResponder automaticFunctionCallingResponder)

Creates a copy of the current ChatSession with updated AutomaticFunctionCallingResponder.

Parameter
Name Description
automaticFunctionCallingResponder AutomaticFunctionCallingResponder

an AutomaticFunctionCallingResponder instance that will be used in the new ChatSession.

Returns
Type Description
ChatSession

a new ChatSession instance with the specified AutomaticFunctionCallingResponder.

withGenerationConfig(GenerationConfig generationConfig)

public ChatSession withGenerationConfig(GenerationConfig generationConfig)

Creates a copy of the current ChatSession with updated GenerationConfig.

Parameter
Name Description
generationConfig GenerationConfig

a com.google.cloud.vertexai.api.GenerationConfig that will be used in the new ChatSession.

Returns
Type Description
ChatSession

a new ChatSession instance with the specified GenerationConfig.

withSafetySettings(List safetySettings)

public ChatSession withSafetySettings(List safetySettings)

Creates a copy of the current ChatSession with updated SafetySettings.

Parameter
Name Description
safetySettings List<SafetySetting>

a com.google.cloud.vertexai.api.SafetySetting that will be used in the new ChatSession.

Returns
Type Description
ChatSession

a new ChatSession instance with the specified SafetySettings.

withSystemInstruction(Content systemInstruction)

public ChatSession withSystemInstruction(Content systemInstruction)

Creates a copy of the current ChatSession with updated SystemInstruction.

Parameter
Name Description
systemInstruction Content

a com.google.cloud.vertexai.api.Content containing system instructions.

Returns
Type Description
ChatSession

a new ChatSession instance with the specified ToolConfigs.

withToolConfig(ToolConfig toolConfig)

public ChatSession withToolConfig(ToolConfig toolConfig)

Creates a copy of the current ChatSession with updated ToolConfig.

Parameter
Name Description
toolConfig ToolConfig

a com.google.cloud.vertexai.api.ToolConfig that will be used in the new ChatSession.

Returns
Type Description
ChatSession

a new ChatSession instance with the specified ToolConfigs.

withTools(List tools)

public ChatSession withTools(List tools)

Creates a copy of the current ChatSession with updated Tools.

Parameter
Name Description
tools List<Tool>

a com.google.cloud.vertexai.api.Tool that will be used in the new ChatSession.

Returns
Type Description
ChatSession

a new ChatSession instance with the specified Tools.