Skip to content

Commit dfb401a

Browse files
toi500HenryHengZJ
andauthored
feat: Add configurable system prompt to Condition Agent (#4587)
* feat: Add configurable system prompt to Condition Agent * Update system prompt to HTML for UI readability * fix: Remove invalid default routing and sync hardcoded role-based examples * Update ConditionAgent.ts * Update ConditionAgent.ts --------- Co-authored-by: Henry Heng <[email protected]>
1 parent 21caedd commit dfb401a

File tree

2 files changed

+79
-57
lines changed

2 files changed

+79
-57
lines changed

packages/components/nodes/agentflow/ConditionAgent/ConditionAgent.ts

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class ConditionAgent_Agentflow implements INode {
2727
constructor() {
2828
this.label = 'Condition Agent'
2929
this.name = 'conditionAgentAgentflow'
30-
this.version = 1.0
30+
this.version = 1.1
3131
this.type = 'ConditionAgent'
3232
this.category = 'Agent Flows'
3333
this.description = `Utilize an agent to split flows based on dynamic conditions`
@@ -80,6 +80,26 @@ class ConditionAgent_Agentflow implements INode {
8080
scenario: ''
8181
}
8282
]
83+
},
84+
{
85+
label: 'Override System Prompt',
86+
name: 'conditionAgentOverrideSystemPrompt',
87+
type: 'boolean',
88+
description: 'Override initial system prompt for Condition Agent',
89+
optional: true
90+
},
91+
{
92+
label: 'Node System Prompt',
93+
name: 'conditionAgentSystemPrompt',
94+
type: 'string',
95+
rows: 4,
96+
optional: true,
97+
acceptVariable: true,
98+
default: CONDITION_AGENT_SYSTEM_PROMPT,
99+
description: 'Expert use only. Modifying this can significantly alter agent behavior. Leave default if unsure',
100+
show: {
101+
conditionAgentOverrideSystemPrompt: true
102+
}
83103
}
84104
/*{
85105
label: 'Enable Memory',
@@ -242,6 +262,12 @@ class ConditionAgent_Agentflow implements INode {
242262
const conditionAgentInput = nodeData.inputs?.conditionAgentInput as string
243263
let input = conditionAgentInput || question
244264
const conditionAgentInstructions = nodeData.inputs?.conditionAgentInstructions as string
265+
const conditionAgentSystemPrompt = nodeData.inputs?.conditionAgentSystemPrompt as string
266+
const conditionAgentOverrideSystemPrompt = nodeData.inputs?.conditionAgentOverrideSystemPrompt as boolean
267+
let systemPrompt = CONDITION_AGENT_SYSTEM_PROMPT
268+
if (conditionAgentSystemPrompt && conditionAgentOverrideSystemPrompt) {
269+
systemPrompt = conditionAgentSystemPrompt
270+
}
245271

246272
// Extract memory and configuration options
247273
const enableMemory = nodeData.inputs?.conditionAgentEnableMemory as boolean
@@ -277,31 +303,15 @@ class ConditionAgent_Agentflow implements INode {
277303
const messages: BaseMessageLike[] = [
278304
{
279305
role: 'system',
280-
content: CONDITION_AGENT_SYSTEM_PROMPT
306+
content: systemPrompt
281307
},
282308
{
283309
role: 'user',
284-
content: `{"input": "Hello", "scenarios": ["user is asking about AI", "default"], "instruction": "Your task is to check and see if user is asking topic about AI"}`
310+
content: `{"input": "Hello", "scenarios": ["user is asking about AI", "user is not asking about AI"], "instruction": "Your task is to check if the user is asking about AI."}`
285311
},
286312
{
287313
role: 'assistant',
288-
content: `\`\`\`json\n{"output": "default"}\n\`\`\``
289-
},
290-
{
291-
role: 'user',
292-
content: `{"input": "What is AIGC?", "scenarios": ["user is asking about AI", "default"], "instruction": "Your task is to check and see if user is asking topic about AI"}`
293-
},
294-
{
295-
role: 'assistant',
296-
content: `\`\`\`json\n{"output": "user is asking about AI"}\n\`\`\``
297-
},
298-
{
299-
role: 'user',
300-
content: `{"input": "Can you explain deep learning?", "scenarios": ["user is interested in AI topics", "default"], "instruction": "Determine if the user is interested in learning about AI"}`
301-
},
302-
{
303-
role: 'assistant',
304-
content: `\`\`\`json\n{"output": "user is interested in AI topics"}\n\`\`\``
314+
content: `\`\`\`json\n{"output": "user is not asking about AI"}\n\`\`\``
305315
}
306316
]
307317
// Use to store messages with image file references as we do not want to store the base64 data into database
@@ -374,15 +384,19 @@ class ConditionAgent_Agentflow implements INode {
374384
)
375385
}
376386

377-
let calledOutputName = 'default'
387+
let calledOutputName: string
378388
try {
379389
const parsedResponse = this.parseJsonMarkdown(response.content as string)
380-
if (!parsedResponse.output) {
381-
throw new Error('Missing "output" key in response')
390+
if (!parsedResponse.output || typeof parsedResponse.output !== 'string') {
391+
throw new Error('LLM response is missing the "output" key or it is not a string.')
382392
}
383393
calledOutputName = parsedResponse.output
384394
} catch (error) {
385-
console.warn(`Failed to parse LLM response: ${error}. Using default output.`)
395+
throw new Error(
396+
`Failed to parse a valid scenario from the LLM's response. Please check if the model is capable of following JSON output instructions. Raw LLM Response: "${
397+
response.content as string
398+
}"`
399+
)
386400
}
387401

388402
// Clean up empty inputs

packages/components/nodes/agentflow/prompt.ts

Lines changed: 41 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -39,37 +39,45 @@ export const DEFAULT_HUMAN_INPUT_DESCRIPTION_HTML = `<p>Summarize the conversati
3939
</ul>
4040
`
4141

42-
export const CONDITION_AGENT_SYSTEM_PROMPT = `You are part of a multi-agent system designed to make agent coordination and execution easy. Your task is to analyze the given input and select one matching scenario from a provided set of scenarios. If none of the scenarios match the input, you should return "default."
43-
44-
- **Input**: A string representing the user's query or message.
45-
- **Scenarios**: A list of predefined scenarios that relate to the input.
46-
- **Instruction**: Determine if the input fits any of the scenarios.
47-
48-
## Steps
49-
50-
1. **Read the input string** and the list of scenarios.
51-
2. **Analyze the content of the input** to identify its main topic or intention.
52-
3. **Compare the input with each scenario**:
53-
- If a scenario matches the main topic of the input, select that scenario.
54-
- If no scenarios match, prepare to output "\`\`\`json\n{"output": "default"}\`\`\`"
55-
4. **Output the result**: If a match is found, return the corresponding scenario in JSON; otherwise, return "\`\`\`json\n{"output": "default"}\`\`\`"
56-
57-
## Output Format
58-
59-
Output should be a JSON object that either names the matching scenario or returns "\`\`\`json\n{"output": "default"}\`\`\`" if no scenarios match. No explanation is needed.
60-
61-
## Examples
62-
63-
1. **Input**: {"input": "Hello", "scenarios": ["user is asking about AI", "default"], "instruction": "Your task is to check and see if user is asking topic about AI"}
64-
**Output**: "\`\`\`json\n{"output": "default"}\`\`\`"
65-
66-
2. **Input**: {"input": "What is AIGC?", "scenarios": ["user is asking about AI", "default"], "instruction": "Your task is to check and see if user is asking topic about AI"}
67-
**Output**: "\`\`\`json\n{"output": "user is asking about AI"}\`\`\`"
68-
69-
3. **Input**: {"input": "Can you explain deep learning?", "scenarios": ["user is interested in AI topics", "default"], "instruction": "Determine if the user is interested in learning about AI"}
70-
**Output**: "\`\`\`json\n{"output": "user is interested in AI topics"}\`\`\`"
71-
72-
## Note
73-
- Ensure that the input scenarios align well with potential user queries for accurate matching
74-
- DO NOT include anything other than the JSON in your response.
42+
export const CONDITION_AGENT_SYSTEM_PROMPT = `
43+
<p>You are part of a multi-agent system designed to make agent coordination and execution easy. Your task is to analyze the given input and select one matching scenario from a provided set of scenarios.</p>
44+
45+
<ul>
46+
<li><strong>Input</strong>: A string representing the user's query, message or data.</li>
47+
<li><strong>Scenarios</strong>: A list of predefined scenarios that relate to the input.</li>
48+
<li><strong>Instruction</strong>: Determine which of the provided scenarios is the best fit for the input.</li>
49+
</ul>
50+
51+
<h2>Steps</h2>
52+
<ol>
53+
<li><strong>Read the input string</strong> and the list of scenarios.</li>
54+
<li><strong>Analyze the content of the input</strong> to identify its main topic or intention.</li>
55+
<li><strong>Compare the input with each scenario</strong>: Evaluate how well the input's topic or intention aligns with each of the provided scenarios and select the one that is the best fit.</li>
56+
<li><strong>Output the result</strong>: Return the selected scenario in the specified JSON format.</li>
57+
</ol>
58+
59+
<h2>Output Format</h2>
60+
<p>Output should be a JSON object that names the selected scenario, like this: <code>{"output": "<selected_scenario_name>"}</code>. No explanation is needed.</p>
61+
62+
<h2>Examples</h2>
63+
<ol>
64+
<li>
65+
<p><strong>Input</strong>: <code>{"input": "Hello", "scenarios": ["user is asking about AI", "user is not asking about AI"], "instruction": "Your task is to check if the user is asking about AI."}</code></p>
66+
<p><strong>Output</strong>: <code>{"output": "user is not asking about AI"}</code></p>
67+
</li>
68+
<li>
69+
<p><strong>Input</strong>: <code>{"input": "What is AIGC?", "scenarios": ["user is asking about AI", "user is asking about the weather"], "instruction": "Your task is to check and see if the user is asking a topic about AI."}</code></p>
70+
<p><strong>Output</strong>: <code>{"output": "user is asking about AI"}</code></p>
71+
</li>
72+
<li>
73+
<p><strong>Input</strong>: <code>{"input": "Can you explain deep learning?", "scenarios": ["user is interested in AI topics", "user wants to order food"], "instruction": "Determine if the user is interested in learning about AI."}</code></p>
74+
<p><strong>Output</strong>: <code>{"output": "user is interested in AI topics"}</code></p>
75+
</li>
76+
</ol>
77+
78+
<h2>Note</h2>
79+
<ul>
80+
<li>Ensure that the input scenarios align well with potential user queries for accurate matching.</li>
81+
<li>DO NOT include anything other than the JSON in your response.</li>
82+
</ul>
7583
`

0 commit comments

Comments
 (0)