feat: add SystemMessage to TextPrompt and FunctionPrompt

This commit is contained in:
Michael Yang 2024-10-30 09:51:08 +08:00
parent 76ddf203f8
commit c7b8dbb15e
3 changed files with 40 additions and 2 deletions

View File

@ -24,5 +24,7 @@ public class SystemMessage extends AbstractTextMessage {
this.content = content;
}
public static SystemMessage of(String content) {
return new SystemMessage(content);
}
}

View File

@ -22,12 +22,15 @@ import com.agentsflex.core.memory.ChatMemory;
import com.agentsflex.core.memory.DefaultChatMemory;
import com.agentsflex.core.message.HumanMessage;
import com.agentsflex.core.message.Message;
import com.agentsflex.core.message.SystemMessage;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
public class FunctionPrompt extends Prompt<FunctionMessageResponse> {
private SystemMessage systemMessage;
private final ChatMemory memory = new DefaultChatMemory();
private final List<Function> functions = new ArrayList<>();
@ -61,9 +64,25 @@ public class FunctionPrompt extends Prompt<FunctionMessageResponse> {
functions.addAll(Functions.from(funcObject, methodNames));
}
public SystemMessage getSystemMessage() {
return systemMessage;
}
public void setSystemMessage(SystemMessage systemMessage) {
this.systemMessage = systemMessage;
}
public ChatMemory getMemory() {
return memory;
}
@Override
public List<Message> toMessages() {
return memory.getMessages();
List<Message> messages = memory.getMessages();
if (systemMessage != null){
messages.add(0, systemMessage);
}
return messages;
}
public List<Function> getFunctions() {

View File

@ -18,12 +18,15 @@ package com.agentsflex.core.prompt;
import com.agentsflex.core.llm.response.AiMessageResponse;
import com.agentsflex.core.message.HumanMessage;
import com.agentsflex.core.message.Message;
import com.agentsflex.core.message.SystemMessage;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class TextPrompt extends Prompt<AiMessageResponse> {
private SystemMessage systemMessage;
protected String content;
public TextPrompt() {
@ -41,8 +44,22 @@ public class TextPrompt extends Prompt<AiMessageResponse> {
this.content = content;
}
public SystemMessage getSystemMessage() {
return systemMessage;
}
public void setSystemMessage(SystemMessage systemMessage) {
this.systemMessage = systemMessage;
}
@Override
public List<Message> toMessages() {
if (systemMessage != null) {
ArrayList<Message> messages = new ArrayList<>();
messages.add(systemMessage);
messages.add(new HumanMessage(content));
return messages;
}
return Collections.singletonList(new HumanMessage(content));
}