Chat

public final class Chat


Representation of a multi-turn interaction with a model.

Captures and stores the history of communication in memory, and provides it as context with each new message.

Note: This object is not thread-safe, and calling sendMessage multiple times without waiting for a response will throw an InvalidStateException.

Summary

Public fields

final @NonNull List<@NonNull Content>

The previous content from the chat that has been successfully sent and received from the model.

Public constructors

Public methods

final @NonNull GenerateContentResponse

Sends a message using the existing history of this chat as context and the provided image prompt.

final @NonNull GenerateContentResponse

Sends a message using the provided prompt; automatically providing the existing history as context.

final @NonNull GenerateContentResponse

Sends a message using the provided text prompt; automatically providing the existing history as context.

final @NonNull Flow<@NonNull GenerateContentResponse>

Sends a message using the existing history of this chat as context and the provided image prompt.

final @NonNull Flow<@NonNull GenerateContentResponse>

Sends a message using the existing history of this chat as context and the provided Content prompt.

final @NonNull Flow<@NonNull GenerateContentResponse>

Sends a message using the existing history of this chat as context and the provided text prompt.

Public fields

history

public final @NonNull List<@NonNull Contenthistory

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).

Public constructors

Chat

public Chat(@NonNull GenerativeModel model, @NonNull List<@NonNull Content> history)
Parameters
@NonNull GenerativeModel model

The model to use for the interaction.

Public methods

sendMessage

public final @NonNull GenerateContentResponse sendMessage(@NonNull Bitmap prompt)

Sends a message using the existing history of this chat as context and the provided image prompt.

If successful, the message and response will be added to the history. If unsuccessful, history will remain unchanged.

Parameters
@NonNull Bitmap prompt

The input that, together with the history, will be given to the model as the prompt.

Throws
com.google.firebase.vertexai.type.InvalidStateException com.google.firebase.vertexai.type.InvalidStateException

if prompt is not coming from the 'user' role.

com.google.firebase.vertexai.type.InvalidStateException com.google.firebase.vertexai.type.InvalidStateException

if the Chat instance has an active request.

sendMessage

public final @NonNull GenerateContentResponse sendMessage(@NonNull Content prompt)

Sends a message using the provided prompt; automatically providing the existing history as context.

If successful, the message and response will be added to the history. If unsuccessful, history will remain unchanged.

Parameters
@NonNull Content prompt

The input that, together with the history, will be given to the model as the prompt.

Throws
com.google.firebase.vertexai.type.InvalidStateException com.google.firebase.vertexai.type.InvalidStateException

if prompt is not coming from the 'user' role.

com.google.firebase.vertexai.type.InvalidStateException com.google.firebase.vertexai.type.InvalidStateException

if the Chat instance has an active request.

sendMessage

public final @NonNull GenerateContentResponse sendMessage(@NonNull String prompt)

Sends a message using the provided text prompt; automatically providing the existing history as context.

If successful, the message and response will be added to the history. If unsuccessful, history will remain unchanged.

Parameters
@NonNull String prompt

The input that, together with the history, will be given to the model as the prompt.

Throws
com.google.firebase.vertexai.type.InvalidStateException com.google.firebase.vertexai.type.InvalidStateException

if prompt is not coming from the 'user' role.

com.google.firebase.vertexai.type.InvalidStateException com.google.firebase.vertexai.type.InvalidStateException

if the Chat instance has an active request.

sendMessageStream

public final @NonNull Flow<@NonNull GenerateContentResponsesendMessageStream(@NonNull Bitmap prompt)

Sends a message using the existing history of this chat as context and the provided image prompt.

The response from the model is returned as a stream.

If successful, the message and response will be added to the history. If unsuccessful, history will remain unchanged.

Parameters
@NonNull Bitmap prompt

The input that, together with the history, will be given to the model as the prompt.

Throws
com.google.firebase.vertexai.type.InvalidStateException com.google.firebase.vertexai.type.InvalidStateException

if prompt is not coming from the 'user' role.

com.google.firebase.vertexai.type.InvalidStateException com.google.firebase.vertexai.type.InvalidStateException

if the Chat instance has an active request.

sendMessageStream

public final @NonNull Flow<@NonNull GenerateContentResponsesendMessageStream(@NonNull Content prompt)

Sends a message using the existing history of this chat as context and the provided Content prompt.

The response from the model is returned as a stream.

If successful, the message and response will be added to the history. If unsuccessful, history will remain unchanged.

Parameters
@NonNull Content prompt

The input that, together with the history, will be given to the model as the prompt.

Throws
com.google.firebase.vertexai.type.InvalidStateException com.google.firebase.vertexai.type.InvalidStateException

if prompt is not coming from the 'user' role.

com.google.firebase.vertexai.type.InvalidStateException com.google.firebase.vertexai.type.InvalidStateException

if the Chat instance has an active request.

sendMessageStream

public final @NonNull Flow<@NonNull GenerateContentResponsesendMessageStream(@NonNull String prompt)

Sends a message using the existing history of this chat as context and the provided text prompt.

The response from the model is returned as a stream.

If successful, the message and response will be added to the history. If unsuccessful, history will remain unchanged.

Parameters
@NonNull String prompt

The input(s) that, together with the history, will be given to the model as the prompt.

Throws
com.google.firebase.vertexai.type.InvalidStateException com.google.firebase.vertexai.type.InvalidStateException

if prompt is not coming from the 'user' role.

com.google.firebase.vertexai.type.InvalidStateException com.google.firebase.vertexai.type.InvalidStateException

if the Chat instance has an active request.