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 methods |
|
|---|---|
final @NonNull GenerateContentResponse |
sendMessage(@NonNull Bitmap prompt)Sends a message using the existing history of this chat as context and the provided image prompt. |
final @NonNull GenerateContentResponse |
sendMessage(@NonNull Content prompt)Sends a message using the provided |
final @NonNull GenerateContentResponse |
sendMessage(@NonNull String prompt)Sends a message using the provided |
final @NonNull Flow<@NonNull GenerateContentResponse> |
sendMessageStream(@NonNull Bitmap prompt)Sends a message using the existing history of this chat as context and the provided image prompt. |
final @NonNull Flow<@NonNull GenerateContentResponse> |
sendMessageStream(@NonNull Content prompt)Sends a message using the existing history of this chat as context and the provided |
final @NonNull Flow<@NonNull GenerateContentResponse> |
sendMessageStream(@NonNull String prompt)Sends a message using the existing history of this chat as context and the provided text prompt. |
Public fields
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.ai.type.InvalidStateException com.google.firebase.ai.type.InvalidStateException |
if |
com.google.firebase.ai.type.InvalidStateException com.google.firebase.ai.type.InvalidStateException |
if the |
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.ai.type.InvalidStateException com.google.firebase.ai.type.InvalidStateException |
if |
com.google.firebase.ai.type.InvalidStateException com.google.firebase.ai.type.InvalidStateException |
if the |
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.ai.type.InvalidStateException com.google.firebase.ai.type.InvalidStateException |
if |
com.google.firebase.ai.type.InvalidStateException com.google.firebase.ai.type.InvalidStateException |
if the |
sendMessageStream
public final @NonNull Flow<@NonNull GenerateContentResponse> sendMessageStream(@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.ai.type.InvalidStateException com.google.firebase.ai.type.InvalidStateException |
if |
com.google.firebase.ai.type.InvalidStateException com.google.firebase.ai.type.InvalidStateException |
if the |
sendMessageStream
public final @NonNull Flow<@NonNull GenerateContentResponse> sendMessageStream(@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.ai.type.InvalidStateException com.google.firebase.ai.type.InvalidStateException |
if |
com.google.firebase.ai.type.InvalidStateException com.google.firebase.ai.type.InvalidStateException |
if the |
sendMessageStream
public final @NonNull Flow<@NonNull GenerateContentResponse> sendMessageStream(@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.ai.type.InvalidStateException com.google.firebase.ai.type.InvalidStateException |
if |
com.google.firebase.ai.type.InvalidStateException com.google.firebase.ai.type.InvalidStateException |
if the |