Class ChatSession (1.3.0)

public final class ChatSession

Represents a conversation between the user and the model

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<Content> 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<GenerateContentResponse> 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
IllegalArgumentException

sendMessageStream(String text)

public ResponseStream<GenerateContentResponse> 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<Content> history)

public void setHistory(List<Content> 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".

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<SafetySetting> safetySettings)

public ChatSession withSafetySettings(List<SafetySetting> 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.

withTools(List<Tool> tools)

public ChatSession withTools(List<Tool> 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.