Class Chat
This class provides a way to interact with a generative model in a multi-turn chat session. It keeps track of the chat history and uses it to provide context for subsequent messages.
Note: this class is NOT thread-safe.
-
Method Summary
Modifier and TypeMethodDescriptioncom.google.common.collect.ImmutableList<Content>
getHistory
(boolean curated) Returns the chat history.sendMessage
(Content content) Sends a message to the model in the current multi-turn chat session and returns the model's response.sendMessage
(Content content, GenerateContentConfig config) Sends a message to the model in the current multi-turn chat session and returns the model's response.sendMessage
(String text) Sends a message to the model in the current multi-turn chat session and returns the model's response.sendMessage
(String text, GenerateContentConfig config) Sends a message to the model in the current multi-turn chat session and returns the model's response.sendMessage
(List<Content> contents) Sends a message to the model in the current multi-turn chat session and returns the model's response.sendMessage
(List<Content> contents, GenerateContentConfig config) Sends a message to the model in the current multi-turn chat session and returns the model's response.sendMessageStream
(Content content) Sends a message to the model in the current multi-turn chat session and returns a stream of responses.sendMessageStream
(Content content, GenerateContentConfig config) Sends a message to the model in the current multi-turn chat session and returns a stream of responses.sendMessageStream
(String text) Sends a message to the model in the current multi-turn chat session and returns a stream of responses.sendMessageStream
(String text, GenerateContentConfig config) Sends a message to the model in the current multi-turn chat session and returns a stream of responses.sendMessageStream
(List<Content> contents) Sends a message to the model in the current multi-turn chat session and returns the model's response.sendMessageStream
(List<Content> contents, GenerateContentConfig config) Sends a message to the model in the current multi-turn chat session and returns a stream of responses.
-
Method Details
-
sendMessage
Sends a message to the model in the current multi-turn chat session and returns the model's response.This appends the message and the model's response to the chat history, which is sent back to the model in subsequent calls to sendMessage. Be sure to initialize a chat session using chats.create() before calling sendMessage.
- Parameters:
text
- the text to send to the generative modelconfig
- aGenerateContentConfig
instance that specifies the optional configurations
-
sendMessage
Sends a message to the model in the current multi-turn chat session and returns the model's response.This appends the message and the model's response to the chat history, which is sent back to the model in subsequent calls to sendMessage. Be sure to initialize a chat session using chats.create() before calling sendMessage.
- Parameters:
text
- the text to send to the generative model
-
sendMessage
Sends a message to the model in the current multi-turn chat session and returns the model's response.This appends the message and the model's response to the chat history, which is sent back to the model in subsequent calls to sendMessage. Be sure to initialize a chat session using chats.create() before calling sendMessage.
- Parameters:
content
- aContent
to send to the generative modelconfig
- aGenerateContentConfig
instance that specifies the optional configurations
-
sendMessage
Sends a message to the model in the current multi-turn chat session and returns the model's response.This appends the message and the model's response to the chat history, which is sent back to the model in subsequent calls to sendMessage. Be sure to initialize a chat session using chats.create() before calling sendMessage.
- Parameters:
content
- aContent
to send to the generative model
-
sendMessage
Sends a message to the model in the current multi-turn chat session and returns the model's response.This appends the message and the model's response to the chat history, which is sent back to the model in subsequent calls to sendMessage. Be sure to initialize a chat session using chats.create() before calling sendMessage.
- Parameters:
contents
- aList<Content>
to send to the generative modelconfig
- aGenerateContentConfig
instance that specifies the optional configurations
-
sendMessage
Sends a message to the model in the current multi-turn chat session and returns the model's response.This appends the message and the model's response to the chat history, which is sent back to the model in subsequent calls to sendMessage. Be sure to initialize a chat session using chats.create() before calling sendMessage.
-
sendMessageStream
public ResponseStream<GenerateContentResponse> sendMessageStream(String text, GenerateContentConfig config) Sends a message to the model in the current multi-turn chat session and returns a stream of responses.This appends the message and the model's response to the chat history after the stream is consumed. Be sure to initialize a chat session using chats.create() before calling sendMessageStream.
- Parameters:
text
- the text to send to the generative modelconfig
- aGenerateContentConfig
instance that specifies the optional configurations *
-
sendMessageStream
Sends a message to the model in the current multi-turn chat session and returns a stream of responses.This appends the message and the model's response to the chat history after the stream is consumed. Be sure to initialize a chat session using chats.create() before calling sendMessageStream.
- Parameters:
text
- the text to send to the generative model
-
sendMessageStream
public ResponseStream<GenerateContentResponse> sendMessageStream(Content content, GenerateContentConfig config) Sends a message to the model in the current multi-turn chat session and returns a stream of responses.This appends the message and the model's response to the chat history after the stream is consumed. Be sure to initialize a chat session using chats.create() before calling sendMessageStream.
- Parameters:
content
- aContent
to send to the generative modelconfig
- aGenerateContentConfig
instance that specifies the optional configurations *
-
sendMessageStream
Sends a message to the model in the current multi-turn chat session and returns a stream of responses.This appends the message and the model's response to the chat history after the stream is consumed. Be sure to initialize a chat session using chats.create() before calling sendMessageStream.
- Parameters:
content
- aContent
to send to the generative model
-
sendMessageStream
public ResponseStream<GenerateContentResponse> sendMessageStream(List<Content> contents, GenerateContentConfig config) Sends a message to the model in the current multi-turn chat session and returns a stream of responses.This appends the message and the model's response to the chat history after the stream is consumed. Be sure to initialize a chat session using chats.create() before calling sendMessageStream.
- Parameters:
contents
- aList<Content>
to send to the generative modelconfig
- aGenerateContentConfig
instance that specifies the optional configurations
-
sendMessageStream
Sends a message to the model in the current multi-turn chat session and returns the model's response.This appends the message and the model's response to the chat history after the stream is consumed. Be sure to initialize a chat session using chats.create() before calling sendMessageStream.m
-
getHistory
Returns the chat history.- Parameters:
curated
- Whether to return the curated history or the comprehensive history. Comprehensive history includes all messages, including empty or invalid parts. Curated history excludes empty or invalid parts.
-