mirror of
https://github.com/microsoft/semantic-kernel.git
synced 2026-10-12 02:19:06 +00:00
Java: Allow creating empty ChatHistory (#5525)
### Motivation and Context <!-- Thank you for your contribution to the semantic-kernel repo! Please help reviewers and future users, providing the following information: 1. Why is this change required? 2. What problem does it solve? 3. What scenario does it contribute to? 4. If it fixes an open issue, please link to the issue here. --> ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> ### Contribution Checklist <!-- Before submitting this PR, please make sure: --> - [ ] The code builds clean without any errors or warnings - [ ] The PR follows the [SK Contribution Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md) and the [pre-submission formatting script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts) raises no violations - [ ] All unit tests pass, and I have added new tests where possible - [ ] I didn't break anyone 😄
This commit is contained in:
+20
-7
@@ -10,6 +10,7 @@ import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.Spliterator;
|
||||
import java.util.function.Consumer;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
/**
|
||||
* Provides a history of messages between the User, Assistant and System
|
||||
@@ -25,7 +26,17 @@ public class ChatHistory implements Iterable<ChatMessageContent<?>> {
|
||||
* chat history
|
||||
*/
|
||||
public ChatHistory() {
|
||||
this(DEFAULT_CHAT_SYSTEM_PROMPT);
|
||||
this(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor that can add the default system instructions to the chat history.
|
||||
*
|
||||
* @param addDefaultSystemPrompt If true, the default system prompt will be added to the chat,
|
||||
* otherwise empty
|
||||
*/
|
||||
public ChatHistory(boolean addDefaultSystemPrompt) {
|
||||
this(addDefaultSystemPrompt ? DEFAULT_CHAT_SYSTEM_PROMPT : null);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -33,13 +44,15 @@ public class ChatHistory implements Iterable<ChatMessageContent<?>> {
|
||||
*
|
||||
* @param instructions The instructions to add to the chat history
|
||||
*/
|
||||
public ChatHistory(String instructions) {
|
||||
public ChatHistory(@Nullable String instructions) {
|
||||
this.chatMessageContents = new ArrayList<>();
|
||||
this.chatMessageContents.add(
|
||||
new ChatMessageContent<>(
|
||||
AuthorRole.ASSISTANT,
|
||||
instructions == null || instructions.isEmpty() ? DEFAULT_CHAT_SYSTEM_PROMPT
|
||||
: instructions));
|
||||
if (instructions != null) {
|
||||
|
||||
this.chatMessageContents.add(
|
||||
new ChatMessageContent<>(
|
||||
AuthorRole.SYSTEM,
|
||||
instructions));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user