Chat

public final class Chat


Representation of a multi-turn interaction with a model.

Handles the capturing and storage of the communication with the model, providing methods for further interaction.

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 interactions with the model

Public constructors

Public methods

final @NonNull GenerateContentResponse

Generates a response from the backend with the provided image prompt.

final @NonNull GenerateContentResponse

Generates a response from the backend with the provided Content, and any previous ones sent/returned from this chat.

final @NonNull GenerateContentResponse

Generates a response from the backend with the provided text prompt.

final @NonNull Flow<@NonNull GenerateContentResponse>

Generates a streaming response from the backend with the provided image prompt.

final @NonNull Flow<@NonNull GenerateContentResponse>

Generates a streaming response from the backend with the provided Content.

final @NonNull Flow<@NonNull GenerateContentResponse>

Generates a streaming response from the backend with the provided text prompt.

Public fields

history

public final @NonNull List<@NonNull Contenthistory

The previous interactions with the model

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)

Generates a response from the backend with the provided image prompt.

Parameters
@NonNull Bitmap prompt

The image to be converted into a single piece of Content to send to the model.

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

Generates a response from the backend with the provided Content, and any previous ones sent/returned from this chat.

Parameters
@NonNull Content prompt

A Content to send to the model.

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

if the 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)

Generates a response from the backend with the provided text prompt.

Parameters
@NonNull String prompt

The text to be converted into a single piece of Content to send to the model.

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

Generates a streaming response from the backend with the provided image prompt.

Parameters
@NonNull Bitmap prompt

A Content to send to the model.

Returns
@NonNull Flow<@NonNull GenerateContentResponse>

A Flow which will emit responses as they are returned from the model.

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

Generates a streaming response from the backend with the provided Content.

Parameters
@NonNull Content prompt

A Content to send to the model.

Returns
@NonNull Flow<@NonNull GenerateContentResponse>

A Flow which will emit responses as they are returned from the model.

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

if the 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)

Generates a streaming response from the backend with the provided text prompt.

Parameters
@NonNull String prompt

a text to be converted into a single piece of Content to send to the model

Returns
@NonNull Flow<@NonNull GenerateContentResponse>

A Flow which will emit responses as they are returned from the model.

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

if the Chat instance has an active request.