|
Expand Up
|
@@ -29,7 +29,6 @@ |
|
|
import com.google.cloud.vertexai.api.GenerationConfig; |
|
|
import com.google.cloud.vertexai.api.SafetySetting; |
|
|
import com.google.cloud.vertexai.api.Tool; |
|
|
import com.google.cloud.vertexai.api.ToolConfig; |
|
|
import com.google.common.collect.ImmutableList; |
|
|
import java.io.IOException; |
|
|
import java.util.ArrayList; |
|
Expand All
|
@@ -41,8 +40,8 @@ public final class ChatSession { |
|
|
private final GenerativeModel model; |
|
|
private final Optional<ChatSession> rootChatSession; |
|
|
private final Optional<AutomaticFunctionCallingResponder> automaticFunctionCallingResponder; |
|
|
private List<Content> history; |
|
|
private int previousHistorySize; |
|
|
private List<Content> history = new ArrayList<>(); |
|
|
private int previousHistorySize = 0; |
|
|
private Optional<ResponseStream<GenerateContentResponse>> currentResponseStream; |
|
|
private Optional<GenerateContentResponse> currentResponse; |
|
|
|
|
Expand All
|
@@ -51,17 +50,14 @@ public final class ChatSession { |
|
|
* GenerationConfig) inherits from the model. |
|
|
*/ |
|
|
public ChatSession(GenerativeModel model) { |
|
|
this(model, new ArrayList<>(), 0, Optional.empty(), Optional.empty()); |
|
|
this(model, Optional.empty(), Optional.empty()); |
|
|
} |
|
|
|
|
|
/** |
|
|
* Creates a new chat session given a GenerativeModel instance and a root chat session. |
|
|
* Configurations of the chat (e.g., GenerationConfig) inherits from the model. |
|
|
* |
|
|
* @param model a {@link GenerativeModel} instance that generates contents in the chat. |
|
|
* @param history a list of {@link Content} containing interleaving conversation between "user" |
|
|
* and "model". |
|
|
* @param previousHistorySize the size of the previous history. |
|
|
* @param rootChatSession a root {@link ChatSession} instance. All the chat history in the current |
|
|
* chat session will be merged to the root chat session. |
|
|
* @param automaticFunctionCallingResponder an {@link AutomaticFunctionCallingResponder} instance |
|
Expand All
|
@@ -70,14 +66,10 @@ public ChatSession(GenerativeModel model) { |
|
|
*/ |
|
|
private ChatSession( |
|
|
GenerativeModel model, |
|
|
List<Content> history, |
|
|
int previousHistorySize, |
|
|
Optional<ChatSession> rootChatSession, |
|
|
Optional<AutomaticFunctionCallingResponder> automaticFunctionCallingResponder) { |
|
|
checkNotNull(model, "model should not be null"); |
|
|
this.model = model; |
|
|
this.history = history; |
|
|
this.previousHistorySize = previousHistorySize; |
|
|
this.rootChatSession = rootChatSession; |
|
|
this.automaticFunctionCallingResponder = automaticFunctionCallingResponder; |
|
|
currentResponseStream = Optional.empty(); |
|
Expand All
|
@@ -92,12 +84,15 @@ private ChatSession( |
|
|
* @return a new {@link ChatSession} instance with the specified GenerationConfig. |
|
|
*/ |
|
|
public ChatSession withGenerationConfig(GenerationConfig generationConfig) { |
|
|
return new ChatSession( |
|
|
model.withGenerationConfig(generationConfig), |
|
|
history, |
|
|
previousHistorySize, |
|
|
Optional.of(rootChatSession.orElse(this)), |
|
|
automaticFunctionCallingResponder); |
|
|
ChatSession rootChat = rootChatSession.orElse(this); |
|
|
ChatSession newChatSession = |
|
|
new ChatSession( |
|
|
model.withGenerationConfig(generationConfig), |
|
|
Optional.of(rootChat), |
|
|
automaticFunctionCallingResponder); |
|
|
newChatSession.history = history; |
|
|
newChatSession.previousHistorySize = previousHistorySize; |
|
|
return newChatSession; |
|
|
} |
|
|
|
|
|
/** |
|
Expand All
|
@@ -108,12 +103,15 @@ public ChatSession withGenerationConfig(GenerationConfig generationConfig) { |
|
|
* @return a new {@link ChatSession} instance with the specified SafetySettings. |
|
|
*/ |
|
|
public ChatSession withSafetySettings(List<SafetySetting> safetySettings) { |
|
|
return new ChatSession( |
|
|
model.withSafetySettings(safetySettings), |
|
|
history, |
|
|
previousHistorySize, |
|
|
Optional.of(rootChatSession.orElse(this)), |
|
|
automaticFunctionCallingResponder); |
|
|
ChatSession rootChat = rootChatSession.orElse(this); |
|
|
ChatSession newChatSession = |
|
|
new ChatSession( |
|
|
model.withSafetySettings(safetySettings), |
|
|
Optional.of(rootChat), |
|
|
automaticFunctionCallingResponder); |
|
|
newChatSession.history = history; |
|
|
newChatSession.previousHistorySize = previousHistorySize; |
|
|
return newChatSession; |
|
|
} |
|
|
|
|
|
/** |
|
Expand All
|
@@ -124,44 +122,13 @@ public ChatSession withSafetySettings(List<SafetySetting> safetySettings) { |
|
|
* @return a new {@link ChatSession} instance with the specified Tools. |
|
|
*/ |
|
|
public ChatSession withTools(List<Tool> tools) { |
|
|
return new ChatSession( |
|
|
model.withTools(tools), |
|
|
history, |
|
|
previousHistorySize, |
|
|
Optional.of(rootChatSession.orElse(this)), |
|
|
automaticFunctionCallingResponder); |
|
|
} |
|
|
|
|
|
/** |
|
|
* Creates a copy of the current ChatSession with updated ToolConfig. |
|
|
* |
|
|
* @param toolConfig a {@link com.google.cloud.vertexai.api.ToolConfig} that will be used in the |
|
|
* new ChatSession. |
|
|
* @return a new {@link ChatSession} instance with the specified ToolConfigs. |
|
|
*/ |
|
|
public ChatSession withToolConfig(ToolConfig toolConfig) { |
|
|
return new ChatSession( |
|
|
model.withToolConfig(toolConfig), |
|
|
history, |
|
|
previousHistorySize, |
|
|
Optional.of(rootChatSession.orElse(this)), |
|
|
automaticFunctionCallingResponder); |
|
|
} |
|
|
|
|
|
/** |
|
|
* Creates a copy of the current ChatSession with updated SystemInstruction. |
|
|
* |
|
|
* @param systemInstruction a {@link com.google.cloud.vertexai.api.Content} containing system |
|
|
* instructions. |
|
|
* @return a new {@link ChatSession} instance with the specified ToolConfigs. |
|
|
*/ |
|
|
public ChatSession withSystemInstruction(Content systemInstruction) { |
|
|
return new ChatSession( |
|
|
model.withSystemInstruction(systemInstruction), |
|
|
history, |
|
|
previousHistorySize, |
|
|
Optional.of(rootChatSession.orElse(this)), |
|
|
automaticFunctionCallingResponder); |
|
|
ChatSession rootChat = rootChatSession.orElse(this); |
|
|
ChatSession newChatSession = |
|
|
new ChatSession( |
|
|
model.withTools(tools), Optional.of(rootChat), automaticFunctionCallingResponder); |
|
|
newChatSession.history = history; |
|
|
newChatSession.previousHistorySize = previousHistorySize; |
|
|
return newChatSession; |
|
|
} |
|
|
|
|
|
/** |
|
Expand All
|
@@ -174,12 +141,13 @@ public ChatSession withSystemInstruction(Content systemInstruction) { |
|
|
*/ |
|
|
public ChatSession withAutomaticFunctionCallingResponder( |
|
|
AutomaticFunctionCallingResponder automaticFunctionCallingResponder) { |
|
|
return new ChatSession( |
|
|
model, |
|
|
history, |
|
|
previousHistorySize, |
|
|
Optional.of(rootChatSession.orElse(this)), |
|
|
Optional.of(automaticFunctionCallingResponder)); |
|
|
ChatSession rootChat = rootChatSession.orElse(this); |
|
|
ChatSession newChatSession = |
|
|
new ChatSession( |
|
|
model, Optional.of(rootChat), Optional.of(automaticFunctionCallingResponder)); |
|
|
newChatSession.history = history; |
|
|
newChatSession.previousHistorySize = previousHistorySize; |
|
|
return newChatSession; |
|
|
} |
|
|
|
|
|
/** |
|
Expand Down
|
|