feat: add system prompt config for LLMNode

This commit is contained in:
Michael Yang 2024-10-30 09:57:11 +08:00
parent c7b8dbb15e
commit 4548e311d0
2 changed files with 13 additions and 6 deletions

View File

@ -21,6 +21,7 @@ import com.agentsflex.core.llm.ChatOptions;
import com.agentsflex.core.llm.Llm;
import com.agentsflex.core.llm.response.AiMessageResponse;
import com.agentsflex.core.message.AiMessage;
import com.agentsflex.core.message.SystemMessage;
import com.agentsflex.core.prompt.TextPrompt;
import com.agentsflex.core.prompt.template.TextPromptTemplate;
import com.agentsflex.core.util.Maps;
@ -93,16 +94,18 @@ public class LLMNode extends BaseNode {
protected Map<String, Object> execute(Chain chain) {
Map<String, Object> parameters = getParameters(chain);
if (userPromptTemplate == null){
if (userPromptTemplate == null) {
chain.stopError("user prompt is null or empty");
return Collections.emptyMap();
}
TextPrompt userPrompt = userPromptTemplate.format(parameters);
if (systemPromptTemplate != null) {
String systemPrompt = systemPromptTemplate.formatToString(parameters);
userPrompt.setSystemMessage(SystemMessage.of(systemPrompt));
}
AiMessageResponse response = llm.chat(userPrompt, chatOptions);
if (chain != null) {

View File

@ -87,6 +87,10 @@ public class TextPromptTemplate implements PromptTemplate<TextPrompt> {
}
public TextPrompt format(Map<String, Object> params) {
return new TextPrompt(formatToString(params));
}
public String formatToString(Map<String, Object> params) {
StringBuilder result = new StringBuilder();
for (String part : parts) {
if (part.charAt(0) == '{' && part.charAt(part.length() - 1) == '}') {
@ -99,7 +103,7 @@ public class TextPromptTemplate implements PromptTemplate<TextPrompt> {
result.append(part);
}
}
return new TextPrompt(result.toString());
return result.toString();
}
public Set<String> getKeys() {