Automatic Rule Generation#
Automatic rule generation lets users describe an app's purpose in plain text and have an LLM produce a complete prompt, input variables, and an opening statement for a basic (non-workflow) Dify app. It surfaces in the app configuration UI as the "Auto generate" button, opening the GetAutomaticRes modal.
Entry Points#
| Layer | Location |
|---|---|
| UI modal | get-automatic-res.tsx |
| Frontend API calls | web/service/debug.ts — generateBasicAppFirstTimeRule & generateRule |
| Template pre-fill | useGenerateRuleTemplate in web/service/use-apps.ts |
| Backend route | POST /rule-generate → RuleGenerateApi |
| Backend logic | LLMGenerator.generate_rule_config() in api/core/llm_generator/llm_generator.py |
How It Works#
1. UI — GetAutomaticRes component#
The GetAutomaticRes dialog is the main user-facing surface. Key behaviors:
- Model selection: reads from
useAutoGenModel(auto-gen-model-storage.ts), which persists the chosen model tolocalStorageunder the keyauto-gen-model. Falls back to the tenant's default text-generation model . - Instruction pre-fill: on mount, fetches a starter instruction template from
POST /instruction-generate/templateviauseGenerateRuleTemplate. - Template shortcuts: in
isBasicMode, nine preset use-cases (Python debugger, translator, meeting takeaways, etc.) appear as quick-fill chips . - Generation path: when
isBasicMode || !currentPrompt,onGeneratecallsgenerateBasicAppFirstTimeRule; otherwise it callsgenerateRulefor prompt-improvement on existing workflow nodes. - Versioning: results are stored session-wide via
useGenData, allowing the user to browse and compare multiple generated versions before applying. - Apply confirmation: selecting "Apply" triggers an
AlertDialogoverwrite confirmation before callingonFinished(current).
2. Frontend service layer#
generateBasicAppFirstTimeRule issues POST /rule-generate with body { instruction, model_config, no_variable }.
generateRule issues POST /instruction-generate — used when an existing prompt is present (workflow node or legacy app improvement).
The response type GenRes carries modified (the generated/revised prompt), plus optional variables[] and opening_statement that are only populated by rule-generate.
3. Backend — RuleGenerateApi and LLMGenerator#
RuleGenerateApi.post() validates the request as RuleGeneratePayload (instruction, model_config_data, no_variable) and delegates to LLMGenerator.generate_rule_config().
generate_rule_config() has two code paths :
no_variable=True (1 LLM call — used by basic app first-time generation):
Formats WORKFLOW_RULE_CONFIG_PROMPT_GENERATE_TEMPLATE with the user's instruction and invokes the LLM once. The response text becomes rule_config["prompt"].
no_variable=False (3 sequential LLM calls — used when variable extraction is needed):
- Prompt generation — uses
RULE_CONFIG_PROMPT_GENERATE_TEMPLATE→rule_config["prompt"] - Variable extraction — uses
RULE_CONFIG_PARAMETER_GENERATE_TEMPLATEon the generated prompt; extracts variable names withre.findall(r'"\s*([^"]+)\s*"', ...)→rule_config["variables"] - Opening statement — uses
RULE_CONFIG_STATEMENT_GENERATE_TEMPLATE→rule_config["opening_statement"]
Prompt templates live in api/core/llm_generator/prompts.py. Output parsing helpers are in api/core/llm_generator/output_parser/rule_config_generator.py.
Related Files#
| File | Purpose |
|---|---|
automatic/ | All UI sub-components for the generation modal (result.tsx, instruction-editor.tsx, version-selector.tsx, etc.) |
api/core/llm_generator/llm_generator.py | Core LLM generation logic for rule/prompt/code/workflow generation |
api/controllers/console/app/generator.py | Flask routes for all generator endpoints |
api/core/llm_generator/prompts.py | Raw prompt templates used by all generator methods |
api/core/llm_generator/entities.py | Pydantic request/response models (RuleGeneratePayload, etc.) |