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) {
|
private Conversation setupConversation(final Split split) {
|
||||||
var conversation = split.context().getConversation();
|
var conversation = split.context().getConversation();
|
||||||
if (conversation == null || conversation.getConversation().isEmpty()) {
|
if (conversation == null || conversation.getConversation().isEmpty()) {
|
||||||
conversation = Conversation.create(this.systemPrompt(), split.text());
|
conversation = this.createConversation(this.systemPrompt(), split.text());
|
||||||
split.context().setConversation(conversation);
|
split.context().setConversation(conversation);
|
||||||
}
|
}
|
||||||
else if (this.agentType(split.context()) == AgentType.START_CONVERSATION) {
|
else if (this.agentType(split.context()) == AgentType.START_CONVERSATION) {
|
||||||
if (this.systemPrompt() == null || this.systemPrompt().isEmpty()) {
|
final var system = this.systemPrompt();
|
||||||
conversation = Conversation.create(split.text());
|
if (system == null || system.isEmpty()) {
|
||||||
|
conversation = this.createConversation(null, split.text());
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
conversation = Conversation.create(this.systemPrompt() + "\n" + split.text());
|
conversation = this.createConversation(null, system + "\n" + split.text());
|
||||||
}
|
}
|
||||||
split.context().setConversation(conversation);
|
split.context().setConversation(conversation);
|
||||||
}
|
}
|
||||||
else if (this.agentType(split.context()) == AgentType.START_CONVERSATION_WITH_SYSTEM_PROMPT) {
|
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);
|
split.context().setConversation(conversation);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@@ -409,7 +410,7 @@ public interface BaseAgent {
|
|||||||
mergedSysAndUser = conversation.getConversation().get(0).text();
|
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++) {
|
for (int n = 2; n < conversation.getConversation().size(); n++) {
|
||||||
newConversation.addTurn(conversation.getConversation().get(n));
|
newConversation.addTurn(conversation.getConversation().get(n));
|
||||||
}
|
}
|
||||||
@@ -429,6 +430,12 @@ public interface BaseAgent {
|
|||||||
return conversation;
|
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) {
|
private void appendLastCompletionToConversation(final AgentContext context, final boolean skipCompletion) {
|
||||||
if (skipCompletion || context.getConversation() == null || context.getLastCompletion() == null) {
|
if (skipCompletion || context.getConversation() == null || context.getLastCompletion() == null) {
|
||||||
return;
|
return;
|
||||||
|
|||||||
@@ -36,6 +36,8 @@ public final class Conversation {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private final List<Turn> conversation = Collections.synchronizedList(new ArrayList<>());
|
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() {
|
private Conversation() {
|
||||||
}
|
}
|
||||||
@@ -92,9 +94,21 @@ public final class Conversation {
|
|||||||
final StringBuilder builder = new StringBuilder();
|
final StringBuilder builder = new StringBuilder();
|
||||||
builder.append("Conversation [conversation=");
|
builder.append("Conversation [conversation=");
|
||||||
builder.append(this.conversation);
|
builder.append(this.conversation);
|
||||||
|
if (this.metadata != null) {
|
||||||
|
builder.append(", metadata=");
|
||||||
|
builder.append(this.metadata);
|
||||||
|
}
|
||||||
builder.append("]");
|
builder.append("]");
|
||||||
return builder.toString();
|
return builder.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getMetadata() {
|
||||||
|
return metadata;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMetadata(final String metadata) {
|
||||||
|
this.metadata = metadata;
|
||||||
|
}
|
||||||
|
|
||||||
public enum TurnType {
|
public enum TurnType {
|
||||||
SYSTEM, USER, ASSISTANT, TOOL
|
SYSTEM, USER, ASSISTANT, TOOL
|
||||||
|
|||||||
Reference in New Issue
Block a user