Package com.google.genkit.ai.session
Class Chat<S>
java.lang.Object
com.google.genkit.ai.session.Chat<S>
- Type Parameters:
S- the type of the session state
Chat represents a conversation within a session thread.
Chat provides a simple interface for multi-turn conversations with automatic history management. Messages are persisted to the session store after each interaction.
Example usage:
// Simple chat
Chat<MyState> chat = session.chat();
ModelResponse response = chat.send("Hello!");
// Chat with system prompt
Chat<MyState> chat = session
.chat(ChatOptions.<MyState>builder().model("openai/gpt-4o").system("You are a helpful assistant.").build());
// Multi-turn conversation
chat.send("What is the capital of France?");
chat.send("And what about Germany?"); // Context is preserved
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classOptions for individual send operations. -
Method Summary
Modifier and TypeMethodDescriptionGets the current agent name.Gets the current conversation history.Gets the pending interrupt requests from the last send.Gets the session.Gets the thread name.booleanChecks if there are pending interrupts.Sends a message and gets a response.send(Message message, Chat.SendOptions sendOptions) Sends a message with send options and gets a response.Sends a message and gets a response.send(String text, Chat.SendOptions sendOptions) Sends a message with send options and gets a response.sendStream(Message message, Chat.SendOptions sendOptions, Consumer<ModelResponseChunk> streamCallback) Sends a message with streaming response.sendStream(String text, Consumer<ModelResponseChunk> streamCallback) Sends a message with streaming response.
-
Method Details
-
send
Sends a message and gets a response.This method:
- Adds the user message to history
- Builds a request with all conversation history
- Sends to the model and gets a response
- Adds the model response to history
- Persists the updated history
- Parameters:
text- the user message- Returns:
- the model response
- Throws:
GenkitException- if generation fails
-
send
Sends a message and gets a response.- Parameters:
message- the message to send- Returns:
- the model response
- Throws:
GenkitException- if generation fails
-
send
Sends a message with send options and gets a response.- Parameters:
text- the user messagesendOptions- additional options for this send- Returns:
- the model response
- Throws:
GenkitException- if generation fails
-
send
Sends a message with send options and gets a response.- Parameters:
message- the message to sendsendOptions- additional options for this send- Returns:
- the model response
- Throws:
GenkitException- if generation fails
-
sendStream
public ModelResponse sendStream(String text, Consumer<ModelResponseChunk> streamCallback) throws GenkitException Sends a message with streaming response.- Parameters:
text- the user messagestreamCallback- callback for each response chunk- Returns:
- the final aggregated response
- Throws:
GenkitException- if generation fails
-
sendStream
public ModelResponse sendStream(Message message, Chat.SendOptions sendOptions, Consumer<ModelResponseChunk> streamCallback) throws GenkitException Sends a message with streaming response.- Parameters:
message- the message to sendsendOptions- additional options for this sendstreamCallback- callback for each response chunk- Returns:
- the final aggregated response
- Throws:
GenkitException- if generation fails
-
getHistory
Gets the current conversation history.- Returns:
- a copy of the message history
-
getSession
Gets the session.- Returns:
- the parent session
-
getThreadName
Gets the thread name.- Returns:
- the thread name
-
getPendingInterrupts
Gets the pending interrupt requests from the last send.If the last
send(java.lang.String)call returned with interrupts, this method returns the list of pending interrupts that need to be resolved before continuing.- Returns:
- the list of pending interrupt requests (empty if none)
-
hasPendingInterrupts
public boolean hasPendingInterrupts()Checks if there are pending interrupts.- Returns:
- true if there are pending interrupts
-
getCurrentAgentName
Gets the current agent name.Returns null if no agent handoff has occurred, otherwise returns the name of the agent that the conversation was most recently handed off to.
- Returns:
- the current agent name, or null if no handoff has occurred
-