mirror of
https://gitee.com/agents-flex/agents-flex.git
synced 2024-11-29 18:38:17 +08:00
feat: add system prompt config for LLMNode
This commit is contained in:
parent
c7b8dbb15e
commit
4548e311d0
@ -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) {
|
||||
|
@ -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() {
|
||||
|
Loading…
Reference in New Issue
Block a user