feat: Refactor conversation handling in BaseAgent and add metadata support in Conversation class
This commit is contained in:
@@ -384,20 +384,21 @@ public interface BaseAgent {
|
||||
private Conversation setupConversation(final Split split) {
|
||||
var conversation = split.context().getConversation();
|
||||
if (conversation == null || conversation.getConversation().isEmpty()) {
|
||||
conversation = Conversation.create(this.systemPrompt(), split.text());
|
||||
conversation = this.createConversation(this.systemPrompt(), split.text());
|
||||
split.context().setConversation(conversation);
|
||||
}
|
||||
else if (this.agentType(split.context()) == AgentType.START_CONVERSATION) {
|
||||
if (this.systemPrompt() == null || this.systemPrompt().isEmpty()) {
|
||||
conversation = Conversation.create(split.text());
|
||||
final var system = this.systemPrompt();
|
||||
if (system == null || system.isEmpty()) {
|
||||
conversation = this.createConversation(null, split.text());
|
||||
}
|
||||
else {
|
||||
conversation = Conversation.create(this.systemPrompt() + "\n" + split.text());
|
||||
conversation = this.createConversation(null, system + "\n" + split.text());
|
||||
}
|
||||
split.context().setConversation(conversation);
|
||||
}
|
||||
else if (this.agentType(split.context()) == AgentType.START_CONVERSATION_WITH_SYSTEM_PROMPT) {
|
||||
conversation = Conversation.create(this.systemPrompt(), split.text());
|
||||
conversation = this.createConversation(this.systemPrompt(), split.text());
|
||||
split.context().setConversation(conversation);
|
||||
}
|
||||
else {
|
||||
@@ -409,7 +410,7 @@ public interface BaseAgent {
|
||||
mergedSysAndUser = conversation.getConversation().get(0).text();
|
||||
}
|
||||
|
||||
final var newConversation = Conversation.create(mergedSysAndUser);
|
||||
final var newConversation = this.createConversation(null, mergedSysAndUser);
|
||||
for (int n = 2; n < conversation.getConversation().size(); n++) {
|
||||
newConversation.addTurn(conversation.getConversation().get(n));
|
||||
}
|
||||
@@ -429,6 +430,12 @@ public interface BaseAgent {
|
||||
return conversation;
|
||||
}
|
||||
|
||||
private Conversation createConversation(String system, String user) {
|
||||
var conversation = Conversation.create(system, user);
|
||||
conversation.setMetadata("Agent: " + this.name());
|
||||
return conversation;
|
||||
}
|
||||
|
||||
private void appendLastCompletionToConversation(final AgentContext context, final boolean skipCompletion) {
|
||||
if (skipCompletion || context.getConversation() == null || context.getLastCompletion() == null) {
|
||||
return;
|
||||
|
||||
@@ -36,6 +36,8 @@ public final class Conversation {
|
||||
}
|
||||
|
||||
private final List<Turn> conversation = Collections.synchronizedList(new ArrayList<>());
|
||||
|
||||
private String metadata; // Optional metadata that can be used to store additional information about the conversation, e.g. conversation id, user id, etc.
|
||||
|
||||
private Conversation() {
|
||||
}
|
||||
@@ -92,9 +94,21 @@ public final class Conversation {
|
||||
final StringBuilder builder = new StringBuilder();
|
||||
builder.append("Conversation [conversation=");
|
||||
builder.append(this.conversation);
|
||||
if (this.metadata != null) {
|
||||
builder.append(", metadata=");
|
||||
builder.append(this.metadata);
|
||||
}
|
||||
builder.append("]");
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
public String getMetadata() {
|
||||
return metadata;
|
||||
}
|
||||
|
||||
public void setMetadata(final String metadata) {
|
||||
this.metadata = metadata;
|
||||
}
|
||||
|
||||
public enum TurnType {
|
||||
SYSTEM, USER, ASSISTANT, TOOL
|
||||
|
||||
Reference in New Issue
Block a user