Class Chat

  • All Implemented Interfaces:

    
    public class Chat
    extends ChatBase
                        

    A chat session with a generative model.

    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.

    • Constructor Detail

    • Method Detail

      • sendMessage

         GenerateContentResponse sendMessage(String text, GenerateContentConfig config)

        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
        config - a com.google.genai.types.GenerateContentConfig instance that specifies the optional configurations
      • sendMessage

         GenerateContentResponse sendMessage(String text)

        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

         GenerateContentResponse sendMessage(Content content)

        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 - a com.google.genai.types.Content to send to the generative model
      • sendMessage

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

        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 - a < to send to the generative model
        config - a com.google.genai.types.GenerateContentConfig instance that specifies the optional configurations
      • sendMessage

         GenerateContentResponse sendMessage(List<Content> contents)

        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 - a < to send to the generative model
      • sendMessageStream

         ResponseStream<GenerateContentResponse> sendMessageStream(String text)

        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

         ResponseStream<GenerateContentResponse> sendMessageStream(Content content)

        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 - a com.google.genai.types.Content to send to the generative model
      • sendMessageStream

         ResponseStream<GenerateContentResponse> sendMessageStream(List<Content> contents)

        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

        Parameters:
        contents - a < to send to the generative model the optional configurations